How to create a release signed apk file using Gradle?

前端 未结 30 1408
既然无缘
既然无缘 2020-11-22 13:48

I would like to have my Gradle build to create a release signed apk file using Gradle.

I\'m not sure if the code is correct or if I\'m missing a parameter when doing

相关标签:
30条回答
  • 2020-11-22 14:06

    If you want to avoid hardcoding your keystore & password in build.gradle, you can use a properties file as explained here: HANDLING SIGNING CONFIGS WITH GRADLE

    Basically:

    1) create a myproject.properties file at /home/[username]/.signing with such contents:

    keystore=[path to]\release.keystore
    keystore.password=*********
    keyAlias=***********
    keyPassword=********
    

    2) create a gradle.properties file (perhaps at the root of your project directory) with the contents:

    MyProject.properties=/home/[username]/.signing/myproject.properties
    

    3) refer to it in your build.gradle like this:

        if(project.hasProperty("MyProject.properties")
            && new File(project.property("MyProject.properties")).exists()) {
    
        Properties props = new Properties()
        props.load(new FileInputStream(file(project.property("MyProject.properties"))))
    
        signingConfigs {
            release {
                storeFile file(props['keystore'])
                storePassword props['keystore.password']
                keyAlias props['keyAlias']
                keyPassword props['keyPassword']
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:06

    Extending the answer by David Vavra,create a file ~/.gradle/gradle.properties and add

    RELEASE_STORE_FILE=/path/to/.keystore
    RELEASE_KEY_ALIAS=XXXXX
    RELEASE_STORE_PASSWORD=XXXXXXXXX
    RELEASE_KEY_PASSWORD=XXXXXXXXX
    

    Then in build.gradle

      signingConfigs {
        release {
        }
      }
    
      buildTypes {
        release {
          minifyEnabled true
          shrinkResources true
    
        }
      }
    
      // make this optional
      if ( project.hasProperty("RELEASE_KEY_ALIAS") ) {
        signingConfigs {
          release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
          }
        }
        buildTypes {
          release {
            signingConfig signingConfigs.release
          }
        }
      }
    
    0 讨论(0)
  • 2020-11-22 14:06

    if you don't want to see Cannot invoke method readLine() on null object. you need write in gradle.properties first.

    KEYSTORE_PASS=*****
    ALIAS_NAME=*****
    ALIAS_PASS=*****
    
    0 讨论(0)
  • 2020-11-22 14:07

    I had quite a lot of fun figuring this one out. Here is my walk-through.

    A to Z walk-through on how to create a gradle build file in IntelliJ (v.13.1.4) This walk-through assumes you know how to make a keystore file. For this tutorial to work you will need your keystore file to be located in your app folder and you will need to have your zipalign.exe file to be located in 'SDK-ROOT\tools'. This file is usually found in 'SDK-ROOT\build-tools' and under this folder it will be in the highest api folder (alpha or beta I recommend the alpha version).

    For those of you that wish to jump straight in here is the gradle build file.

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.9.+'
        }
    }
    apply plugin: 'android'
    
    repositories {
        mavenCentral()
    }
    android {
        compileSdkVersion 19
        buildToolsVersion '20.0.0'
        defaultConfig {
            minSdkVersion 8
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
        signingConfigs {
            playstore {
                keyAlias 'developers4u'
                keyPassword 'thisIsNotMyRealPassword'
                storeFile file('developers4u.keystore')
                storePassword 'realyItIsNot'
            }
        }
        buildTypes {
            assembleRelease {
                debuggable false
                jniDebugBuild false
                runProguard true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
                zipAlign true
                signingConfig signingConfigs.playstore
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:support-v4:20.0.0'
        implementation 'com.android.support:appcompat-v7:20.0.0'
    }
    

    You can build part of this build file (above) from menu option: File/Project Structure From here select Facets and click 'Android-Gradle(App). From here you will see tabs: 'Properties', 'Signing', 'Flavors', 'Build Types' and 'Dependencies' for this walk-through we will just be using 'Signing' and 'Build Types'. Under 'Build Types' (in the name section) enter any name that you wish to identify your build type configuration and in the other 4 fields enter your keystore information (setting the keystore path the the one under your app folder).

    Under the 'Build Types' enter the value 'assembleRelease' into the name field, 'Debuggable' should be set to false, 'Jni Debug Build' should be false, set 'Run Proguard' to true and 'Zip Align' to true. This will generate build file, but not as depicted above, you will have to add a few things to the build file afterwards. The ProGuard file location here will be set manually in the gradle build file. (as depicted above)

    The DSL containers you will have to add afterwards are as follows:

    android {
        ....
        compileSdkVersion 19
        buildToolsVersion '20.0.0'
        defaultConfig {
            minSdkVersion 8
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
        ....
    }
    

    You will also have to add:

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:support-v4:20.0.0'
        implementation 'com.android.support:appcompat-v7:20.0.0'
    }
    

    note this DSL container above('dependencies') should be at the bottom of the config file but not inside the android DSL container. In order to build the dependencies container from the IntelliJ menu, select: File/Project Structure. From there select Facets again and then Android-Gradle(app). You will see the same 5 tabs as mentioned above. Select the 'Dependencies' tab and add the dependencies you require.

    After all of this is done you should see a Gradle build file similar to the file at the top of this walk-through. To build your signed zip aligned release you will need to open the Gradle tasks. You can get to this window by selecting View/Tool Windows/Gradle. From here you can double Click 'assembleAssembleRelease. This should generate your deployable APK.

    The potential problems that can occur when compiling your release are (but not limited to): Your Gradle build file being in the wrong place. There are two Gradle build files; one in your application root folder and another in the app folder under the application root. You must user the latter.

    You may also have lint problems. (Note: Android Developer Studio is much better at spotting Lint problems than IntelliJ you will notice this when trying to generate an signed APK from the menu options)

    To get around lint problems you will need to put the following DSL container inside the android container (at the top):

    android {
            ....
        lintOptions {
            abortOnError false
        }
        ....
    }
    

    putting this inside your android DSL container will cause an error file to be generated in the build folder (directly under your app folder) the file name should be something like 'lint-results-release-fatal.html' this file will tell you the the class where the error occurred. Another file that will be generated is an XML file that contains the 'issue ID' associated with the lint error. The file name should be something like 'lint-results-release-fatal.xml'. Somewhere near the top of the file you will see a node 'issue' inside which you will see something similar to 'id="IDOfYourLintProblem"'

    To correct this problem open the file in your project that was listed in the 'lint-results-assembleRelease-fatal.html' file and enter the following line of code in the Java Class file just above the class name: @SuppressLint("IDOfYourLintProblem"). You may have to import 'android.annotation.SuppressLint;'

    So your java class file should appear like:

    package com.WarwickWestonWright.developers4u.app.CandidateArea;
    
    import android.annotation.SuppressLint;
    ... other imports
    
    @SuppressLint("IDOfYourLintProblem")
    public class SearchForJobsFragment extends Fragment {... rest of your class definition}
    

    Note that suppressing lint errors is not always the best IDEA you may be better off to change your code that caused the lint errors.

    Another problem that could potentially occur is if you have not set the environment variable for the Gradle HOME environment variable. This variable is named 'GRADLE_HOME' and should be set the the path of the gradle home directory, something like 'C:\gradle-1.12' Sometimes you may also want to set the environment variable for 'ANDROID_HOME' set this to 'YOUR-SDK-Root\sdk'

    After this is done return to the Gradle tasks window and double click the assembleAssembleRelease.

    If all is successful you should be able to go to the folder app\build\apk and find your deployable APK file.

    0 讨论(0)
  • 2020-11-22 14:08

    In my case, I was uploading the wrong apk, to another app's release.

    0 讨论(0)
  • 2020-11-22 14:09

    You can request passwords from the command line:

    ...
    
    signingConfigs {
      if (gradle.startParameter.taskNames.any {it.contains('Release') }) {
        release {
          storeFile file("your.keystore")
          storePassword new String(System.console().readPassword("\n\$ Enter keystore password: "))
          keyAlias "key-alias"
          keyPassword new String(System.console().readPassword("\n\$ Enter keys password: "))
        } 
      } else {
        //Here be dragons: unreachable else-branch forces Gradle to create
        //install...Release tasks.
        release {
          keyAlias 'dummy'
          keyPassword 'dummy'
          storeFile file('dummy')
          storePassword 'dummy'
        } 
      }
    }
    
    ...
    
    buildTypes {
      release {
    
        ...
    
        signingConfig signingConfigs.release
      }
    
      ...
    }
    
    ...
    

    The if-then-else block prevents requests for passwords when you're building a release. Although the else branch is unreachable, it tricks Gradle into creating an install...Release task.

    Backstory. As noted by https://stackoverflow.com/a/19130098/3664487, "Gradle scripts can prompt for user input using the System.console().readLine method." Unfortunately, Gradle will always request a password, even when you're building a debug release (cf. How to create a release signed apk file using Gradle?). Fortunately, this can be overcome, as I have shown above.

    0 讨论(0)
提交回复
热议问题