accessing to classes of app from dex file by classloader

后端 未结 3 623
南笙
南笙 2021-02-06 16:13

I have a app that recieve a dex file from server then save it on sdcard and load it for

doing some functions.I am loading a my app\'s class from my dex file as followi

相关标签:
3条回答
  • 2021-02-06 16:56

    See if you're following these steps.

    1. Do you have the library file as an APK or a JAR containing (Classes.dex) file on the SD Card?
    2. Copy the APK file into a temporary folder into your application director /data/data/your.app.name/tmp. This is important as for security reasons Android doesn't let you load something off the SD card.
    3. Use DexClassLoader as described here -> How to load a Java class dynamically on android/dalvik?
    4. Use this classloader instance for loading any dynamic class files.

    If you have more than 1 JAR files then merge them into a single DEX file/APK.

    To convert your Java JAR file or classes into DEX you need to use the Android SDK's dx.bat. Here's a sample ANT script to do. This can also be done via Command line too with similar arguments.

    <exec executable="dx.bat">
        <arg value="--dex" />
        <arg value="--output=${target.directory.fullpath}/${apk.name}" />
        <arg value="--positions=lines" />
        <arg path="${classes.directory.full.path}" />
    </exec>
    
    0 讨论(0)
  • 2021-02-06 17:06

    @zohreh Replace the below line

    final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
    

    to

    final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
    
    0 讨论(0)
  • 2021-02-06 17:12

    I found my answer I need change

    final Class<Object> classToLoad = (Class<Object>)classloader.loadClass("com.example.myapp.M");
    

    to

    final Class<Object> classToLoad = (Class<Object>)Class.forName("com.example.myapp.M");
    

    in my dex file

    0 讨论(0)
提交回复
热议问题