mercredi 19 août 2020

Android Proguard/R8 reflection error: NoSuchFieldException

I have a large Constants Class, and I need to use reflection to get static String arrays from it.

public class Constants {

public static String OneAIntro = "Guten Morgen – Auf dich warten einige Fragen";
public static String[] OneAStepOne = {"Wie würdest du deinen Schlaf beurteilen?", "Sehr schlecht","Sehr gut", "Konntest du in der letzten Nacht gut einschlafen, durchschlafen und bist zur gewünschten Zeit aufgewacht?"};
public static String[] OneAStepTwo = {"Im Moment fühle ich mich ...", "zufrieden", "unzufrieden "};
...

Despite following all the rules suggested online, I am unable to prevent

java.lang.NoSuchFieldException: OneAStepOne

One of the things that is confusing me is the correct place to put the rules. Under Android Studio there is a file called proguard-rules.pro There are also files in the intermediate build directory /proguard-files/ proguard-android.txt-4.01, proguard-android-optiize.txt-4.01 and proguard-defaults.txt-4.01.

There are also files in the Android SDK folder, separate to the project in question. I have tried:

-keep class package.name.Constants

as well as:

-keepclassmembers class package.name.Constants {
    public static final <fields>;
}

and:

-keepclassmembers class package.name.Constants

but nothing seems to work. What is the correct way to be able to access public static String[]'s, using reflection, with proguard/R8 switched on, and which file exactly should I actually modify? :

            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

(I believe the above proguardFiles no longer do anything, and is just legacy code that is ignored from older versions of Android / Gradle?)





Aucun commentaire:

Enregistrer un commentaire