How to create a release signed apk file using Gradle?

前端 未结 30 1441
既然无缘
既然无缘 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 13:58

    i am work in Ubuntu14.04. vim ~/.bashrc and add export ANDROID_KEYSTORE= export ANDROID_KEYALIAS=

    and then in build.gradle set.

        final Console console = System.console();
    if (console != null) {
    
        // Building from console
        signingConfigs {
            release {
                storeFile file(System.getenv("KEYSTORE"))
                storePassword new String(System.console().readPassword("\n\$ Enter keystore password: "))
                keyAlias System.getenv("KEY_ALIAS")
                keyPassword new String(System.console().readPassword("\n\$ Enter key password: "))
            }
        }
    
    } else {
    
        // Building from IDE's "Run" button
        signingConfigs {
            release {
    
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:00

    Like @Destil said but allow others who don't have the key to build: 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 build.gradle like this:

    ...    
    if(project.hasProperty("RELEASE_STORE_FILE")) {
        signingConfigs {    
           release {
               storeFile file(RELEASE_STORE_FILE)
               storePassword RELEASE_STORE_PASSWORD
               keyAlias RELEASE_KEY_ALIAS
               keyPassword RELEASE_KEY_PASSWORD
           }
        }
    }
    
    buildTypes {
        if(project.hasProperty("RELEASE_STORE_FILE")) {
            release {
                signingConfig signingConfigs.release
            }
        }
    }
    ....
    

    Then you can run gradle assembleRelease OR gradle build

    0 讨论(0)
  • 2020-11-22 14:03

    If you build apk via command line like me then you can provide signing configuration as arguments.

    Add this to your build.gradle

    def getStore = { ->
        def result = project.hasProperty('storeFile') ? storeFile : "null"
        return result
    }
    
    def getStorePassword = { ->
        def result = project.hasProperty('storePassword') ? storePassword : ""
        return result
    }
    
    def getKeyAlias = { ->
        def result = project.hasProperty('keyAlias') ? keyAlias : ""
        return result
    }
    
    def getKeyPassword = { ->
        def result = project.hasProperty('keyPassword') ? keyPassword : ""
        return result
    }
    

    Make your signingConfigs like this

    signingConfigs {
        release {
            storeFile file(getStore())
            storePassword getStorePassword()
            keyAlias getKeyAlias()
            keyPassword getKeyPassword()
        }
    }
    

    Then you execute gradlew like this

    ./gradlew assembleRelease -PstoreFile="keystore.jks" -PstorePassword="password" -PkeyAlias="alias" -PkeyPassword="password"
    
    0 讨论(0)
  • 2020-11-22 14:04

    This is a reply to user672009 and addition to sdqali's post (his code will crash on building debug version by IDE's "Run" button):

    You can use the following code:

    final Console console = System.console();
    if (console != null) {
    
        // Building from console 
        signingConfigs {
            release {
                storeFile file(console.readLine("Enter keystore path: "))
                storePassword console.readLine("Enter keystore password: ")
                keyAlias console.readLine("Enter alias key: ")
                keyPassword console.readLine("Enter key password: ")
            }
        }
    
    } else {
    
        // Building from IDE's "Run" button
        signingConfigs {
            release {
    
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:04

    For Groovy (build.gradle)

    You should not put your signing credentials directly in the build.gradle file. Instead the credentials should come from a file not under version control.

    Put a file signing.properties where the module specific build.gradle is found. Don't forget to add it to your .gitignore file!

    signing.properties

    storeFilePath=/home/willi/example.keystore
    storePassword=secret
    keyPassword=secret
    keyAlias=myReleaseSigningKey
    

    build.gradle

    android {
        // ...
        signingConfigs{
            release {
                def props = new Properties()
    
                def fileInputStream = new FileInputStream(file('../signing.properties'))
                props.load(fileInputStream)
                fileInputStream.close()
    
                storeFile = file(props['storeFilePath'])
                storePassword = props['storePassword']
                keyAlias = props['keyAlias']
                keyPassword = props['keyPassword']
            }
        }
    
        buildTypes {
            release {
                signingConfig signingConfigs.release
                // ...
            }
        }
    }
    
    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
提交回复
热议问题