Search in sources :

Example 1 with AndrolibException

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

the class AndrolibResources method getFrameworkApk.

public File getFrameworkApk(int id, String frameTag) throws AndrolibException {
    File dir = getFrameworkDir();
    File apk;
    if (frameTag != null) {
        apk = new File(dir, String.valueOf(id) + '-' + frameTag + ".apk");
        if (apk.exists()) {
            return apk;
        }
    }
    apk = new File(dir, String.valueOf(id) + ".apk");
    if (apk.exists()) {
        return apk;
    }
    if (id == 1) {
        try (InputStream in = AndrolibResources.class.getResourceAsStream("/brut/androlib/android-framework.jar");
            OutputStream out = new FileOutputStream(apk)) {
            IOUtils.copy(in, out);
            return apk;
        } catch (IOException ex) {
            throw new AndrolibException(ex);
        }
    }
    throw new CantFindFrameworkResException(id);
}
Also used : CantFindFrameworkResException(brut.androlib.err.CantFindFrameworkResException) ZipOutputStream(java.util.zip.ZipOutputStream) AndrolibException(brut.androlib.AndrolibException) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 2 with AndrolibException

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

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

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

the class AndrolibResources method publicizeResources.

public void publicizeResources(File arscFile) throws AndrolibException {
    byte[] data = new byte[(int) arscFile.length()];
    try (InputStream in = new FileInputStream(arscFile);
        OutputStream out = new FileOutputStream(arscFile)) {
        in.read(data);
        publicizeResources(data);
        out.write(data);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) AndrolibException(brut.androlib.AndrolibException)

Example 5 with AndrolibException

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

the class AndrolibResources method aaptPackage.

public void aaptPackage(File apkFile, File manifest, File resDir, File rawDir, File assetDir, File[] include) throws AndrolibException {
    boolean customAapt = false;
    String aaptPath = apkOptions.aaptPath;
    List<String> cmd = new ArrayList<String>();
    // path for aapt binary
    if (!aaptPath.isEmpty()) {
        File aaptFile = new File(aaptPath);
        if (aaptFile.canRead() && aaptFile.exists()) {
            aaptFile.setExecutable(true);
            cmd.add(aaptFile.getPath());
            customAapt = true;
            if (apkOptions.verbose) {
                LOGGER.info(aaptFile.getPath() + " being used as aapt location.");
            }
        } else {
            LOGGER.warning("aapt location could not be found. Defaulting back to default");
            try {
                cmd.add(getAaptBinaryFile().getAbsolutePath());
            } catch (BrutException ignored) {
                cmd.add("aapt");
            }
        }
    } else {
        try {
            cmd.add(getAaptBinaryFile().getAbsolutePath());
        } catch (BrutException ignored) {
            cmd.add("aapt");
        }
    }
    cmd.add("p");
    if (apkOptions.verbose) {
        // output aapt verbose
        cmd.add("-v");
    }
    if (apkOptions.updateFiles) {
        cmd.add("-u");
    }
    if (apkOptions.debugMode) {
        // inject debuggable="true" into manifest
        cmd.add("--debug-mode");
    }
    // disable if user adds own aapt (can't know if they have this feature)
    if (mPackageId != null && !customAapt && !mSharedLibrary) {
        cmd.add("--forced-package-id");
        cmd.add(mPackageId);
    }
    if (mSharedLibrary) {
        cmd.add("--shared-lib");
    }
    if (mMinSdkVersion != null) {
        cmd.add("--min-sdk-version");
        cmd.add(mMinSdkVersion);
    }
    if (mTargetSdkVersion != null) {
        cmd.add("--target-sdk-version");
        cmd.add(mTargetSdkVersion);
    }
    if (mMaxSdkVersion != null) {
        cmd.add("--max-sdk-version");
        cmd.add(mMaxSdkVersion);
        // if we have max sdk version, set --max-res-version
        // so we can ignore anything over that during build.
        cmd.add("--max-res-version");
        cmd.add(mMaxSdkVersion);
    }
    if (mPackageRenamed != null) {
        cmd.add("--rename-manifest-package");
        cmd.add(mPackageRenamed);
    }
    if (mVersionCode != null) {
        cmd.add("--version-code");
        cmd.add(mVersionCode);
    }
    if (mVersionName != null) {
        cmd.add("--version-name");
        cmd.add(mVersionName);
    }
    cmd.add("--no-version-vectors");
    cmd.add("-F");
    cmd.add(apkFile.getAbsolutePath());
    if (apkOptions.isFramework) {
        cmd.add("-x");
    }
    if (apkOptions.doNotCompress != null) {
        for (String file : apkOptions.doNotCompress) {
            cmd.add("-0");
            cmd.add(file);
        }
    }
    if (!apkOptions.resourcesAreCompressed) {
        cmd.add("-0");
        cmd.add("arsc");
    }
    if (include != null) {
        for (File file : include) {
            cmd.add("-I");
            cmd.add(file.getPath());
        }
    }
    if (resDir != null) {
        cmd.add("-S");
        cmd.add(resDir.getAbsolutePath());
    }
    if (manifest != null) {
        cmd.add("-M");
        cmd.add(manifest.getAbsolutePath());
    }
    if (assetDir != null) {
        cmd.add("-A");
        cmd.add(assetDir.getAbsolutePath());
    }
    if (rawDir != null) {
        cmd.add(rawDir.getAbsolutePath());
    }
    try {
        OS.exec(cmd.toArray(new String[0]));
        if (apkOptions.verbose) {
            LOGGER.info("command ran: ");
            LOGGER.info(cmd.toString());
        }
    } catch (BrutException ex) {
        throw new AndrolibException(ex);
    }
}
Also used : BrutException(brut.common.BrutException) 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