androidgradlebuildzipalignapksigner

Android - zipalign fails before apksigner


I'm currently in the process of upgrading from V1 signing (jarsigner) to V2 signing (apksigner). Jarsigner has been working for me no problem, however I'm running into issues with zipalign in my build.gradle before I can run apksigner.

On command line, I know there is a hack to zipalign twice, which works for me.

However, when I am running zipalign on Jenkins, I have been getting this error:

Process 'command '/Users/admin/Library/Android/sdk/build-tools/27.0.1/zipalign'' finished with non-zero exit value 1

What can I do to make zipalign work so I can use apksigner?


Solution

  • I was able to come to the solution with a try-catch. It's ugly, but it works.

    try {
        println "Zipaligning ${apkFile.name}"
        exec {
            commandLine zipalign, '-f', '4', apkFile.canonicalPath, tempAPK.canonicalPath
        }
    } catch (err) {
        // Zipalign twice if first zipalign fails
        exec {
            commandLine zipalign, '-f', '4', tempAPK.canonicalPath, signedAPK.canonicalPath
        }
    }