Search in sources :

Example 16 with AndrolibException

use of brut.androlib.AndrolibException in project Apktool by iBotPeaches.

the class AndrolibResources method generateValuesFile.

private void generateValuesFile(ResValuesFile valuesFile, Directory out, ExtXmlSerializer serial) throws AndrolibException {
    try {
        OutputStream outStream = out.getFileOutput(valuesFile.getPath());
        serial.setOutput((outStream), null);
        serial.startDocument(null, null);
        serial.startTag(null, "resources");
        for (ResResource res : valuesFile.listResources()) {
            if (valuesFile.isSynthesized(res)) {
                continue;
            }
            ((ResValuesXmlSerializable) res.getValue()).serializeToResValuesXml(serial, res);
        }
        serial.endTag(null, "resources");
        serial.newLine();
        serial.endDocument();
        serial.flush();
        outStream.close();
    } catch (IOException | DirectoryException ex) {
        throw new AndrolibException("Could not generate: " + valuesFile.getPath(), ex);
    }
}
Also used : ResValuesXmlSerializable(brut.androlib.res.xml.ResValuesXmlSerializable) ZipOutputStream(java.util.zip.ZipOutputStream) AndrolibException(brut.androlib.AndrolibException) DirectoryException(brut.directory.DirectoryException)

Example 17 with AndrolibException

use of brut.androlib.AndrolibException in project Apktool by iBotPeaches.

the class AndrolibResources method loadFrameworkPkg.

public ResPackage loadFrameworkPkg(ResTable resTable, int id, String frameTag) throws AndrolibException {
    File apk = getFrameworkApk(id, frameTag);
    LOGGER.info("Loading resource table from file: " + apk);
    ResPackage[] pkgs = getResPackagesFromApk(new ExtFile(apk), resTable, true);
    ResPackage pkg;
    if (pkgs.length > 1) {
        pkg = selectPkgWithMostResSpecs(pkgs);
    } else if (pkgs.length == 0) {
        throw new AndrolibException("Arsc files with zero or multiple packages");
    } else {
        pkg = pkgs[0];
    }
    if (pkg.getId() != id) {
        throw new AndrolibException("Expected pkg of id: " + String.valueOf(id) + ", got: " + pkg.getId());
    }
    resTable.addPackage(pkg, false);
    return pkg;
}
Also used : AndrolibException(brut.androlib.AndrolibException) ExtFile(brut.directory.ExtFile) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 18 with AndrolibException

use of brut.androlib.AndrolibException 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 19 with AndrolibException

use of brut.androlib.AndrolibException in project Apktool by iBotPeaches.

the class AndrolibResources method detectWhetherAppIsFramework.

public boolean detectWhetherAppIsFramework(File appDir) throws AndrolibException {
    File publicXml = new File(appDir, "res/values/public.xml");
    if (!publicXml.exists()) {
        return false;
    }
    Iterator<String> it;
    try {
        it = IOUtils.lineIterator(new FileReader(new File(appDir, "res/values/public.xml")));
    } catch (FileNotFoundException ex) {
        throw new AndrolibException("Could not detect whether app is framework one", ex);
    }
    it.next();
    it.next();
    return it.next().contains("0x01");
}
Also used : AndrolibException(brut.androlib.AndrolibException) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 20 with AndrolibException

use of brut.androlib.AndrolibException in project Apktool by iBotPeaches.

the class AndrolibResources method emptyFrameworkDirectory.

public void emptyFrameworkDirectory() throws AndrolibException {
    File dir = getFrameworkDir();
    File apk;
    apk = new File(dir, "1.apk");
    if (!apk.exists()) {
        LOGGER.warning("Can't empty framework directory, no file found at: " + apk.getAbsolutePath());
    } else {
        try {
            if (apk.exists() && dir.listFiles().length > 1 && !apkOptions.forceDeleteFramework) {
                LOGGER.warning("More than default framework detected. Please run command with `--force` parameter to wipe framework directory.");
            } else {
                for (File file : dir.listFiles()) {
                    if (file.isFile() && file.getName().endsWith(".apk")) {
                        LOGGER.info("Removing " + file.getName() + " framework file...");
                        file.delete();
                    }
                }
            }
        } catch (NullPointerException e) {
            throw new AndrolibException(e);
        }
    }
}
Also used : AndrolibException(brut.androlib.AndrolibException) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Aggregations

AndrolibException (brut.androlib.AndrolibException)23 ExtFile (brut.directory.ExtFile)11 ZipFile (java.util.zip.ZipFile)9 DirectoryException (brut.directory.DirectoryException)7 ZipOutputStream (java.util.zip.ZipOutputStream)5 Directory (brut.directory.Directory)4 FileDirectory (brut.directory.FileDirectory)4 IOException (java.io.IOException)3 BrutException (brut.common.BrutException)2 CantFind9PatchChunk (brut.androlib.err.CantFind9PatchChunk)1 CantFindFrameworkResException (brut.androlib.err.CantFindFrameworkResException)1 ResTable (brut.androlib.res.data.ResTable)1 ResBoolValue (brut.androlib.res.data.value.ResBoolValue)1 ResFileValue (brut.androlib.res.data.value.ResFileValue)1 ARSCData (brut.androlib.res.decoder.ARSCDecoder.ARSCData)1 ExtMXSerializer (brut.androlib.res.util.ExtMXSerializer)1 ResValuesXmlSerializable (brut.androlib.res.xml.ResValuesXmlSerializable)1 BufferedImage (java.awt.image.BufferedImage)1 PrintWriter (java.io.PrintWriter)1 BigInteger (java.math.BigInteger)1