使用DexClassLoader时如何获取Activity字段的当前值?

How to get current value of field in Activity when using DexClassLoader?

我在 MainActivity 中有一个这样的字段

public class MainActivity extends AppCompatActivity {

private static String field = "abc";

private void setField(Intent intent) {
    String field2;
    if (intent != null && (field2 = intent.getStringExtra("field")) != null) {
        field = field2;

    }
}

/* access modifiers changed from: protected */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_main);
    setField(getIntent());
....

然后我使用此代码从 Dex 文件加载方法

Class<?> classToLoad = new DexClassLoader(codeFile.getAbsolutePath(), tmpDir.getAbsolutePath(), (String) null, ClassLoader.getSystemClassLoader()).loadClass("com.example.MyClass");
        Method method = classToLoad.getDeclaredMethod("run", new Class[0]);
        method.invoke(classToLoad, new Object[0]);

那么,在 运行() 方法中,如何获取 field 的当前值?谢谢!

public static void run()  {
    // How?

}

我使用 getContextClassLoader

找到了解决方案
Class clazz = callerClasses.loadClass("com.example.MainActivity");       
Fields field = new Fields();
Field field= clazz.getDeclaredField("field");
flag.setAccessible(true);
Log.d(TAG, "field: " + flag.get(field));

其中"Fields"是Dex代码中的一个内class

private static class Fields {

    private static String field = "test";
}