How to use java language to dynamically debug APK in AndroidStudio?

232 Views Asked by At

Requirement: I now have a CrackMe.apk without project file and code, I want to modify its code in java language without using smali and to dynamically debug the modified java file. I looked up many ways to dynamically debug the apk on the internet, and the internet is full of debugging using smali language, there is no case of debugging the apk using java language. Now, I am able to modify and run CrackMe.apk, but I don't know how to debug my code dynamically, I would like to ask how to implement dynamic debugging feature?

Steps: I extract the dex file from CrackMe.apk, convert it to classes.jar file, remove the android and androidx packages from the jar file (not removing it will prompt the existence of the same package name after Add As Library). Then create a new blank project test in AS software, add classes.jar As Library. then create a new class MainActivity.java with the same name in the file path src/main/com.XXX.XXX, modify the code if (this$0. check() == 999) to if (this$0.check() == 3) and compile it. After compiling, overwrite the new MainActivity.class in the path build\intermediates\javac\debug\classes\com.XXX.XXX with the MainActivity.class file of the same name in the old classes.jar, and then convert the classes.jar to classes.dex and replace the old classes.dex, then after signing it, it can be installed and run.

During this process, can I debug the new MainActivity.java file at the AS software breakpoint in the test project path src/main/com.XXX.XXX. The class file generated by the test project is in the test project file path build\ intermediates\javac\debug\classes\com.XXX.XXX in the test project. I want to dynamically debug CrackMe.apk, not test-debug.apk, MainActivity.java has nothing to do with the test project.
ApkLink: CrackMe.apk

1

There are 1 best solutions below

1
Anique Azhar On

Well, there is no direct way to modify an apk dynamically using java. All you can do is instrument the apk using any of instrumentation library which are written in multiple languages like tyscript or JavaScript.

Among these libraries, Frida & Objection are the most famous ones. Frida allows you to change any value, methods, return types of values, any thing loaded in memory.

Good thing is that you dont need to learn smali, you can still use Java libraries to call any function on it.

Sample code in your case will look like

Java.perform(function() {
var object = Java.use('packageName.ActivityName');

object.check.implementation = function() {
    
    return 3;
}
});