2016/10/06

Proguard が "Ignoring InnerClasses attribute for an anonymous inner class" という Warning を出した場合の修正方法

Android Studio を更新したら、Proguard が "Ignoring InnerClasses attribute for an anonymous inner class" という Warning を大量に吐くようになってしまいました。

あまりに大量のエラーメッセージに最初読む気が失せてしまったのですが、よく見てみると、 以下のエラーが、様々なクラスに対して出力されているだけです。

Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(android.support.a.a.c) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.

原因

このエラー、バイナリ提供されているクラスのメソッドをオーバーライドし、その中で無名内部クラスを作成すると出力されます1

例えば、以下のようなケースです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do something
            }
        };
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(onClickListener);
    }
 
}

よくあるパターンですね。

エラーメッセージの中には、ソースコードから再コンパイルするのが推奨の解決策だと書かれていますが、 当然のことながら、android.app.Activity を再コンパイルするのは不可能なので、他に方法を探すしかありません。

解決策

EnclosingMethod に関して Proguard を無効にすればよいので、以下を proguard-rules.pro2 に追記します。

-keepattributes EnclosingMethod

参考

android - What is the "Ignoring InnerClasses attribute" warning output during compilation? - Stack Overflow

  1. かつ、Proguard で optimize が有効になっている場合 
  2. ファイル名は build.gradle で指定しているもの 
?

0 件のコメント: