Me name is Matthew and i have some trouble with reflection on Android after i use ProGuard.
I'm writing game in libgdx and I need to dynamically get String, I have something like this:
package com.PACKAGE;
public class CLASS extends ANOTHER_CLASS{
(...)
private static void load(String fieldName) {
Field field;
String fileName;
try {
field = SOME_CLASS.class.getField(fieldName);
fileName = (String) field.get(null);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
SOME_CLASS.SOME_METHOD.load(fileName);
}
(...)
private STH get(String fieldName) {
Field field;
String fileName;
try {
field = SOME_CLASS.class.getField(fieldName);
fileName = (String) field.get(null);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return SOME_CLASS.SOME_METHOD.get(fileName);
}
(...)
}
After I export release apk (compile fine) and run game, I get NoSuchFieldException:
java.lang.RuntimeException: java.lang.NoSuchFieldException: FIELD_NAME
What I need to add in proguard-project.txt
to handle this? This is not working, I have no clue what to do.
-keepclassmembers class com.PACKAGE.CLASS {
String fileName;
}
class looks like:
package com.ANOTHER_PACKAGE;
public class SOME_CLASS {
public static final String fieldName1 = "string I want";
public static final String fieldName2 = "string I want";
public static final String fieldName3 = "string I want";
}
UPDATE:
Thanks to @Selvin, the answer was
-keepclassmembers class ANOTHER_PACKAGE.SOME_CLASS {
public static final <fields>;
}
in project-proguard.txt
. Now it works perfectly :)