I would like to build a gradle Exec task that would run android's zipalign on my signed apk file, and then validates the the alignment.
Asked
Active
Viewed 1,051 times
1 Answers
1
Variables:
- ANDROID_HOME - path to android SDK
- ZIPALIGN_PATH - path to zipalign executable, relative to the
- ANDROID_HOME buildDir - gradle's build directory
- OUTPUT_APK_PATH - the directory of created apk files
- APK_FILE_TO_ALIGN - the apk that you want to perform the zipalign on (should be signed)
- APK_FILE_NAME - the name of the file after zipalign
zipalign task:
task zipAlign(type: Exec) {
executable "${ANDROID_HOME}${ZIPALIGN_PATH}"
args "-f", "-v", "4", "${buildDir}${OUTPUT_APK_PATH}${APK_FILE_TO_ALIGN}", "${buildDir}${OUTPUT_APK_PATH}${APK_FILE_NAME}"
}
zipalign verification task (note that this task depends on the zipalign task):
task verifyZipAlign(type: Exec, dependsOn: 'zipAlign') {
executable "${ANDROID_HOME}${ZIPALIGN_PATH}"
args "-c", "-v", "4", "${buildDir}${OUTPUT_APK_PATH}${APK_FILE_NAME}"
}

Tidhar Klein Orbach
- 2,896
- 2
- 30
- 47
-
After gradle version 2.2.0 it only produces zip aligned APKs, so this is completely unnecessary: https://issuetracker.google.com/issues/37103802 – Benproductions1 Aug 09 '17 at 03:58
-
Not really, if you make changes to the APK you will need to zip align again – James Goodwin May 14 '18 at 16:19
-
Zipalign does verification automatically. There's a nice comment in source code "Trust, but verify". – Eugen Pechanec Aug 25 '19 at 08:11