Search code examples
androidgradleandroid-gradle-pluginzipalign

Gradle task to execute android zipalign


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.


Solution

  • 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}"
    
    }