peacepassion peacepassion - 4 months ago 20
Android Question

How proguard can skip some reserved names as mapping result

I am developing a gradle plugin. It generates a source java file after proguard based on the mapping.txt. The content looks like following.

public class bmdb {

private static Map<Class<?>, Injector<Object>> map = new HashMap<>();

static {
try {
map.put(a.a.b.c.class, new a.a.b.d());

} catch (Exception e) {
e.printStackTrace();
}
}

public static Map<Class<?>, Injector<Object>> a() {
return map;
}
}


And this java file will be compiled to a class file. However, sometimes the mapped result would be java key words, like
a.a.for.class
,
a.a.int.class
. And the compilation would fail.

So I wonder if proguard has any option which can exclude some words for the mapping result.

Answer

Finally, I found proguard does not support such configuration.

And I use following workround:
1. I write a string array containing Java key words like following:
String[] keyWords = ["do", "if", ...];
2. I would check the proguard result if it is in the above array. If NO, the normal new abc() will be used, and If YES, the reflection Class.forName("for") will be used.

So the result might be like following:

map.put(a.a.b.c.class, new d());
map.put(a.a.b.d.class, Class.forName("for").newInstance());