2017-04-04 46 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" 
相關問題