从命令行构建APK后,使用zopfli重新压缩运行zipalign以使其更小

时间:2016-02-23 06:09:05

标签: android android-gradle zipalign zopfli

正如Google Developers' article中所述,现在可以通过运行a = validate('something'); (validate - is the validation function, but you can have another name of function) if (!a) { return false; } else { --your form submit here-- }使用zopfli重新压缩APK文件。就我而言,在5.1 MB APK文件中观察到减少了200 KB。

通常我会使用自定义shell脚本运行zipalign -z来构建APK。

我想在上面的命令之后运行gradle assembleRelease。但是,zipalign -z <the final apk>位于zipalign目录中,除非从build-tools/<build tools version>文件中提取<build tools version>并手动构建路径,否则无法找到它。 / p>

是否可以使用build.gradle命令运行zipalign,该命令会在正确的gradle目录上自动运行zipalign而无需重新构建路径?

例如build-tools

等命令

1 个答案:

答案 0 :(得分:2)

您链接到的article已使用gradle任务进行更新,以便将zopfli压缩添加到assembleRelease任务的末尾。

//add zopfli to variants with release build type
android.applicationVariants.all { variant ->
  if (variant.buildType.name == 'release') {
    variant.outputs.each { output ->
        output.assemble.doLast {
            println "Zopflifying... it might take a while"
            exec {
                commandLine output.zipAlign.zipAlignExe,'-f','-z', '4', output.outputFile.absolutePath , output.outputFile.absolutePath.replaceAll('\\.apk$', '-zopfli.apk')
            }
        }
    }
  }
}