[Java] 纯文本查看 复制代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addClassLoader(this, "/sdcard/2.dex");
}
//动态加载dex 文件
public void addClassLoader(Context context, String DexFilePath) {
Log.d("测试", "lado: ============成功加载lado");
File fIleDex = context.getDir("oopt_dex", 0);
File fileLib = context.getDir("lib_path", 0);
Log.d("测试", "lado: ============开始创建类加载器");
ClassLoader pathClassLoader= MainActivity.this.getClassLoader();
ClassLoader boootClassLoader= MainActivity.this.getClassLoader().getParent();
DexClassLoader dexClassLoader = new DexClassLoader(DexFilePath, fIleDex.getAbsolutePath(), fileLib.getAbsolutePath(), boootClassLoader);
try {
Field parentField = ClassLoader.class.getDeclaredField("parent");
parentField.setAccessible(true);
parentField.set(pathClassLoader,dexClassLoader);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Class<?> clazz = null;
try {
clazz = dexClassLoader.loadClass("com.playgame.demooo.MainActivity"); //这里直接就异常了
if (clazz != null) {
context.startActivity(new Intent(context,clazz));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
XML:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAG"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" tools:ignore="ProtectedPermissions" />
<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.demooo.MainActivity">
</activity>
<activity android:name="com.playgame.demooo.testMainActivity">
</activity>
</application>
dex:
不知道为什么在clazz = dexClassLoader.loadClass("com.playgame.demooo.MainActivity");
在这条就直接异常了,是不是我哪写错了? |