【问题标题】:Android Studio 2.3 is generating unaligned signed APK's instead of zipaligned?Android Studio 2.3 正在生成未对齐的签名 APK 而不是 zipaligned?
【发布时间】:2017-08-29 22:40:34
【问题描述】:

我最近升级到了 Android Studio 2.3,现在我将使用 Build / Generate signed APK... 为我现有的一个应用程序生成一个签名的 APK,就像我一直做的那样。之前我总是得到一个名为MyApp-1.0.apk 的文件(其中1.0 是版本名称),但现在我得到了MyApp-1.0-unaligned.apk

我注意到有一些新选项可供选择 V1 (Jar signature) 和/或 V2 (Full APK Signature。我都选择了recommended in the documentation。但是文档确实这样说

注意:如果您使用 APK 签名方案 v2 对您的应用进行签名并对该应用进行进一步更改,则该应用的签名将失效。因此,请在使用 APK 签名方案 v2 对您的应用进行签名之前使用 zipalign 等工具,而不是之后。

在我的build.gradle 我有

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

我看到一些人在使用 Android gradle 构建工具的 alpha 版本时遇到了类似的问题,但我使用的是2.3.0

classpath 'com.android.tools.build:gradle:2.3.0'

那么,我如何在 APK 生成过程中对我的 APK 进行 zipalign,然后再对它们进行签名?

【问题讨论】:

  • 你能输出“zipalign -c -v 4 ”的结果吗?
  • 我虽然自 Android 2.2.0 以来它不会产生未对齐的版本。 code.google.com/p/android/issues/detail?id=223551
  • @ Isuru zipalign -c -v 4 <path-to-apk> 返回一长串行,最后在底部是 Verification succesful [原文如此]。我认为文档说您不能压缩已使用 V2 签名的应用程序?顺便说一句,我正在使用构建工具 25.0.2。多年来一直在 Android Studio 中使用菜单命令进行签名,没有任何问题,但我最近更新了 Android Studio、构建工具、SDK,一切......
  • 我遇到了同样的问题,我确实创建了一个 C# 程序来编译未对齐的 apk 以发布版本。但我不知道为什么在新版本的 Android Studio 中会出现这种情况。
  • @Isuru 问题解决了,我的错,看我的答案...

标签: android android-studio apk android-studio-2.3 zipalign


【解决方案1】:

问题是由外部 gradle 脚本管理生成的 APK 的文件名引起的。我完全忘记了那个脚本,现在它开始无法检查 APK 是否已压缩,因为 Google 引入了 v2 签名。

我的build.gradle 中包含这样的脚本

apply from: '../../export_signed_apk.gradle'

脚本本身看起来像这样

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)
    }
}

似乎output.zipAlign 在应用 V2 签名后失败了,所以即使签名的 APK 确实是压缩对齐的,它也会返回 myApp-1.0-unaligned

我只是删除了 IF 语句,只是保留

newApkName = "${appName}-${variant.versionName}.apk"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    相关资源
    最近更新 更多