Search in sources :

Example 1 with DirectoryException

use of brut.directory.DirectoryException in project Apktool by iBotPeaches.

the class AndrolibResources method decode.

public void decode(ResTable resTable, ExtFile apkFile, File outDir) throws AndrolibException {
    Duo<ResFileDecoder, AXmlResourceParser> duo = getResFileDecoder();
    ResFileDecoder fileDecoder = duo.m1;
    ResAttrDecoder attrDecoder = duo.m2.getAttrDecoder();
    attrDecoder.setCurrentPackage(resTable.listMainPackages().iterator().next());
    Directory inApk, in = null, out;
    try {
        out = new FileDirectory(outDir);
        inApk = apkFile.getDirectory();
        out = out.createDir("res");
        if (inApk.containsDir("res")) {
            in = inApk.getDir("res");
        }
        if (in == null && inApk.containsDir("r")) {
            in = inApk.getDir("r");
        }
        if (in == null && inApk.containsDir("R")) {
            in = inApk.getDir("R");
        }
    } catch (DirectoryException ex) {
        throw new AndrolibException(ex);
    }
    ExtMXSerializer xmlSerializer = getResXmlSerializer();
    for (ResPackage pkg : resTable.listMainPackages()) {
        attrDecoder.setCurrentPackage(pkg);
        LOGGER.info("Decoding file-resources...");
        for (ResResource res : pkg.listFiles()) {
            fileDecoder.decode(res, in, out);
        }
        LOGGER.info("Decoding values */* XMLs...");
        for (ResValuesFile valuesFile : pkg.listValuesFiles()) {
            generateValuesFile(valuesFile, out, xmlSerializer);
        }
        generatePublicXml(pkg, out, xmlSerializer);
    }
    AndrolibException decodeError = duo.m2.getFirstError();
    if (decodeError != null) {
        throw decodeError;
    }
}
Also used : FileDirectory(brut.directory.FileDirectory) AndrolibException(brut.androlib.AndrolibException) ExtMXSerializer(brut.androlib.res.util.ExtMXSerializer) DirectoryException(brut.directory.DirectoryException) FileDirectory(brut.directory.FileDirectory) Directory(brut.directory.Directory)

Example 2 with DirectoryException

use of brut.directory.DirectoryException in project Apktool by iBotPeaches.

the class AndrolibResources method generatePublicXml.

private void generatePublicXml(ResPackage pkg, Directory out, XmlSerializer serial) throws AndrolibException {
    try {
        OutputStream outStream = out.getFileOutput("values/public.xml");
        serial.setOutput(outStream, null);
        serial.startDocument(null, null);
        serial.startTag(null, "resources");
        for (ResResSpec spec : pkg.listResSpecs()) {
            serial.startTag(null, "public");
            serial.attribute(null, "type", spec.getType().getName());
            serial.attribute(null, "name", spec.getName());
            serial.attribute(null, "id", String.format("0x%08x", spec.getId().id));
            serial.endTag(null, "public");
        }
        serial.endTag(null, "resources");
        serial.endDocument();
        serial.flush();
        outStream.close();
    } catch (IOException | DirectoryException ex) {
        throw new AndrolibException("Could not generate public.xml file", ex);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) AndrolibException(brut.androlib.AndrolibException) DirectoryException(brut.directory.DirectoryException)

Example 3 with DirectoryException

use of brut.directory.DirectoryException in project Apktool by iBotPeaches.

the class ResSmaliUpdater method updateResIDs.

public void updateResIDs(ResTable resTable, File smaliDir) throws AndrolibException {
    try {
        Directory dir = new FileDirectory(smaliDir);
        for (String fileName : dir.getFiles(true)) {
            Iterator<String> it = IOUtils.readLines(dir.getFileInput(fileName)).iterator();
            PrintWriter out = new PrintWriter(dir.getFileOutput(fileName));
            while (it.hasNext()) {
                String line = it.next();
                out.println(line);
                Matcher m1 = RES_NAME_PATTERN.matcher(line);
                if (!m1.matches()) {
                    continue;
                }
                Matcher m2 = RES_ID_PATTERN.matcher(it.next());
                if (!m2.matches()) {
                    throw new AndrolibException();
                }
                int resID = resTable.getPackage(m1.group(1)).getType(m1.group(2)).getResSpec(m1.group(3)).getId().id;
                if (m2.group(1) != null) {
                    out.println(String.format(RES_ID_FORMAT_FIELD, m2.group(1), resID));
                } else {
                    out.println(String.format(RES_ID_FORMAT_CONST, m2.group(2), resID));
                }
            }
            out.close();
        }
    } catch (IOException ex) {
        throw new AndrolibException("Could not tag res IDs for: " + smaliDir.getAbsolutePath(), ex);
    } catch (DirectoryException ex) {
        throw new AndrolibException("Could not tag res IDs for: " + smaliDir.getAbsolutePath(), ex);
    }
}
Also used : Matcher(java.util.regex.Matcher) FileDirectory(brut.directory.FileDirectory) AndrolibException(brut.androlib.AndrolibException) IOException(java.io.IOException) DirectoryException(brut.directory.DirectoryException) Directory(brut.directory.Directory) FileDirectory(brut.directory.FileDirectory) PrintWriter(java.io.PrintWriter)

Example 4 with DirectoryException

use of brut.directory.DirectoryException in project Apktool by iBotPeaches.

the class Main method cmdDecode.

private static void cmdDecode(CommandLine cli) throws AndrolibException {
    ApkDecoder decoder = new ApkDecoder();
    int paraCount = cli.getArgList().size();
    String apkName = (String) cli.getArgList().get(paraCount - 1);
    File outDir;
    // check for options
    if (cli.hasOption("s") || cli.hasOption("no-src")) {
        decoder.setDecodeSources(ApkDecoder.DECODE_SOURCES_NONE);
    }
    if (cli.hasOption("d") || cli.hasOption("debug")) {
        System.err.println("SmaliDebugging has been removed in 2.1.0 onward. Please see: https://github.com/iBotPeaches/Apktool/issues/1061");
        System.exit(1);
    }
    if (cli.hasOption("b") || cli.hasOption("no-debug-info")) {
        decoder.setBaksmaliDebugMode(false);
    }
    if (cli.hasOption("t") || cli.hasOption("frame-tag")) {
        decoder.setFrameworkTag(cli.getOptionValue("t"));
    }
    if (cli.hasOption("f") || cli.hasOption("force")) {
        decoder.setForceDelete(true);
    }
    if (cli.hasOption("r") || cli.hasOption("no-res")) {
        decoder.setDecodeResources(ApkDecoder.DECODE_RESOURCES_NONE);
    }
    if (cli.hasOption("k") || cli.hasOption("keep-broken-res")) {
        decoder.setKeepBrokenResources(true);
    }
    if (cli.hasOption("p") || cli.hasOption("frame-path")) {
        decoder.setFrameworkDir(cli.getOptionValue("p"));
    }
    if (cli.hasOption("m") || cli.hasOption("match-original")) {
        decoder.setAnalysisMode(true, false);
    }
    if (cli.hasOption("api")) {
        decoder.setApi(Integer.parseInt(cli.getOptionValue("api")));
    }
    if (cli.hasOption("o") || cli.hasOption("output")) {
        outDir = new File(cli.getOptionValue("o"));
        decoder.setOutDir(outDir);
    } else {
        // make out folder manually using name of apk
        String outName = apkName;
        outName = outName.endsWith(".apk") ? outName.substring(0, outName.length() - 4).trim() : outName + ".out";
        // make file from path
        outName = new File(outName).getName();
        outDir = new File(outName);
        decoder.setOutDir(outDir);
    }
    decoder.setApkFile(new File(apkName));
    try {
        decoder.decode();
    } catch (OutDirExistsException ex) {
        System.err.println("Destination directory (" + outDir.getAbsolutePath() + ") " + "already exists. Use -f switch if you want to overwrite it.");
        System.exit(1);
    } catch (InFileNotFoundException ex) {
        System.err.println("Input file (" + apkName + ") " + "was not found or was not readable.");
        System.exit(1);
    } catch (CantFindFrameworkResException ex) {
        System.err.println("Can't find framework resources for package of id: " + String.valueOf(ex.getPkgId()) + ". You must install proper " + "framework files, see project website for more info.");
        System.exit(1);
    } catch (IOException ex) {
        System.err.println("Could not modify file. Please ensure you have permission.");
        System.exit(1);
    } catch (DirectoryException ex) {
        System.err.println("Could not modify internal dex files. Please ensure you have permission.");
        System.exit(1);
    }
}
Also used : CantFindFrameworkResException(brut.androlib.err.CantFindFrameworkResException) OutDirExistsException(brut.androlib.err.OutDirExistsException) InFileNotFoundException(brut.androlib.err.InFileNotFoundException) IOException(java.io.IOException) File(java.io.File) DirectoryException(brut.directory.DirectoryException)

Example 5 with DirectoryException

use of brut.directory.DirectoryException in project Apktool by iBotPeaches.

the class AndrolibResources method decodeManifest.

public void decodeManifest(ResTable resTable, ExtFile apkFile, File outDir) throws AndrolibException {
    Duo<ResFileDecoder, AXmlResourceParser> duo = getManifestFileDecoder();
    ResFileDecoder fileDecoder = duo.m1;
    // Set ResAttrDecoder
    duo.m2.setAttrDecoder(new ResAttrDecoder());
    ResAttrDecoder attrDecoder = duo.m2.getAttrDecoder();
    // Fake ResPackage
    attrDecoder.setCurrentPackage(new ResPackage(resTable, 0, null));
    Directory inApk, out;
    try {
        inApk = apkFile.getDirectory();
        out = new FileDirectory(outDir);
        LOGGER.info("Decoding AndroidManifest.xml with only framework resources...");
        fileDecoder.decodeManifest(inApk, "AndroidManifest.xml", out, "AndroidManifest.xml");
    } catch (DirectoryException ex) {
        throw new AndrolibException(ex);
    }
}
Also used : FileDirectory(brut.directory.FileDirectory) AndrolibException(brut.androlib.AndrolibException) DirectoryException(brut.directory.DirectoryException) FileDirectory(brut.directory.FileDirectory) Directory(brut.directory.Directory)

Aggregations

DirectoryException (brut.directory.DirectoryException)8 AndrolibException (brut.androlib.AndrolibException)7 Directory (brut.directory.Directory)4 FileDirectory (brut.directory.FileDirectory)4 ExtFile (brut.directory.ExtFile)2 IOException (java.io.IOException)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 CantFindFrameworkResException (brut.androlib.err.CantFindFrameworkResException)1 InFileNotFoundException (brut.androlib.err.InFileNotFoundException)1 OutDirExistsException (brut.androlib.err.OutDirExistsException)1 ExtMXSerializer (brut.androlib.res.util.ExtMXSerializer)1 ResValuesXmlSerializable (brut.androlib.res.xml.ResValuesXmlSerializable)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 Matcher (java.util.regex.Matcher)1 ZipFile (java.util.zip.ZipFile)1 DexBuilder (org.jf.dexlib2.writer.builder.DexBuilder)1 FileDataStore (org.jf.dexlib2.writer.io.FileDataStore)1