本文翻譯自: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