Sign APK without putting keystore info in build.gradle
The nice thing about Groovy is that you can freely mix Java code, and it's pretty easy to read in a key/value file using java.util.Properties
. Perhaps there's an even easier way using idiomatic Groovy, but Java is still pretty simple.
Create a keystore.properties
file (in this example, in the root directory of your project next to settings.gradle
, though you can put it wherever you like:
storePassword=...
keyPassword=...
keyAlias=...
storeFile=...
Add this to your build.gradle
:
allprojects {
afterEvaluate { project ->
def propsFile = rootProject.file('keystore.properties')
def configName = 'release'
if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
android.signingConfigs[configName].storeFile = file(props['storeFile'])
android.signingConfigs[configName].storePassword = props['storePassword']
android.signingConfigs[configName].keyAlias = props['keyAlias']
android.signingConfigs[configName].keyPassword = props['keyPassword']
}
}
}
How to create a release signed apk file using Gradle?
Easier way than previous answers:
Put this into ~/.gradle/gradle.properties
RELEASE_STORE_FILE={path to your keystore}
RELEASE_STORE_PASSWORD=*****
RELEASE_KEY_ALIAS=*****
RELEASE_KEY_PASSWORD=*****
Modify your app/build.gradle
, and add this inside the android {
code block:
...
signingConfigs {
release {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
// Optional, specify signing versions used
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
....
Then you can run gradle assembleRelease
Also see the reference for the signingConfigs
Gradle DSL
Sign APK without putting keystore info in build.gradle
The nice thing about Groovy is that you can freely mix Java code, and it's pretty easy to read in a key/value file using java.util.Properties
. Perhaps there's an even easier way using idiomatic Groovy, but Java is still pretty simple.
Create a keystore.properties
file (in this example, in the root directory of your project next to settings.gradle
, though you can put it wherever you like:
storePassword=...
keyPassword=...
keyAlias=...
storeFile=...
Add this to your build.gradle
:
allprojects {
afterEvaluate { project ->
def propsFile = rootProject.file('keystore.properties')
def configName = 'release'
if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
android.signingConfigs[configName].storeFile = file(props['storeFile'])
android.signingConfigs[configName].storePassword = props['storePassword']
android.signingConfigs[configName].keyAlias = props['keyAlias']
android.signingConfigs[configName].keyPassword = props['keyPassword']
}
}
}
Android Studio don't recognise signing configuration declared in a properties file
Make sure you have:
buildTypes {
debug {
signingConfig signingConfigs.debug
......
}
release {
signingConfig signingConfigs.release
...
}
}
in module.
Is it safe to write password in signing configuration?
I have been thinking about that recently and I've figured out better solution. You can put in gradle configuration information from where it should look for signing data. So If You are working with git You can ignore that file. Any new developer after taking the project and willing to sign apk will have to obtain mentioned file before building release.
Here is the example. In gradle file in signingConfigs tag You use Properties
which is reading keystore.config
containing signing information:
signingConfigs {
release {
Properties keystoreProps = new Properties()
keystoreProps.load(new FileInputStream(file('keystore.config')))
keyAlias keystoreProps['keyAlias']
keyPassword keystoreProps['keyPassword']
storePassword keystoreProps['storePassword']
storeFile file('keystore.jks')
}
}
Here is the keystore.config file:
keyAlias=MyAlias
keyPassword=MyPassword
storePassword=StorePassword
The above solution assumes that the keystore.jks
and keystore.config
are located in the app
folder but You are free to change the path.
Android Error Building Signed APK: keystore.jks not found for signing config 'externalOverride'
I found the solution. I misplaced the path to the keystore.jks
file.
Searched for the file on my computer used that path and everything worked great.
Related Topics
Android Asynctask Testing with Android Test Framework
Wrong Requestcode in Onactivityresult
How to Open Standard Google Map Application from My Application
How to Programmatically Clear Application Data
Add Views Below Toolbar in Coordinatorlayout
What Do Gc_For_Malloc, Gc_Explicit, and Other Gc_* Mean in Android Logcat
Custom Event Listener on Android App
Application Installation Failed in Android Studio
How to Add Multiple Widgets in the Same App
How to Create an Android Application in Android Studio That Uses the Google Maps API V2
How to Create Tests in Android Studio
Play Sound on Button Click Android
Creating Viewholders for Listviews with Different Item Layouts
Android SQLite: Update Statement
Android Adb Stop Application Command Like "Force-Stop" for Non Rooted Device