Android Studio 2.3生成未签名的已签名APK而不是zipaligned?

时间:2017-04-04 15:57:39

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

我最近升级到了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 Signature Scheme 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生成过程设为zipalign我的APK?

1 个答案:

答案 0 :(得分:1)

问题是由外部gradle脚本管理生成的APK的文件名引起的。我完全忘记了那个剧本,现在它已经开始未通过支票查看APK是否已经拉链签名,因为谷歌推出了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确实是zipaligned,它也会返回myApp-1.0-unaligned

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

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