Search code examples
androidgradleandroid-gradle-pluginbuild.gradlesigned-apk

Generate signed apk without typing key


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?


Solution

  • 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
        }
    }