Error:Execution failed for task ':transformClassesAndResourcesWithProguardForRelease' への対策 : Android Studio
Proguard.txtを設置した
Proguardとは
Androidアプリのリリース版などで使われる、.apkファイルは簡単に開くことが可能です。つまり、頑張って書いたコードなどが盗用されてしまう可能性もあれば、クレジットカード情報などを登録しているアプリであればその情報を得るために脆弱性を突いてくる可能性もあります。
これを防ぐためにあるのが、Proguardでソースコードを難読化してくれます。
Androidアプリを公開するにはほぼ必須のproguardですが、たまにハマる箇所が多かったので、その対策などをまとめていきます。
Proguardの設定
勝手に生成される
Android Studioでプロジェクトを新しく作ると勝手に作成されます。Proguard.proはGradle buildにできています。
build.gradleの変更
app直下のbuild.gradleファイル内で minifyEnabledをtrueにします。build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.1" defaultConfig { applicationId "com.example.Users.myapplication" /* ... */ } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
これでデフォルトのProguardの設定になりました。
proguard-rules.pro
これだけで完了ですが、ここで細かい設定を追加することが可能です。proguard-rules.pro
# To enable ProGuard in your project, edit project.properties # to define the proguard.config property as described in that file. # # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in Users/../../tools/proguard/proguard-android.txt # You can edit the include path and order by changing the ProGuard # include property in project.properties. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} #以下、追加しかたの例 #keep:特定のものを難読化対象から外す -keep class android.support.v4.app.** { *; } #dontwarn:warningを消す -dontwarn android.support.v4.**
Proguard設定時の予想されるエラー
Buildすると以下のようなwarningが出ることがあります。android.webkit...が参照はされているが、実行はされていないという警告を出しています。
proguardの場合、これだけでもエラーになり、Buildができないのでこれを直します。
エラー
Warning:library class android.webkit.WebViewClient depends on program class android.net.http.SslError
Warning:there were 3 instances of library classes depending on program classes.
Warning:Exception while processing task java.io.IOException: Please correct the above warnings first.
Error:Execution failed for task ':AppName:transformClassesAndResourcesWithProguardForRelease'.
> java.io.IOException: Please correct the above warnings first.
直し方
proguard-rules.pro
# To enable ProGuard in your project, edit project.properties # to define the proguard.config property as described in that file. # # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in Users/../../tools/proguard/proguard-android.txt # You can edit the include path and order by changing the ProGuard # include property in project.properties. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} #この行を追加します。 -dontwarn android.webkit.**
単純にwebkitでwarningが出ているのでそのwarningをなくす設定を組み込みます。
proguard-rules.proに -dontwarnを追加します。
これでこのエラーは消えます。場合によっては複数のライブラリでエラーを起こすので一つずつ消していってください。
参考にしました。
developer.android.com