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)
See if you're following these steps.
DexClassLoader
as described here -> How to load a Java class dynamically on android/dalvik?
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With