Android Studio: 如何使用 Gradle 生成带签名的 APK?

我在谷歌上搜索了,但是找不到答案。

这是我第一次使用 Gradle 系统,现在我正在生成一个签名的 APK 上传到 Google Play (项目是从 eclipse 导入的)。

现在,我已经读了 给你部分,你应该把 signingConfigs加到你的 build.gradle

我已经添加了这些代码行,现在我看到您需要运行 ./gradlew assembleRelease,但是在我的 CMD 返回中运行它

“ gradle”不被认为是内部或外部命令, 可操作的程序或批处理文件。

我还试图右键单击 build.gradle 并运行它,说它是成功的,但是一旦我查看 build/apk 文件夹,只有一个名为 app-debug-unaligned.apk的文件。

那么,如何使用 Gradle 系统生成签名的 APK 呢?

102389 次浏览

build menu > generate signed apk

There are three ways to generate your build as per the buildType. (In your case, it's release but it can be named anything you want.)

  1. Go to Gradle Task in right panel of Android Studio and search for assembleRelease or assemble(#your_defined_buildtype) under Module Tasks

  2. Go to Build Variant in Left Panel and select the build from drop down

  3. Go to project root directory in File Explore and open cmd/terminal and run:

    Linux: ./gradlew assembleRelease or assemble(#your_defined_buildtype)

    Windows: gradlew assembleRelease or assemble(#your_defined_buildtype)

If you want to do a release build (only), you can use Build > Generate Signed apk. For other build types, only the above three options are available.

You can find the generated APK in your module/build directory having the build type name in it.

I think this can help you https://www.timroes.de/2013/09/22/handling-signing-configs-with-gradle/ then just select the Release from the Build Variants

It is possible to take any existing Android Studio gradle project and build/sign it from the command line without editing any files. This makes it very nice for storing your project in version control while keeping your keys and passwords separate:

./gradlew assembleRelease -Pandroid.injected.signing.store.file=$KEYFILE -Pandroid.injected.signing.store.password=$STORE_PASSWORD -Pandroid.injected.signing.key.alias=$KEY_ALIAS -Pandroid.injected.signing.key.password=$KEY_PASSWORD

You can use this code

android {
...
signingConfigs {
release {
storeFile file("../your_key_store_file.jks")
storePassword "some_password"
keyAlias "alias_name"
keyPassword "key_password"
}
}


buildTypes {


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

then from your terminal run

./gradlew assembleRelease

you will get the apk at

your-android-app/build/outputs/apk/your-android-app-release.apk

This is for Kotlin DSL (build.gradle.kts).
You could either define your properties in local.properties file in the project root directory or define them as environment variables (which is especially useful for CIs like GitHub Actions).

// See https://stackoverflow.com/q/60474010
fun getLocalProperty(key: String) = gradleLocalProperties(rootDir).getProperty(key)


fun String?.toFile() = file(this!!)


// Could also use System.getenv("VARIABLE_NAME") to get each variable individually
val environment: Map<String, String> = System.getenv()


android {
signingConfigs {
create("MyAppSigningConfig") {
keyAlias = getLocalProperty("signing.keyAlias") ?: environment["SIGNING_KEY_ALIAS"] ?: error("Error!")
storeFile = (getLocalProperty("signing.storeFile") ?: environment["SIGNING_STORE_FILE"] ?: error("Error!")).toFile()
keyPassword = getLocalProperty("signing.keyPassword") ?: environment["SIGNING_KEY_PASSWORD"] ?: error("Error!")
storePassword = getLocalProperty("signing.storePassword") ?: environment["SIGNING_STORE_PASSWORD"] ?: error("Error!")
enableV1Signing = true
enableV2Signing = true
}
}


buildTypes {
getByName("release") { // OR simply release { in newer versions of Android Gradle Plugin (AGP)
signingConfig = signingConfigs["MyAppSigningConfig"]
// ...
}
}
}

myProject/local.properties file:

signing.keyAlias=foo
signing.keyPassword=bar
# also called keystore
signing.storePassword=abcdefgh
signing.storeFile=C\:\\my-files\\my-keystore.jks

NOTE: Do NOT add your local.properties file to your version control system (like Git), as it exposes your secret information like passwords etc. to the public (if it's a public repository).

Generate your APK with either of the 3 ways that this answer mentioned.

If you live in certain countries, be sure to use a VPN.

step1: run this command in the command-line:

keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

it will ask you for some information such as password, name,... and enter them.

step2: create a file name key.properties in your android folder. write these lines in the created file

storePassword=<password from previous step>
keyPassword=<password from previous step>
keyAlias=key
storeFile=<location of the key store file, such as ~/key.jks>

keep the key.properties file private, always keep a backup of the key.properties file and never publish publicly.

step3: Replace the following lines in app-level Gradle

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}

step4:

keytool -list -v -keystore ~/key.jks -alias key -storepass <password> -keypass <password>

step5: I recommend building APK, using android studio. Build > Generate Signed Bundle/APK...