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
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')
}
}
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.
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.
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.
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
In newer Android Studio, there is a GUI way which is very easy and it populates Gradle file as well.
File -> Project Structure
Module ->
Choose the main module ('app' or other custom name)
Signing
tab -> Plus image to add new configuration
Fill data on the right side
OK and Gradle file is automatically created
You will manually have to add a line signingConfig signingConfigs.NameOfYourConfig
inside builtTypes{release{}}
Images:
Two important(!) notes:
(EDIT 12/15)
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
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.