0

I followed the exact steps given in the post below:

Is it possible to dynamically load a library at runtime from an Android application?

I am using and android 2.2 phone for testing and getting an error which is driving me crazy :(

07-27 01:24:55.692: W/System.err(14319): java.lang.ClassNotFoundException: com.shoaib.AndroidCCL.MyClass in loader dalvik.system.DexClassLoader@43abbc20

Can someone help me what to do now..i have tried various suggested solutions on different posts

I was wondering whether this was feasible so I wrote the following class:

package org.shoaib.androidccl;

import android.util.Log;

public class MyClass {
    public MyClass() {
        Log.d(MyClass.class.getName(), "MyClass: constructor called.");
    }

    public void doSomething() {
        Log.d(MyClass.class.getName(), "MyClass: doSomething() called.");
    }
}

And I packaged it in a DEX file that I saved on my device's SD card as /sdcard/testdex.jar.

Then I wrote the program below, after having removed MyClass from my Eclipse project and cleaned it:

public class Main extends Activity {

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            final String libPath = Environment.getExternalStorageDirectory() + "/testdex.jar";
            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("org.shoaib.androidccl.MyClass");

            final Object myInstance  = classToLoad.newInstance();
            final Method doSomething = classToLoad.getMethod("doSomething");

            doSomething.invoke(myInstance);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Community
  • 1
  • 1
Shoaib
  • 65
  • 7
  • Please post your code :) – JesusFreke Jul 26 '13 at 21:06
  • Hi, welcome to Stack Overflow - please try to improve this answer by adding some code, explaining where it breaks for example, and what you've tried to solve the problem. – Ian Clark Jul 26 '13 at 21:10
  • @JesusFreke. Thanks for ur reply. I simply followed the steps given in the [link](http://stackoverflow.com/questions/6857807/is-it-possible-to-dynamically-load-a-library-at-runtime-from-an-android-applicat/17890525#17890525) tried it on android 2.2 and 2.3 but still getting the same error – Shoaib Jul 27 '13 at 06:13
  • @JesusFreke ?? Still waiting for any suggestion what might be causing the error – Shoaib Jul 28 '13 at 09:13
  • I don't see anything wrong offhand. Look in logcat, there will almost certainly be more information about what is going wrong, before you get the ClassNotFoundException. – JesusFreke Jul 29 '13 at 17:46

1 Answers1

0

I guess your problem lies on the way you package MyClass in a DEX. How do you do that?

I followed Shlublu's thread and tried myself and succeeded. I don't know how to "package MyClass in a DEX", so I simply build an android apk and put it in the sdcard. And the code worked.

Are you wraping MyClass in a classes.dex? DexClassLoader will only look for classes.dex in your jar/dex. Maybe you can first try what I did (build an signed apk instead of jar, but you don't need to install the apk), and see if it's working. Hope this helps!

hyde
  • 60,639
  • 21
  • 115
  • 176
Chen Hao
  • 26
  • 3