現象 Android Studio 2.2で、Generate Signed APK時などに、大量のエラーが発生する
※ログ上ではエラーが表示されますが、APKは出力されます。
(エラー自体はIssueがあがっていますが、原因・対策不明のままなので、APKが出力されるのは、おそらく、一時的な対策かと・・)
Gradle Consoleの表示例
※表示されるクラス名(android.support.v4.a.eなど)は、他の場合もあります。
(おそらく、InnerClass(内部クラス)を持つ場合は、多くの場合、Proguardによる名前変更の影響を受けるはずなので。)
:app:transformClassesWithDexForRelease
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(android.support.v4.a.e) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
発生条件(一部)
[build.gradle]
・minifyEnabled true を指定している
[proguard-rules.pro]
・-keepattributes EnclosingMethod
を指定していない
[操作]
・「Generate Signed APK...」または「Build APK」を実行する
※「Make Project」や「Rebuild Project」では発生しないようです。
(:app:transformClassesWithDexForReleaseが呼ばれるかどうかで決まるはず)
対処法 proguardの設定(app\proguard-rules.proなど)に、-keepattributes EnclosingMethodを記述する
-keepattributes EnclosingMethod
Issue 215748のNo.7に、temporary workaroundがありますが・・
・optimizeの設定は、本現象に影響しないようです。
・InnerClassesは、元々指定されています。
(Android Plugin for Gradle 2.2以降では、ビルド時にproguard-android.txtなどが自動的に生成されるようになりました。)
詳細は、
(Project)\build\intermediates\proguard-files\
proguard-android.txt-2.2.0などを参照で。
# Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and will be ignored by new version of the Android plugin for Gradle.
# Preserve some attributes that may be required for reflection.
-keepattributes *Annotation*,Signature,InnerClasses
対処法をテストした環境
Android Studio 2.2
[Project]
Gradle version 2.14.1
Android Plugin version 2.2.0
build.gradle
compileSdkVersion 24
buildToolsVersion "24.0.2"
・・・
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
・・・
dependencies {
・・・
compile 'com.android.support:support-v4:24.2.1'
・・・
}
※proguard-android.txt、proguard-android-optimize.txtのどちらを指定しても、エラー発生の有無に影響しませんでした。
補足
Android Studio 2.2以前の環境で、本現象が発生していない場合は、
Android Studio 2.2へのアップデートは慎重に。
(アップデート時の問題は恒例行事になっていますが・・)
※2.2以前でも似たような現象が発生していた、という報告もあり。
手元環境では、Android Studio 2.1.3では同様のエラーは発生していませんでした。
Android Plugin Versionに依存する??
参考
Stackoverflow - Error in gradle build after updating Android Studio with log4j
http://stackoverflow.com/questions/36592864/error-in-gradle-build-after-updating-android-studio-with-log4j
[Issue 206867]
Build Errors
https://code.google.com/p/android/issues/detail?id=206867
[Issue 215748]
AGPBI reports errors instead of warnings
https://code.google.com/p/android/issues/detail?id=215748
[Proguard - Usage/ Optimization Options]
http://proguard.sourceforge.net/manual/usage.html#optimizationoptions