Proguard:保持动态声明的方法

时间:2012-04-18 15:33:10

标签: android proguard

我正在使用反射调用一个超出Android应用程序目标API级别的方法:

try {
    Method m = Class.forName("android.content.Context")
            .getDeclaredMethod("getExternalCacheDir");
    Object result = m.invoke(this);
    if (result instanceof File) {
        Log.v("MyApp", "external cache: "
                + ((File) result).getAbsolutePath());
        cacheDirectory = (File) result;
    } else {
        Log.v("MyApp", "non-file cache: " + result);
    }
} catch (Exception e) {
    // ...
}

我可以通过Proguard毫无问题地优化它,但它警告我:

Note: com.example.MyApp accesses a declared method 'getExternalCacheDir()' dynamically
      Maybe this is library method 'android.content.Context { java.io.File getExternalCacheDir(); }'
      Maybe this is library method 'android.content.ContextWrapper { java.io.File getExternalCacheDir(); }'
      Maybe this is library method 'android.test.mock.MockContext { java.io.File getExternalCacheDir(); }'
Note: there were 1 accesses to class members by means of introspection.
      You should consider explicitly keeping the mentioned class members
      (using '-keep' or '-keepclassmembers').

这是一个实际问题,还是Proguard只是告诉我潜在的问题?

2 个答案:

答案 0 :(得分:5)

这与Paul Lammertsma发布的答案类似。另请参阅ProGuard Eric Lafortune的答案作者:How to suppress 'Maybe this is program method' warnings from ProGuard

  

您可以通过明确提及中的方法来避免它   配置:

     

-keep class com.foo.OtherClass { com.foo.OtherClass getInstance(); }

     

或者,您可以取消关于班级的注释:

     

-dontnote com.foo.MyClass

答案 1 :(得分:3)

问题出在我正在使用的图书馆项目中,而该项目的proguard.cfg未被Proguard检查。

通过在我自己的项目proguard.cfg中添加以下行,我能够使通知消失:

-keep class android.content.Context {
    public java.io.File getExternalCacheDir();
}