How to create a release signed apk file using Gradle?

前端 未结 30 1438
既然无缘
既然无缘 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:17

    Adding my way to do it in React-Native using react-native-config package.
    Create a .env file:

    RELEASE_STORE_PASSWORD=[YOUR_PASSWORD]
    RELEASE_KEY_PASSWORD=[YOUR_PASSWORD]
    

    note this should not be part of the version control.

    in your build.gradle:

    signingConfigs {
            debug {
                ...
            }
            release {
                storeFile file(RELEASE_STORE_FILE)
                storePassword project.env.get('RELEASE_STORE_PASSWORD')
                keyAlias RELEASE_KEY_ALIAS
                keyPassword project.env.get('RELEASE_KEY_PASSWORD')
            }
        }
    
    0 讨论(0)
  • 2020-11-22 14:18

    I had several issues that I put the following line in a wrong place:

    signingConfigs {
        release {
            // We can leave these in environment variables
            storeFile file("d:\\Fejlesztés\\******.keystore")
            keyAlias "mykey"
    
            // These two lines make gradle believe that the signingConfigs
            // section is complete. Without them, tasks like installRelease
            // will not be available!
            storePassword "*****"
            keyPassword "******"
        }
    }
    

    Make sure that you put the signingConfigs parts inside the android section:

    android
    {
        ....
        signingConfigs {
            release {
              ...
            }
        }
    }
    

    instead of

    android
    {
        ....
    }
    
    signingConfigs {
       release {
            ...
       }
    }
    

    It is easy to make this mistake.

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

    I managed to solve it adding this code, and building with gradle build:

    android {
        ...
        signingConfigs {
            release {
                storeFile file("release.keystore")
                storePassword "******"
                keyAlias "******"
                keyPassword "******"
            }
        }
        buildTypes {
            release {
                signingConfig signingConfigs.release
            }
        }
    }
    

    This generates a signed release apk file.

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

    You can also use -P command line option of gradle to help the signing. In your build.gradle, add singingConfigs like this:

    signingConfigs {
       release {
           storeFile file("path/to/your/keystore")
           storePassword RELEASE_STORE_PASSWORD
           keyAlias "your.key.alias"
           keyPassword RELEASE_KEY_PASSWORD
       }
    }
    

    Then call gradle build like this:

    gradle -PRELEASE_KEYSTORE_PASSWORD=******* -PRELEASE_KEY_PASSWORD=****** build
    

    You can use -P to set storeFile and keyAlias if you prefer.

    This is basically Destil's solution but with the command line options.

    For more details on gradle properties, check the gradle user guide.

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

    It is 2019 and I need to sign APK with V1 (jar signature) or V2 (full APK signature). I googled "generate signed apk gradle" and it brought me here. So I am adding my original solution here.

    signingConfigs {
        release {
            ...
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }
    

    My original question: How to use V1 (Jar signature) or V2 (Full APK signature) from build.gradle file

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

    In newer Android Studio, there is a GUI way which is very easy and it populates Gradle file as well.

    1. File -> Project Structure

    2. Module -> Choose the main module ('app' or other custom name)

    3. Signing tab -> Plus image to add new configuration

    4. Fill data on the right side

    5. OK and Gradle file is automatically created

    6. You will manually have to add a line signingConfig signingConfigs.NameOfYourConfig inside builtTypes{release{}}

    Images:

    enter image description here

    enter image description here

    Two important(!) notes:

    (EDIT 12/15)

    1. To create signed APK, you'd have to open Terminal tab of Android Studio (the bottom of the main interface) and issue a command ./gradlew assembleRelease

    2. If you forgot keyAlias (what happens often to me), you will have to initiate Build -> Generate Signed APK to start the process and see the name of the Alias key.

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