首页
论坛
课程
招聘
哪位大佬可以帮我看下这段添加自定义类加载器的代码哪有问题,一直报找不要到类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceClassLoader(this,"/sdcard/classes3.dex");
    }
 
    public void replaceClassLoader(ClassLoader classload) {
 
        try {
            //public static ActivityThread currentActivityThread()
            Class <?> currentActivityThread =  classload.loadClass("android.app.ActivityThread");
            Method currentActivityThreadMehood = currentActivityThread.getDeclaredMethod("currentActivityThread");
            currentActivityThreadMehood.setAccessible(true);
            Object sCurrentActivityThreadObj =  currentActivityThreadMehood.invoke(null);
 
            //final ArrayMap<String, WeakReference<LoadedApk>> mPackages = new  <>();
            Field mPackagesField = currentActivityThread.getDeclaredField("mPackages");
            mPackagesField.setAccessible(true);
            ArrayMap aryMap = (ArrayMap)mPackagesField.get(sCurrentActivityThreadObj);
            WeakReference wr = (WeakReference)aryMap.get(this.getPackageName());
            Object ladObject = wr.get();
 
            Class <?> LoadedApkClazz = classload.loadClass("android.app.LoadedApk");
            Field mClassLoader = LoadedApkClazz.getDeclaredField("mClassLoader");
            mClassLoader.setAccessible(true);
            mClassLoader.set(ladObject,classload);
 
 
 
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
 
    }
 
    public void replaceClassLoader(Context context, String DexFilePath){
        File dexFile = context.getDir("dex_path",0);
        File libFile = context.getDir("lib_path",0);
 
        ClassLoader PathClassLoader = context.getClassLoader();
        ClassLoader BootClassLader = context.getClassLoader().getParent();
        DexClassLoader dexClassload = new DexClassLoader(DexFilePath,dexFile.getAbsolutePath(), libFile.getAbsolutePath(),BootClassLader);
 
        Field parent = null;
        try {
            parent = ClassLoader.class.getDeclaredField("parent");
            parent.setAccessible(true);
            parent.set(PathClassLoader,dexClassload);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Class clazz =null;
        try {
             clazz = dexClassload.loadClass("com.playgame.demo.MainActivity");  //在这就找不到类了
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //调用主类
        if(clazz != null)
            startActivity(new Intent(context,clazz));
    }
}

XML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.playgame.myapplication">
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.playgame.demo.MainActivity"></activity>
 
    </application>
 
</manifest>

其实就2个输出语句:

 

我现在就是想在在其中增加自己的dexClassLaod类加载器,但是不知道哦为什么dexClassload.loadClass("com.playgame.demo.MainActivity"); //找不到这个类,是哪搞错了吗?

收藏
1条回答
guijingwen 2022-9-23

DexFilePath是SD卡路径,给sd卡权限没有?把logcat输出发出来看看

回复