Search in sources :

Example 1 with FileDirectory

use of brut.directory.FileDirectory 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 FileDirectory

use of brut.directory.FileDirectory 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 3 with FileDirectory

use of brut.directory.FileDirectory 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)

Example 4 with FileDirectory

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

the class AndrolibResources method decodeManifestWithResources.

public void decodeManifestWithResources(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 {
        inApk = apkFile.getDirectory();
        out = new FileDirectory(outDir);
        LOGGER.info("Decoding AndroidManifest.xml with resources...");
        fileDecoder.decodeManifest(inApk, "AndroidManifest.xml", out, "AndroidManifest.xml");
        // Remove versionName / versionCode (aapt API 16)
        if (!resTable.getAnalysisMode()) {
            // check for a mismatch between resources.arsc package and the package listed in AndroidManifest
            // also remove the android::versionCode / versionName from manifest for rebuild
            // this is a required change to prevent aapt warning about conflicting versions
            // it will be passed as a parameter to aapt like "--min-sdk-version" via apktool.yml
            adjustPackageManifest(resTable, outDir.getAbsolutePath() + File.separator + "AndroidManifest.xml");
            ResXmlPatcher.removeManifestVersions(new File(outDir.getAbsolutePath() + File.separator + "AndroidManifest.xml"));
            mPackageId = String.valueOf(resTable.getPackageId());
        }
    } catch (DirectoryException ex) {
        throw new AndrolibException(ex);
    }
}
Also used : FileDirectory(brut.directory.FileDirectory) AndrolibException(brut.androlib.AndrolibException) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile) DirectoryException(brut.directory.DirectoryException) FileDirectory(brut.directory.FileDirectory) Directory(brut.directory.Directory)

Example 5 with FileDirectory

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

the class BuildAndDecodeTest method compareBinaryFolder.

private void compareBinaryFolder(String path, boolean res) throws BrutException, IOException {
    Boolean exists = true;
    String tmp = "";
    if (res) {
        tmp = File.separatorChar + "res" + File.separatorChar;
    }
    String location = tmp + path;
    FileDirectory fileDirectory = new FileDirectory(sTestOrigDir, location);
    Set<String> files = fileDirectory.getFiles(true);
    for (String filename : files) {
        File control = new File((sTestOrigDir + location), filename);
        File test = new File((sTestNewDir + location), filename);
        if (!test.isFile() || !control.isFile()) {
            exists = false;
        }
    }
    assertTrue(exists);
}
Also used : FileDirectory(brut.directory.FileDirectory) ExtFile(brut.directory.ExtFile)

Aggregations

FileDirectory (brut.directory.FileDirectory)5 AndrolibException (brut.androlib.AndrolibException)4 Directory (brut.directory.Directory)4 DirectoryException (brut.directory.DirectoryException)4 ExtFile (brut.directory.ExtFile)2 ExtMXSerializer (brut.androlib.res.util.ExtMXSerializer)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Matcher (java.util.regex.Matcher)1 ZipFile (java.util.zip.ZipFile)1