Skip to content

Instantly share code, notes, and snippets.

@franzejr
Last active August 29, 2015 14:15
Show Gist options
  • Save franzejr/b9467e6852a2b2a08664 to your computer and use it in GitHub Desktop.
Save franzejr/b9467e6852a2b2a08664 to your computer and use it in GitHub Desktop.
Executing a method from a APK/DEX
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtView = (TextView) findViewById(R.id.result);
try {
//Classes
String className = "com.ufc.methods.Sum";
// String paramclassName = "com.ufc.methods.Params";
//Apk
String appName = "StaticMath.apk";
String pathExternalStorage = Environment.getExternalStorageDirectory().toString();
File appDirectory = new File(pathExternalStorage +"/" + appName);
if(appDirectory.exists()){
txtView.setText(appDirectory.toString());
DexClassLoader dexLoader = new DexClassLoader(appDirectory.getAbsolutePath(),
getCacheDir().getAbsolutePath(), null, getClassLoader());
Class sumClass = dexLoader.loadClass(className);
Method executeMethod = sumClass.getMethods()[1];
String result = "";
try {
try {
executeMethod = sumClass.getMethod("execute", HashMap.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("first", "33333");
hashMap.put("second", "2");
result = (String)executeMethod.invoke(this, hashMap);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtView.setText(result);
}
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@franzejr
Copy link
Author

Method executeMethod = sumClass.getMethods()[1];

OR

sumClass.getMethod("execute", HashMap.class);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment