I recently upgraded to Android Studio 2.3 and now I was going to generate a signed APK for one of my existing apps using Build / Generate signed APK...
just as I've always done. Before I have always gotten a file called MyApp-1.0.apk
(where 1.0
is the version name), but now I get MyApp-1.0-unaligned.apk
.
I noticed there are some new options to choose V1 (Jar signature)
and/or V2 (Full APK Signature
. I selected both, as recommended in the documentation. The documentation does however say this
Caution: If you sign your app using APK Signature Scheme v2 and make further changes to the app, the app's signature is invalidated. For this reason, use tools such as zipalign before signing your app using APK Signature Scheme v2, not after.
In my build.gradle
I have
buildTypes {
debug{
// Enable/disable ProGuard for debug build
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
zipAlignEnabled true
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
zipAlignEnabled true
}
}
I've seen some people experiencing similar problems with alpha releases of the Android gradle build tool, but I'm using 2.3.0
:
classpath 'com.android.tools.build:gradle:2.3.0'
So how do I make the APK-generation process zipalign my APK's before signing them?
The problem was caused by having an external gradle script manage the filename of generated APK's. I had totally forgot about that script, and now it had started failing a check to see if the APK is zipaligned, since Google introduced the v2 signing.
I had the script included in my build.gradle
like this
apply from: '../../export_signed_apk.gradle'
And the script itself looked like this
android.applicationVariants.all {
variant -> def appName
//Check if an applicationName property is supplied; if not use the name of the parent project.
if (project.hasProperty("applicationName")) {
appName = applicationName
} else {
appName = parent.name
}
variant.outputs.each {
output -> def newApkName
//If there's no ZipAlign task it means that our artifact will be unaligned and we need to mark it as such.
if (output.zipAlign) {
newApkName = "${appName}-${variant.versionName}.apk"
} else {
newApkName = "${appName}-${variant.versionName}-unaligned.apk"
}
output.outputFile = new File(output.outputFile.parent, newApkName)
}
}
It seems that output.zipAlign
fails since applying the V2 signing, so it would return myApp-1.0-unaligned
even when the signed APK was indeed zipaligned.
I simply removed the IF statement, and am just keeping
newApkName = "${appName}-${variant.versionName}.apk"