skylot / jadx

Dex to Java decompiler
Apache License 2.0
41.82k stars 4.89k forks source link

[feature] Jadx scripting: add Java support using JADXecute code #1833

Closed LaurieWired closed 1 year ago

LaurieWired commented 1 year ago

Describe your idea:

I created a scripting plugin for JADX a few weeks ago that allows you to input Java code as a jadx script: JADXecute

I was planning on submitting an issue to merge this into jadx but noticed you have added support for scripting which looks awesome, but I see you're only supporting Kotlin scripting right now. Having Java scripting would be nice as well so would you like me to add some of my JADXecute code?

image

Here's an example of one of the scripting options I added:

Rename a class

import jadx.gui.ui.MainWindow;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.RootNode;
import java.util.List;
import jadx.gui.treemodel.JClass;
import jadx.api.JavaClass;
import jadx.gui.plugins.jadxecute.RenameObjectHelper;

public class UserCodeClass {
    public static String userCodeMain(MainWindow mainWindow) {
        String jadxecuteOutput = "";
        String searchClassName = "exampleClassName"; // Update this

        // Find desired class
        RootNode root = mainWindow.getWrapper().getDecompiler().getRoot();
        List<ClassNode> classes = root.getClasses();
        for (ClassNode cls : classes) {
            if (cls.getFullName().contains(searchClassName)) {
                RenameObjectHelper renameObjectHelper = new RenameObjectHelper();
                JClass jclass = new JClass(cls.getJavaNode());

                // Rename found class to desired name
                jadxecuteOutput += renameObjectHelper.renameObject(mainWindow, jclass, "newClassName");

                // Optionally return here or you could add functionality to change all 
                //  matched objects to different names and return out of the loop
                return jadxecuteOutput;
            }
        }

        return jadxecuteOutput;
    }
}

Here's the full wiki of the scripting capabilities: JADXecute Wiki

skylot commented 1 year ago

Oh, good work :+1:

Having Java scripting would be nice as well so would you like me to add some of my JADXecute code?

Well, it will be hard to use your code directly, because your approach is very different: custom UI and usage of internal classes instead API. I think Java support implementation should be similar to current Kotlin approach, and needs only changes in compilation method and some adjustments in wrappers. Anyway, I think I will postpone work on java support until the current API will be in a more mature state. Also, I want to finish support for external plugins (with plugin manager for easy add/update), so it will be possible to convert your code to actual jadx plugin :slightly_smiling_face:

P.S. related to discussion in #1175

LaurieWired commented 1 year ago

Yeah totally understand. I used all the internal structures since the API was a bit limited for entire scripting on the fly needs, but if you're planning on updating the API as well that should cover it! I can close this issue for now and potentially turn this into a plugin instead once that support is added. I might leave my jadxecute plugin as having some crazy scripting options that I test out which are not supported by the stable jadx scripting functionality ;)