Search in sources :

Example 1 with JadxDecompiler

use of jadx.api.JadxDecompiler in project jadx by skylot.

the class IntegrationTest method getClassNodeFromFile.

public ClassNode getClassNodeFromFile(File file, String clsName) {
    JadxDecompiler d = new JadxDecompiler(args);
    try {
        d.loadFile(file);
    } catch (JadxException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    RootNode root = JadxInternalAccess.getRoot(d);
    root.getConstValues().getResourcesNames().putAll(resMap);
    ClassNode cls = root.searchClassByName(clsName);
    assertThat("Class not found: " + clsName, cls, notNullValue());
    assertThat(clsName, is(cls.getClassInfo().getFullName()));
    if (unloadCls) {
        decompile(d, cls);
    } else {
        decompileWithoutUnload(d, cls);
    }
    System.out.println("-----------------------------------------------------------");
    System.out.println(cls.getCode());
    System.out.println("-----------------------------------------------------------");
    checkCode(cls);
    compile(cls);
    runAutoCheck(clsName);
    return cls;
}
Also used : JadxException(jadx.core.utils.exceptions.JadxException) RootNode(jadx.core.dex.nodes.RootNode) ClassNode(jadx.core.dex.nodes.ClassNode) JadxDecompiler(jadx.api.JadxDecompiler)

Example 2 with JadxDecompiler

use of jadx.api.JadxDecompiler in project jadx by skylot.

the class SingleClassMode method process.

public static boolean process(JadxDecompiler jadx, JadxCLIArgs cliArgs) {
    String singleClass = cliArgs.getSingleClass();
    String singleClassOutput = cliArgs.getSingleClassOutput();
    if (singleClass == null && singleClassOutput == null) {
        return false;
    }
    ClassNode clsForProcess;
    if (singleClass != null) {
        clsForProcess = jadx.getRoot().resolveClass(singleClass);
        if (clsForProcess == null) {
            clsForProcess = jadx.getRoot().getClasses().stream().filter(cls -> cls.getClassInfo().getAliasFullName().equals(singleClass)).findFirst().orElse(null);
        }
        if (clsForProcess == null) {
            throw new JadxRuntimeException("Input class not found: " + singleClass);
        }
        if (clsForProcess.contains(AFlag.DONT_GENERATE)) {
            throw new JadxRuntimeException("Input class can't be saved by currect jadx settings (marked as DONT_GENERATE)");
        }
        if (clsForProcess.isInner()) {
            clsForProcess = clsForProcess.getTopParentClass();
            LOG.warn("Input class is inner, parent class will be saved: {}", clsForProcess.getFullName());
        }
    } else {
        // singleClassOutput is set
        // expect only one class to be loaded
        List<ClassNode> classes = jadx.getRoot().getClasses().stream().filter(c -> !c.isInner() && !c.contains(AFlag.DONT_GENERATE)).collect(Collectors.toList());
        int size = classes.size();
        if (size == 1) {
            clsForProcess = classes.get(0);
        } else {
            throw new JadxRuntimeException("Found " + size + " classes, single class output can't be used");
        }
    }
    ICodeInfo codeInfo;
    try {
        codeInfo = clsForProcess.decompile();
    } catch (Exception e) {
        throw new JadxRuntimeException("Class decompilation failed", e);
    }
    String fileExt = SaveCode.getFileExtension(jadx.getRoot());
    File out;
    if (singleClassOutput == null) {
        out = new File(jadx.getArgs().getOutDirSrc(), clsForProcess.getClassInfo().getAliasFullPath() + fileExt);
    } else {
        if (singleClassOutput.endsWith(fileExt)) {
            // treat as file name
            out = new File(singleClassOutput);
        } else {
            // treat as directory
            out = new File(singleClassOutput, clsForProcess.getShortName() + fileExt);
        }
    }
    File resultOut = FileUtils.prepareFile(out);
    if (clsForProcess.getClassInfo().hasAlias()) {
        LOG.info("Saving class '{}' (alias: '{}') to file '{}'", clsForProcess.getClassInfo().getFullName(), clsForProcess.getFullName(), resultOut.getAbsolutePath());
    } else {
        LOG.info("Saving class '{}' to file '{}'", clsForProcess.getFullName(), resultOut.getAbsolutePath());
    }
    SaveCode.save(codeInfo.getCodeStr(), resultOut);
    return true;
}
Also used : ICodeInfo(jadx.api.ICodeInfo) List(java.util.List) Logger(org.slf4j.Logger) ClassNode(jadx.core.dex.nodes.ClassNode) AFlag(jadx.core.dex.attributes.AFlag) JadxDecompiler(jadx.api.JadxDecompiler) LoggerFactory(org.slf4j.LoggerFactory) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) Collectors(java.util.stream.Collectors) FileUtils(jadx.core.utils.files.FileUtils) File(java.io.File) SaveCode(jadx.core.dex.visitors.SaveCode) ClassNode(jadx.core.dex.nodes.ClassNode) ICodeInfo(jadx.api.ICodeInfo) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) File(java.io.File) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 3 with JadxDecompiler

use of jadx.api.JadxDecompiler in project jadx by skylot.

the class SummaryNode method writeInputSummary.

private void writeInputSummary(StringEscapeUtils.Builder builder) throws IOException {
    builder.append("<h2>Input</h2>");
    JadxDecompiler jadx = mainWindow.getWrapper().getDecompiler();
    builder.append("<h3>Files</h3>");
    builder.append("<ul>");
    for (File inputFile : jadx.getArgs().getInputFiles()) {
        builder.append("<li>");
        builder.escape(inputFile.getCanonicalFile().getAbsolutePath());
        builder.append("</li>");
    }
    builder.append("</ul>");
    List<ClassNode> classes = jadx.getRoot().getClasses(true);
    List<String> codeSources = classes.stream().map(ClassNode::getInputFileName).distinct().sorted().collect(Collectors.toList());
    codeSources.remove("synthetic");
    int codeSourcesCount = codeSources.size();
    builder.append("<h3>Code sources</h3>");
    builder.append("<ul>");
    if (codeSourcesCount != 1) {
        builder.append("<li>Count: " + codeSourcesCount + "</li>");
    }
    // dex files list
    codeSources.removeIf(f -> !f.endsWith(".dex"));
    if (!codeSources.isEmpty()) {
        for (String input : codeSources) {
            builder.append("<li>");
            builder.escape(input);
            builder.append("</li>");
        }
    }
    builder.append("</ul>");
    int methodsCount = classes.stream().mapToInt(cls -> cls.getMethods().size()).sum();
    int fieldsCount = classes.stream().mapToInt(cls -> cls.getFields().size()).sum();
    int insnCount = classes.stream().flatMap(cls -> cls.getMethods().stream()).mapToInt(MethodNode::getInsnsCount).sum();
    builder.append("<h3>Counts</h3>");
    builder.append("<ul>");
    builder.append("<li>Classes: " + classes.size() + "</li>");
    builder.append("<li>Methods: " + methodsCount + "</li>");
    builder.append("<li>Fields: " + fieldsCount + "</li>");
    builder.append("<li>Instructions: " + insnCount + " (units)</li>");
    builder.append("</ul>");
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode) JadxDecompiler(jadx.api.JadxDecompiler) TabbedPane(jadx.gui.ui.TabbedPane) Set(java.util.Set) IOException(java.io.IOException) Icon(javax.swing.Icon) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ContentPanel(jadx.gui.ui.panel.ContentPanel) IAttributeNode(jadx.core.dex.attributes.IAttributeNode) Collectors(java.util.stream.Collectors) File(java.io.File) HashSet(java.util.HashSet) ProcessState(jadx.core.dex.nodes.ProcessState) List(java.util.List) ClassNode(jadx.core.dex.nodes.ClassNode) ErrorsCounter(jadx.core.utils.ErrorsCounter) UiUtils(jadx.gui.utils.UiUtils) ImageIcon(javax.swing.ImageIcon) JNode(jadx.gui.treemodel.JNode) JClass(jadx.gui.treemodel.JClass) MainWindow(jadx.gui.ui.MainWindow) HtmlPanel(jadx.gui.ui.panel.HtmlPanel) Utils(jadx.core.utils.Utils) ClassNode(jadx.core.dex.nodes.ClassNode) JadxDecompiler(jadx.api.JadxDecompiler) File(java.io.File)

Example 4 with JadxDecompiler

use of jadx.api.JadxDecompiler in project jadx by skylot.

the class IntegrationTest method loadFiles.

protected JadxDecompiler loadFiles(List<File> inputFiles) {
    args.setInputFiles(inputFiles);
    boolean useDx = !isJavaInput();
    LOG.info(useDx ? "Using dex input" : "Using java input");
    args.setUseDxInput(useDx);
    JadxDecompiler d = new JadxDecompiler(args);
    try {
        d.load();
    } catch (Exception e) {
        LOG.error("Load failed", e);
        d.close();
        fail(e.getMessage());
        return null;
    }
    RootNode root = JadxInternalAccess.getRoot(d);
    insertResources(root);
    return d;
}
Also used : RootNode(jadx.core.dex.nodes.RootNode) JadxDecompiler(jadx.api.JadxDecompiler) TimeoutException(java.util.concurrent.TimeoutException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with JadxDecompiler

use of jadx.api.JadxDecompiler in project jadx by skylot.

the class ExportGradleTest method exportGradle.

protected void exportGradle(String manifestFilename, String stringsFileName) {
    final JadxDecompiler decompiler = JadxDecompilerTestUtils.getMockDecompiler();
    ResourceFile androidManifest = mock(ResourceFile.class);
    final ResContainer androidManifestContainer = createResourceContainer(manifestFilename);
    when(androidManifest.loadContent()).thenReturn(androidManifestContainer);
    final ResContainer strings = createResourceContainer(stringsFileName);
    final RootNode root = decompiler.getRoot();
    final ExportGradleProject export = new ExportGradleProject(root, exportDir, androidManifest, strings);
    export.init();
    assertThat(export.getSrcOutDir().exists());
    assertThat(export.getResOutDir().exists());
}
Also used : ResourceFile(jadx.api.ResourceFile) RootNode(jadx.core.dex.nodes.RootNode) ExportGradleProject(jadx.core.export.ExportGradleProject) ResContainer(jadx.core.xmlgen.ResContainer) JadxDecompiler(jadx.api.JadxDecompiler)

Aggregations

JadxDecompiler (jadx.api.JadxDecompiler)12 JadxArgs (jadx.api.JadxArgs)4 ClassNode (jadx.core.dex.nodes.ClassNode)4 File (java.io.File)4 IOException (java.io.IOException)4 RootNode (jadx.core.dex.nodes.RootNode)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 IAttributeNode (jadx.core.dex.attributes.IAttributeNode)2 MethodNode (jadx.core.dex.nodes.MethodNode)2 ProcessState (jadx.core.dex.nodes.ProcessState)2 ErrorsCounter (jadx.core.utils.ErrorsCounter)2 Utils (jadx.core.utils.Utils)2 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)2 JClass (jadx.gui.treemodel.JClass)2 JNode (jadx.gui.treemodel.JNode)2 MainWindow (jadx.gui.ui.MainWindow)2 TabbedPane (jadx.gui.ui.TabbedPane)2 ContentPanel (jadx.gui.ui.panel.ContentPanel)2 HtmlPanel (jadx.gui.ui.panel.HtmlPanel)2