2015/10/10

AndroidStudio で Generate Signed APK を実行したら "Unable to compute hash of ..." と出た場合の対処方法

AndroidStudio で署名した APK を作成しようとしたら以下のようなエラーがでてしまいました AndroidStudio 1.4
compileSdkVersion 23
buildToolsVersion "23.0.1"


Information:Gradle tasks [:app:assembleFreeRelease]
:AndroidUiParts:compileLint
:AndroidUiParts:copyReleaseLint UP-TO-DATE
:AndroidUiParts:mergeReleaseProguardFiles UP-TO-DATE
:AndroidUiParts:preBuild UP-TO-DATE
:AndroidUiParts:preReleaseBuild UP-TO-DATE
:AndroidUiParts:checkReleaseManifest
:AndroidUiParts:prepareReleaseDependencies
:AndroidUiParts:compileReleaseAidl UP-TO-DATE
:AndroidUiParts:compileReleaseRenderscript UP-TO-DATE
:AndroidUiParts:generateReleaseBuildConfig UP-TO-DATE
:AndroidUiParts:generateReleaseAssets UP-TO-DATE
:AndroidUiParts:mergeReleaseAssets UP-TO-DATE
:AndroidUiParts:generateReleaseResValues UP-TO-DATE
:AndroidUiParts:generateReleaseResources UP-TO-DATE
:AndroidUiParts:packageReleaseResources UP-TO-DATE
:AndroidUiParts:processReleaseManifest UP-TO-DATE
:AndroidUiParts:processReleaseResources UP-TO-DATE
:AndroidUiParts:generateReleaseSources UP-TO-DATE
:AndroidUiParts:processReleaseJavaRes UP-TO-DATE
:AndroidUiParts:compileReleaseJavaWithJavac UP-TO-DATE
:AndroidUiParts:packageReleaseJar UP-TO-DATE
:AndroidUiParts:compileReleaseNdk UP-TO-DATE
:AndroidUiParts:packageReleaseJniLibs UP-TO-DATE
:AndroidUiParts:packageReleaseLocalJar UP-TO-DATE
:AndroidUiParts:packageReleaseRenderscript UP-TO-DATE
:AndroidUiParts:bundleRelease UP-TO-DATE
:app:preBuild UP-TO-DATE
:app:preFreeReleaseBuild UP-TO-DATE
:app:checkFreeReleaseManifest
:LVL:compileLint
:LVL:copyReleaseLint UP-TO-DATE
:LVL:mergeReleaseProguardFiles UP-TO-DATE
:LVL:preBuild UP-TO-DATE
:LVL:preReleaseBuild UP-TO-DATE
:LVL:checkReleaseManifest
:LVL:prepareReleaseDependencies
:LVL:compileReleaseAidl UP-TO-DATE
:LVL:compileReleaseRenderscript UP-TO-DATE
:LVL:generateReleaseBuildConfig UP-TO-DATE
:LVL:generateReleaseAssets UP-TO-DATE
:LVL:mergeReleaseAssets UP-TO-DATE
:LVL:generateReleaseResValues UP-TO-DATE
:LVL:generateReleaseResources UP-TO-DATE
:LVL:packageReleaseResources UP-TO-DATE
:LVL:processReleaseManifest UP-TO-DATE
:LVL:processReleaseResources UP-TO-DATE
:LVL:generateReleaseSources UP-TO-DATE
:LVL:processReleaseJavaRes UP-TO-DATE
:LVL:compileReleaseJavaWithJavac UP-TO-DATE
:LVL:packageReleaseJar UP-TO-DATE
:LVL:compileReleaseNdk UP-TO-DATE
:LVL:packageReleaseJniLibs UP-TO-DATE
:LVL:packageReleaseLocalJar UP-TO-DATE
:LVL:packageReleaseRenderscript UP-TO-DATE
:LVL:bundleRelease UP-TO-DATE
:app:preFreeDebugBuild UP-TO-DATE
:app:preProDebugBuild UP-TO-DATE
:app:preProReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAds810Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing810Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement810Library UP-TO-DATE
:app:prepareComKokufuAndroidLibSqliteviewerhelperAar10Library UP-TO-DATE
:app:prepareComKokufuAndroidLibUiFileselectoractivityAar01Library UP-TO-DATE
:app:prepareSQLiteViewerAndroidUiPartsUnspecifiedLibrary UP-TO-DATE
:app:prepareFreeReleaseDependencies
:app:compileFreeReleaseAidl
:app:compileFreeReleaseRenderscript
:app:generateFreeReleaseBuildConfig
:app:generateFreeReleaseAssets UP-TO-DATE
:app:mergeFreeReleaseAssets
:app:generateFreeReleaseResValues UP-TO-DATE
:app:generateFreeReleaseResources
:app:mergeFreeReleaseResources
:app:processFreeReleaseManifest
:app:processFreeReleaseResources
:app:generateFreeReleaseSources
:app:processFreeReleaseJavaRes UP-TO-DATE
:app:compileFreeReleaseJavaWithJavac
Note: /home/yusuke/Projects/AndroidStudioProjects/SQLiteViewer/app/src/main/java/com/kokufu/android/apps/sqliteviewer/MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:app:compileFreeReleaseNdk UP-TO-DATE
:app:compileFreeReleaseSources
:app:proguardFreeRelease
Warning:com.google.android.gms.internal.zzac: can't find referenced class android.net.http.AndroidHttpClient
Warning:com.google.android.gms.internal.zzw: can't find referenced method 'void addHeader(java.lang.String,java.lang.String)' in program class com.google.android.gms.internal.zzw$zza
Warning:com.google.android.gms.internal.zzw$zza: can't find referenced method 'void setURI(java.net.URI)' in program class com.google.android.gms.internal.zzw$zza
Warning:there were 2 unresolved references to classes or interfaces.
         You may need to add missing library jars or update their versions.
         If your code works fine without the missing classes, you can suppress
         the warnings with '-dontwarn' options.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
Warning:there were 2 unresolved references to program class members.
         Your input classes appear to be inconsistent.
         You may need to recompile the code.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
Exception while processing task 
java.io.IOException: Please correct the above warnings first.
 at proguard.Initializer.execute(Initializer.java:473)
 at proguard.ProGuard.initialize(ProGuard.java:233)
 at proguard.ProGuard.execute(ProGuard.java:98)
 at proguard.gradle.ProGuardTask.proguard(ProGuardTask.java:1074)
 at com.android.build.gradle.tasks.AndroidProGuardTask.doMinification(AndroidProGuardTask.java:139)
 at com.android.build.gradle.tasks.AndroidProGuardTask$1.run(AndroidProGuardTask.java:115)
 at com.android.builder.tasks.Job.runTask(Job.java:48)
 at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
 at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:227)
 at java.lang.Thread.run(Thread.java:745)
:app:dexFreeRelease
:app:validateExternalOverrideSigning
:app:packageFreeRelease FAILED
Error:Execution failed for task ':app:packageFreeRelease'.
> Unable to compute hash of /home/yusuke/Projects/AndroidStudioProjects/SQLiteViewer/app/build/intermediates/classes-proguard/free/release/classes.jar
Information:BUILD FAILED
Information:Total time: 3.95 secs
Information:1 error
Information:5 warnings
Information:See complete output in console

原因

エラーメッセージをちゃんと読めばわかるのですが、このエラーは署名をつけるところではなく、Proguard による難読化のところで発生しています。
どうも、必要な参照が SDK の中から消えたのが原因のようです。

上記では org.apache.http が削除されたと書いてありますが、関連する android.net.http.AndroidHttpClient も同時に削除されていますね。

解決策

これもエラーメッセージに書いてありました。
You may need to add missing library jars or update their versions. If your code works fine without the missing classes, you can suppress the warnings with '-dontwarn' options.

(意訳)
見つからないライブラリの jar を追加するかアップデートする必要があるかもしれません。
もし、それらのクラスが無くてもコードが実行できているのであれば、'-dontwarn' オプションを使用して警告を抑制できます。

というわけで、今回のケースでは以下の一行を proguard-rules.pro に加えることで無事署名付きAPKを作成することが出来るようになりました。

proguard-rules.pro
-dontwarn com.google.android.gms.**

パッケージ名は Warning メッセージの内容から取ってきます。
また、場合によっては本当に jar が足りない場合等もありますから、よく Warning や Error の内容を読んで判断する必要があるでしょう結局、結論はちゃんとエラー読むって…
あたりまえじゃん!


0 件のコメント: