How to keep my test methods with proguard.cfg

2019-08-24 11:23发布

For my Android instrumentation test I need a few extra entry point into my classes. Those methods are not used in the actual application. My idea was to start them all with test_ and have a general rule to exclude them from being optimized away. This is how far I got:

-keepclassmembers class com.xxx.**.* {
    public ** test_* ();
    public ** test_* (**);
    public static ** test_* ();
    public static ** test_* (**);
}

But it still does not work. public static void test_destroy (final android.content.Context context) and private void dropTables (final SQLiteDatabase db) has just been removed from the code. And I have no idea why.

How is it properly used for wildcard patterns?

2条回答
神经病院院长
2楼-- · 2019-08-24 11:33

The solution is

-keepclassmembers class com.XXX.**.* {
    *** test_* (...);
}
查看更多
干净又极端
3楼-- · 2019-08-24 11:43

Another way to do this is to use an annotation (i.e. guava's @VisibleForTesting) to mark those methods. Then in proguard you can keep all entry points and members with that annotation:

-keep @com.google.common.annotations.VisibleForTesting class *

-keepclasseswithmembers class * {
  @com.google.common.annotations.VisibleForTesting *;
}
查看更多
登录 后发表回答