Answer the question
In order to leave comments, you need to log in
How to use jar dynamically?
There is a simple .jar library, it has one class com.example.myclass:
package com.example;
public class myclass
{
public int Summ(int a, int b)
{
return a + b;
}
}
Manifest-Version: 1.0
Main-Class: com.example.myclass
String dexPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/lib.jar";
String dexOutputDir = getApplicationInfo().dataDir;
DexClassLoader pathClassLoader = new DexClassLoader(dexPath, dexOutputDir, null, this
.getClass().getClassLoader());
try {
Class<?> class1 = pathClassLoader.loadClass("com.example.myclass");
Object object = class1.newInstance();
Class[] params = new Class[2];
params[0] = Integer.TYPE;
params[1] = Integer.TYPE;
Method action = class1.getMethod("Summ", params);
Integer ret = (Integer) action.invoke(object, 12, 13);
tv.setText("method : " + action.getName() + ", return :" + ret);
} catch (Exception e) {
tv.setText("error : " + e.getMessage());
}
Answer the question
In order to leave comments, you need to log in
Found a solution:
File f = new File("/sdcard/MyFavorite/mygame.jar");
DexClassLoader u = new DexClassLoader(f.toURI().toURL().toString(), getDir("libs", MODE_PRIVATE).getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
Class c = u.loadClass("com.example.myclass");
Object object = c.newInstance();
Class[] params = new Class[2];
params[0] = Integer.TYPE;
params[1] = Integer.TYPE;
Method action = c.getMethod("Summ", params);
Integer ret = (Integer) action.invoke(object, 12, 13);
Log.d("DEBUG","method : " + action.getName() + ", return :" + ret);
dx.bat --dex --verbose --output .\libgame\classes.dex .\libgame
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question