First of all here goes my signinConfigs:
signingConfigs {
development {
storeFile file("mykey.jks") //Path to the keystore file
keyAlias "mykey"
storePassword "mykeydev"
keyPassword "mykeydev"
}
production {
storeFile file("mykey.jks") //Path to the keystore file
keyAlias "mykey"
storePassword "mykeyprod"
keyPassword "mykeyprod"
}
}
And now my flavors:
productFlavors {
development{
applicationId "br.com.myapp.dev"
signingConfig signingConfigs.development
resValue "string", "app_name", "DEV"
}
production {
applicationId "br.com.myapp"
signingConfig signingConfigs.production
resValue "string", "app_name", "PROD"
}
}
I have under by buildTypes this:
buildTypes {
release {
minifyEnabled false
productFlavors.production.signingConfig signingConfigs.production
productFlavors.development.signingConfig signingConfigs.development
}
}
And here goes my question, Why am I asked for keyPassword and storePassword every time i want to generate a new signed apk file, if all keys and stuff are inside my .gradle file?
When you build from the "Build -> Generate Signed apk" you'll need to enter Android's studio password once in a while, but it saves the passwords encrypted for you. This is the safest way to go.
But since you need an automated way, you can use the "Terminal" view, and type: ./gradlew assembleRelease. (If you're in a windows machine I think it's gradlew assembleRelease).
In any case, it's not advisable to write your password inside the build.gradle file:
android {
signingConfigs {
release {
storeFile file('/your/keystore/location/key')
keyAlias 'your_alias'
keyPasswordString ps = System.getenv("ps")
storePasswordif System.getenv("ps"ps == null) {
throw new GradleException('missing ps env variable')
}
keyPassword ps
storePassword ps
}
}
If you have provided keystore path and credentials in gradle. Then for generating signed apks follow this steps.
Go to Build Variants
Select build variants as release
Click the play/run button
Apk will be published in Project/build/outputs/apk folder
Related
Background
I think there is an issue on Google login that is related to the fact we use the same key configuration for 2 different flavors of an app we work on. The reason is that both seem to have the same SHA1 on debug and release.
The issue exists only on release version of the second flavor. On build&debug of the first flavor, and on debug of the second, it works fine.
The problem
I should probably generate a new key configuration while using the same release-keystore (generate using the existing one), but I'm not sure how to set it up on the gradle file.
Suppose the 2 package-names are "com.free" and "com.paid" (not real names, just for here to simplify the question).
This is what I have now, simplified and without the real values
defaultConfig {
applicationId "com.free"
...
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword "storePassword1"
keyAlias "keyAlias1"
keyPassword "keyPassword1"
}
release {
storeFile file('release.keystore')
storePassword "storePassword2"
keyAlias "keyAlias2"
keyPassword "keyPassword2"
}
}
buildTypes {
release {
signingConfig signingConfigs.release
...
}
debug {
...
}
}
flavorDimensions.add("default")
productFlavors {
free {
dimension "default"
applicationId "com.free"
...
}
paid {
dimension "default"
applicationId "com.paid"
...
}
}
namespace 'com.free'
So this generates the 4 build-variants in the "Build Variants" window of Android Studio:
freeDebug
freeRelease
paidDebug
paidRelease
I want to stay with these, yet for "paid" ones have a different key-configuration as it's using the same one of "free" ones.
What I've found and tried
I've found the next questions and tutorials about this topic:
Different keyStore for different product flavors in Gradle
How to sign Multiple Flavor APK's with a Single Keystore
https://medium.com/#chauyan/how-to-use-gradle-on-multi-keystore-flavors-project-297ec083150b
https://blog.tunebrains.com/2015/10/02/gradle-multi-flavors-signing.html
https://developer.android.com/studio/publish/app-signing
So, what I tried is to split the "release" in the "signingConfigs" (no need for the debug, as this one works fine for debug-free combination), remove the "signingConfig" from "buildTypes"->"release", and have 4 productFlavors instead of 2:
signingConfigs {
//unchanged:
debug {
storeFile file('debug.keystore')
storePassword "storePassword1"
keyAlias "keyAlias1"
keyPassword "keyPassword1"
}
//using new keystore file, split for 2 different flavors, and have new keyAlias and keyPassword for "paid" :
releaseFree {
storeFile file('new_release.keystore')
storePassword "storePassword2"
keyAlias "keyAlias2"
keyPassword "keyPassword2"
}
releasePaid {
storeFile file('new_release.keystore')
storePassword "storePassword2"
keyAlias "keyAlias3"
keyPassword "keyPassword3"
}
}
buildTypes {
release {
//commented this as it can't be used anymore (split and not shared)
//signingConfig signingConfigs.release
...
}
debug {
...
}
}
flavorDimensions.add("default")
productFlavors {
//split to 4 : free-debug, paid-debug, free-release, paid-release
freeDebug {
dimension "default"
applicationId "com.free"
//identical debug key configuration should work fine for both
signingConfig signingConfigs.debug
...
}
paidDebug {
dimension "default"
applicationId "com.paid"
//identical debug key configuration should work fine for both
signingConfig signingConfigs.debug
...
}
freeRelease {
dimension "default"
applicationId "com.free"
signingConfig signingConfigs.releaseFree
...
}
paidDebug {
dimension "default"
applicationId "com.paid"
signingConfig signingConfigs.releasePaid
...
}
}
The IDE accepts these changes, but instead of the planned 4 items in the "Build Variants" window, I see 8:
freeDebugDebug
freeDebugRelease
freeReleaseDebug
freeReleaseRelease
paidDebugDebug
paidDebugRelease
paidReleaseDebug
paidReleaseRelease
Pretty sure what happened here is that for each flavor, it generated debug&release, and as I have defined 4 flavors, it's 4*2=8 ...
The questions
What have I done wrong here? How can I have 4 items as planned and as existed originally ?
Maybe possible to set a buildType for each flavor?
Or maybe I need to set 2 dimension values, one for "free" and one for "paid" ?
Are the settings of the signingConfigs items seem fine? For each different file, it uses the same storePassword value, and for each flavor, it should use a different keyAlias and keyPassword . Right?
You don't need to create 4 product flavors, you can use build type to differentiate between debug and release build and set signing config accordingly.
productFlavors {
free {
applicationId "com.free"
}
paid {
applicationId "com.paid"
}
}
Use build type for release/debug to set your
signing config.
buildTypes {
release {
productFlavors.free.signingConfig signingConfigs.releaseFree
productFlavors.paid.signingConfig signingConfigs.releasePaid
...
}
debug{
productFlavors.free.signingConfig signingConfigs.debug
productFlavors.paid.signingConfig signingConfigs.debug
}
}
Note: buildTypes block should be placed after productFlavors block
I have source code of android app with gradle support. I want to generate Apk online through my server.
anyone guide me the commands for cmd or terminal by which, I can generate debug and signed apk using my source code.
For generating debug and signed apk file from android project.you should add signingConfigs into your app moulde's build.gradle file as follow:
signingConfigs {
...
release {
...
keyAlias YOUR-KEY-ALIAS
keyPassword YOUR-KEY-PASSWORD
storeFile YOUR-KEYSTORE-FILE-PATH//here is your keystore file path
storePassword KEY-STORE-PASSWORD
...
}
debug{//debug keystore
...
keyAlias "androiddebugkey"
keyPassword "android"
storeFile rootProject.file("debug.keystore")//here is your keystore file path
storePassword "android"
...
}
...
}
in your buildTypes block add:
buildTypes {
...
debug{
...
signingConfig signingConfigs.debug
...
}
release {
...
signingConfig signingConfigs.release
...
}
...
}
finally,your can input command in your project's directory:gradlew assemble,that will output your porject's apk file in some derectory(YOUR-PROJECT-DIRECTORY/app/build/outputs/apk)
My build gradle:
signingConfigs {
general {
keyAlias 'key'
keyPassword '123abc'
storeFile file('../store.jks')
storePassword '123abc'
}
lomza {
keyAlias 'key2'
keyPassword '123abc'
storeFile file('../store.jks')
storePassword '123abc'
}
}
So, there are 2 signing configs, each one is created and intended to use to its own build flavor. It works if I choose flavor and hit Shift+F10 (or push green arrow) - chosen build flavor will generate apk signed with correct key, as configured in build.gradle:
productFlavors {
general {
applicationId <id1>
signingConfig signingConfigs.general
}
lomza {
applicationId <id2>
signingConfig signingConfigs.key2
}
}
However, if I choose Build->Generate signed apk, I am forced to set only single keystore path. If I choose to build multiple flavors, all of them will be signed with the same key, chosen in first step in "Generate signed apk" window.
How to force "Build signed apk" to use respective signing config for each flavor to build, defined in build.gradle?
Prior to Android Studio 0.4, I was able to set a custom debug keystore via
File -> Project Structure -> Facets -> Compiler Tab
This option has gone at least with Android Studio 0.4.2. Where can I set the custom keystore for being able to share it over different PCs, e.g. via a VCS?
This can be solved by adding signingConfigs to the build.gradle configuration.
android {
// ...
signingConfigs {
debug {
storeFile file('../debug.keystore')
}
/*
release {
storeFile file('release.keystore')
storePassword "mystorepassword"
keyAlias "mykeyalias"
keyPassword "mykeypassword"
}
*/
}
You can do the same using signingConfigs in build.gradle file and putting certificates in project directory
Step 1. Create a directory inside you module like
--YourProject
--your_module
--KeystoreCertificates
--myCertificates
--other_certificates
build.gradle file inside your_module dir
android {
signingConfigs {
myCustomDebug {
storeFile file("KeystoreCertificates/myCertificates")
storePassword "certi_password"
keyAlias "certi_alias"
keyPassword "alias_password"
}
}
buildTypes {
debug {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.myCustomDebug
}
}
I have a big issue when I come to sign my application: I have set the signing configuration in accordance with the doc:
signingConfigs {
release {
storeFile file("lomapnew.keystore")
storePassword "myPassword"
keyAlias "myAlias"
keyPassword "Something...."
}
}
But I still get this error message: "The signing configuration should be specified in Gradle build scripts"
I'm going to go out on a limb and guess that you haven't set the signing configuration for the release build type. The debug build type is automatic, so it's not obvious that this is a necessary step for all other build types, including release.
You can apply the signing config like so:
android {
signingConfigs {
// It's not necessary to specify, but I like to keep the debug keystore
// in SCM so all our debug builds (on all workstations) use the same
// key for convenience
debug {
storeFile file("debug.keystore")
}
release {
storeFile file("release.keystore")
storePassword "myPassword"
keyAlias "myAlias"
keyPassword "Something...."
}
}
buildTypes {
/* This one happens automatically
debug {
signingConfig signingConfigs.debug
}
*/
release {
signingConfig signingConfigs.release
}
}
}
I like to keep passwords out of my build file. Hence I create a properties file that I load with
def keystorePropertiesFile = rootProject.file("./local.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
Then I define signingConfigs like so:
signingConfigs {
releaseSigning {
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['keystore.live.storepassword']
keyAlias = keystoreProperties['keystore.live.keyalias']
keyPassword = keystoreProperties['keystore.live.keypassword']
}
debugSigning {
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['keystore.debug.storepassword']
keyAlias = keystoreProperties['keystore.debug.keyalias']
keyPassword = keystoreProperties['keystore.debug.keypassword']
}
}
This doesn't work well with the menu option "create Signed apk" so I create flavors:
productFlavors {
mydebug {
signingConfig signingConfigs.debugSigning
}
myrelease {
signingConfig signingConfigs.releaseSigning
}
}
and now the signingconfigs work with the run button on the toolbar. For a default keystore the local.properties looks like
ndk.dir=/opt/sdk/ndk-bundle
sdk.dir=/opt/sdk
storeFile=/home/christine/.android/debug.keystore
keystore.debug.storepasswd=android
keystore.debug.keyalias=androiddebugkey
keystore.debug.keypassword=android
keystore.live.storepasswd=android
keystore.live.keyalias=androiddebugkey
keystore.livetest.keypassword=android
In your Jenkins build script, you need to create a symbolic link from local.properties to where the properties file is on your build server.
Answer is already given but i would like to highlight the other ways also,
We can specify the information manually like below where we have to specify full path to our keystore location like this
signingConfigs {
release {
storeFile file('O:/Android/Projects/yourKeyStore.jks')
storePassword "qwerty"
keyAlias "yourProjectKeyAlias"
keyPassword "ProjectKeyPassword"
}
}
This can also be specified in Signing Report if you go in
File-->Project Structure
Choose your project app module and select the signing report where you can fill up the information and it will automatically add previous release information in the gradle file.
Finally you just have to add
signingConfig android.signingConfigs.release
in the buildTypes {...} section. That will complete the signing procedure.