Last active
August 29, 2015 14:15
-
-
Save franzejr/b9467e6852a2b2a08664 to your computer and use it in GitHub Desktop.
Executing a method from a APK/DEX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Method executeMethod = sumClass.getMethods()[1];
OR
sumClass.getMethod("execute", HashMap.class);