2017-04-04 45 views
2

我最近升级到Android 2.3工作室,现在我要生成一个签名的APK使用Build/Generate signed APK...就像我一直在做我现有的应用程序之一。之前,我一直得到一个名为MyApp-1.0.apk(其中1.0是版本的名称)的文件,但现在我得到MyApp-1.0-unaligned.apkAndroid Studio 2.3正在生成未对齐的签名APK而不是zipaligned?

我注意到有一些新的选项,选择V1 (Jar signature)和/或V2 (Full APK Signature。我选择了两个,分别为recommended in the documentation。文档但是没有说这

注意:如果您在使用APK签名方案V2签上您的应用程序,让该应用程序的进一步变化,应用程序的签名无效。出于这个原因,请在使用APK Signature Scheme 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?

+0

莫非你 “的zipalign -c -v 4 ” 输出结果呢? – Isuru

+0

虽然自Android 2.2.0以来,它不会产生未对齐的版本。 https://code.google.com/p/android/issues/detail?id=223551 – Isuru

+0

@ Isuru'zipalign -c -v 4 '返回一长串行,最后在底部显示'Verification succesful' [原文]。我认为文档说你不能zipalign已经用V2签名的应用程序?顺便说一下,我正在使用构建工具25.0.2。使用Android Studio中的菜单命令多年,没有问题了签约,但我最近更新的Android工作室,构建工具,SDK,一切... – BadCash

回答

1

问题是由具有外部脚本的gradle造成管理的生成APK的文件名。我已经完全忘记了这个脚本,现在它已经开始没有通过检查来看看这个APK是否是zipaligned,因为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签约失败,所以它会返回myApp-1.0-unaligned即使签名APK确实是zipaligned。

我只是去掉了IF语句,我只是保持

newApkName = "${appName}-${variant.versionName}.apk" 
相关问题