0

我在 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]);

那么,在run()方法中,如何获取字段的当前值?谢谢!

public static void run()  {
    // How?

}
4

1 回答 1

0

我使用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));

哪个“字段”是 Dex 代码中的内部类

private static class Fields {

    private static String field = "test";
}
于 2020-04-29T03:43:56.227 回答