How to create a release signed apk file using Gradle?

前端 未结 30 1811
既然无缘
既然无缘 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条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 14:06

    Note that @sdqali's script will (at least when using Gradle 1.6) ask for the password anytime you invoke any gradle task. Since you only need it when doing gradle assembleRelease (or similar), you could use the following trick:

    android {
        ...
        signingConfigs {
            release {
                // We can leave these in environment variables
                storeFile file(System.getenv("KEYSTORE"))
                keyAlias System.getenv("KEY_ALIAS")
    
                // These two lines make gradle believe that the signingConfigs
                // section is complete. Without them, tasks like installRelease
                // will not be available!
                storePassword "notYourRealPassword"
                keyPassword "notYourRealPassword"
            }
        }
        ...
    }
    
    task askForPasswords << {
        // Must create String because System.readPassword() returns char[]
        // (and assigning that below fails silently)
        def storePw = new String(System.console().readPassword("Keystore password: "))
        def keyPw  = new String(System.console().readPassword("Key password: "))
    
        android.signingConfigs.release.storePassword = storePw
        android.signingConfigs.release.keyPassword = keyPw
    }
    
    tasks.whenTaskAdded { theTask -> 
        if (theTask.name.equals("packageRelease")) {
            theTask.dependsOn "askForPasswords"
        }
    }
    

    Note that I also had to add the following (under android) to make it work:

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
    

提交回复
热议问题