How to create a release signed apk file using Gradle?

前端 未结 30 1693
既然无缘
既然无缘 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条回答
  •  旧时难觅i
    2020-11-22 13:57

    Yet another approach to the same problem. As it is not recommended to store any kind of credential within the source code, we decided to set the passwords for the key store and key alias in a separate properties file as follows:

    key.store.password=[STORE PASSWORD]
    key.alias.password=[KEY PASSWORD]
    

    If you use git, you can create a text file called, for example, secure.properties. You should make sure to exclude it from your repository (if using git, adding it to the .gitignore file). Then, you would need to create a signing configuration, like some of the other answers indicate. The only difference is in how you would load the credentials:

    android {
        ...
        signingConfigs {
            ...
            release {
                storeFile file('[PATH TO]/your_keystore_file.jks')
                keyAlias "your_key_alias"
    
                File propsFile = file("[PATH TO]/secure.properties");
                if (propsFile.exists()) {
                    Properties props = new Properties();
                    props.load(new FileInputStream(propsFile))
                    storePassword props.getProperty('key.store.password')
                    keyPassword props.getProperty('key.alias.password')
                }
            }
            ...
        }
    
        buildTypes {
            ...
            release {
                signingConfig signingConfigs.release
                runProguard true
                proguardFile file('proguard-rules.txt')
            }
            ...
        }
    }
    

    Never forget to assign the signingConfig to the release build type manually (for some reason I sometimes assume it will be used automatically). Also, it is not mandatory to enable proguard, but it is recommendable.

    We like this approach better than using environment variables or requesting user input because it can be done from the IDE, by switching to the realease build type and running the app, rather than having to use the command line.

提交回复
热议问题