Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing to classes of app from dex file by classloader

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 following

dex file

public class Main  {


    public void onCreate() 
    {
        System.out.print("------------onCreate------------");
        try {
             final ClassLoader classloader = ClassLoader.getSystemClassLoader();
            final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("com.example.myapp.M");
            final Object myInstance  = classToLoad.newInstance();
            final Method doSomething = classToLoad.getMethod("doSomething");
            doSomething.invoke(myInstance);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

app code

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            final String libPath = Environment.getExternalStorageDirectory() + "/Lib/lib.apk";
            final File tmpDir = getDir("dex", 0);
            final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
            final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("com.example.lib.Main");
            final Object myInstance  = classToLoad.newInstance();
            final Method doSomething = classToLoad.getMethod("onCreate");
            doSomething.invoke(myInstance);
            } catch (Exception e) {
            e.printStackTrace();
        }
    }

M class in my app

public class M {
public void doSomething()
{
    Log.e("doSomething", "--------------------doSomething----");
}
}

but it have me following error.what is wrong in my code

11-19 08:24:12.718: W/System.err(10867): java.lang.ClassNotFoundException: Didn't find class "com.example.myapp2.M" on path: .
11-19 08:24:12.738: W/System.err(10867):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
11-19 08:24:12.747: W/System.err(10867):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-19 08:24:12.758: W/System.err(10867):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-19 08:24:12.758: W/System.err(10867):    at com.example.lib.Main.onCreate(Main.java:15)
11-19 08:24:12.768: W/System.err(10867):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 08:24:12.788: W/System.err(10867):    at java.lang.reflect.Method.invoke(Method.java:511)
11-19 08:24:12.788: W/System.err(10867):    at com.example.myapp2.MainActivity.onCreate(MainActivity.java:28)
11-19 08:24:12.798: W/System.err(10867):    at android.app.Activity.performCreate(Activity.java:5104)
11-19 08:24:12.818: W/System.err(10867):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-19 08:24:12.828: W/System.err(10867):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-19 08:24:12.828: W/System.err(10867):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 08:24:12.838: W/System.err(10867):    at android.os.Looper.loop(Looper.java:137)
11-19 08:24:12.847: W/System.err(10867):    at android.app.ActivityThread.main(ActivityThread.java:5041)
11-19 08:24:12.847: W/System.err(10867):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 08:24:12.847: W/System.err(10867):    at java.lang.reflect.Method.invoke(Method.java:511)
11-19 08:24:12.857: W/System.err(10867):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-19 08:24:12.898: W/System.err(10867):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-19 08:24:12.898: W/System.err(10867):    at dalvik.system.NativeStart.main(Native Method)
like image 466
zohreh Avatar asked Nov 19 '13 09:11

zohreh


2 Answers

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>
like image 168
Swaroop Avatar answered Oct 09 '22 06:10

Swaroop


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

like image 31
zohreh Avatar answered Oct 09 '22 06:10

zohreh