天天看點

如何使用Gradle建立釋出簽名的apk檔案?

本文翻譯自:How to create a release signed apk file using Gradle?

I would like to have my Gradle build to create a release signed apk file using Gradle.

我想使用Gradle建構Gradle版本以建立釋出簽名的apk檔案。

I'm not sure if the code is correct or if I'm missing a parameter when doing

gradle build

?

我不确定代碼是否正确或在

gradle build

時是否缺少參數?

This is some of the code in my gradle file:

這是我的gradle檔案中的一些代碼:
android {
    ...
    signingConfigs {
          release {
              storeFile file("release.keystore")
              storePassword "******"
              keyAlias "******"
              keyPassword "******"
         }
     }
}
           

The gradle build finishes SUCCESSFUL, and in my

build/apk

folder I only see the

...-release-unsigned.apk

and

...-debug-unaligned.apk

files.

gradle建構成功完成,在我的

build/apk

檔案夾中,我僅看到

...-release-unsigned.apk

...-debug-unaligned.apk

檔案。

Any suggestions on how to solve this?

關于如何解決這個問題有什麼建議嗎?

#1樓

參考:https://stackoom.com/question/1Eu8g/如何使用Gradle建立釋出簽名的apk檔案

#2樓

I managed to solve it adding this code, and building with

gradle build

:

我設法通過添加此代碼來解決它,并使用

gradle build

android {
    ...
    signingConfigs {
        release {
            storeFile file("release.keystore")
            storePassword "******"
            keyAlias "******"
            keyPassword "******"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}
           

This generates a signed release apk file.

這将生成一個簽名的發行版apk檔案。

#3樓

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):

這是對user672009的答複, 也是對sdqali文章的補充(他的代碼将在IDE的“運作”按鈕生成調試版本時崩潰):

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 {

        }
    }

}
           

#4樓

Note that @sdqali's script will (at least when using Gradle 1.6) ask for the password anytime you invoke any gradle task.

請注意,@ sdqali的腳本将(至少在使用Gradle 1.6時)在您調用任何 gradle任務時要求輸入密碼。

Since you only need it when doing

gradle assembleRelease

(or similar), you could use the following trick:

由于僅在進行

gradle assembleRelease

(或類似操作)時才需要它,是以可以使用以下技巧:
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:

請注意,我還必須添加以下内容(在android下)以使其工作:
buildTypes {
    release {
        signingConfig signingConfigs.release
    }
}
           

#5樓

If you want to avoid hardcoding your keystore & password in build.gradle , you can use a properties file as explained here: HANDLING SIGNING CONFIGS WITH GRADLE

如果要避免在build.gradle中對您的密鑰庫和密碼進行寫死,則可以按照以下說明使用屬性檔案: 使用GRUNDLE處理簽名配置

Basically:

基本上:

1) create a myproject.properties file at /home/[username]/.signing with such contents:

1)在/ home / [使用者名] /。signing中建立一個myproject.properties檔案,其内容如下:
keystore=[path to]\release.keystore
keystore.password=*********
keyAlias=***********
keyPassword=********
           

2) create a gradle.properties file (perhaps at the root of your project directory) with the contents:

2)使用内容建立gradle.properties檔案(也許在項目目錄的根目錄):
MyProject.properties=/home/[username]/.signing/myproject.properties
           

3) refer to it in your build.gradle like this:

3)像這樣在build.gradle中引用它:
if(project.hasProperty("MyProject.properties")
        && new File(project.property("MyProject.properties")).exists()) {

    Properties props = new Properties()
    props.load(new FileInputStream(file(project.property("MyProject.properties"))))

    signingConfigs {
        release {
            storeFile file(props['keystore'])
            storePassword props['keystore.password']
            keyAlias props['keyAlias']
            keyPassword props['keyPassword']
        }
    }
}
           

#6樓

Easier way than previous answers:

比以前的答案更簡單的方法:

Put this into

~/.gradle/gradle.properties

将其放入

~/.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:

像這樣修改

build.gradle

...    
signingConfigs {

   release {
       storeFile file(RELEASE_STORE_FILE)
       storePassword RELEASE_STORE_PASSWORD
       keyAlias RELEASE_KEY_ALIAS
       keyPassword RELEASE_KEY_PASSWORD
   }
}

buildTypes {
        release {
            signingConfig signingConfigs.release
        }
}
....
           

Then you can run

gradle assembleRelease

然後您可以運作

gradle assembleRelease