Loading .dex dynamically in android
Asked Answered
M

0

0

Is there any way to load .dex file dynamically at run time in android. I tried as below.

try {
    File dexDir = getApplicationContext().getCacheDir();
    String dexpath = Environment.getExternalStorageState()+"/app-debug.jar";
    DexClassLoader dexClassLoader = new DexClassLoader(dexpath,dexDir.getAbsolutePath()   ,null,context.getClassLoader());
    //PathClassLoader dexClassLoader = new PathClassLoader(dexpath,context.getClassLoader());
    Class<Object> classToLoad = (Class<Object>) dexClassLoader.loadClass("com.example.welcome.myclass.TestClass");
    Object newInstance = classToLoad.newInstance();
    Method method = classToLoad.getMethod("toast");
    method.invoke(newInstance);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

getting following error.

 java.lang.ClassNotFoundException: Didn't find class "com.example.welcome.myclass.TestClass" on path: DexPathList[[],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

    Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.example.welcome.myclass.TestClass" on path: DexPathList[[zip file "/data/app/com.example.welcome.dcl-1/base.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.welcome.dcl-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

08-11 16:15:05.179 9197-9197/com.example.welcome.dcl W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

Muff answered 11/8, 2017 at 9:38 Comment(12)
what is the content of your jar file?Elba
its a simple apk file containing class TestClass , i had renamed .apk to .jarMuff
you need to provide a .dex file inside the apk/jar, is your TestClass a .class file?Elba
@Elba i had compiled a project called myclass2 making .apk and renaming to .jar . yes TestClass is a .class file in app-debug.jarMuff
you need to compile your .class files into a dex, you can use dx tool from Android sdkElba
or, much easier, make a normal app project in android studio, compile the apk and use thatElba
am using simple compiled apk n still not working , getting errorMuff
if you are using a simple compiled apk how come there is a .class file in your apk?Elba
i mean that when i reversed it than i found .class file.Muff
then there is something wrong because android wants .class files to be compiled into a .dex fileElba
bro am having a normal apk like everone , i have used both .apk /.jar , none working , if u could help with thatMuff
bro, if you don't know the difference between a .class file and a .dex file you're gonna have a hard time in your attemptElba

© 2022 - 2024 — McMap. All rights reserved.