Search in sources :

Example 1 with BrutException

use of brut.common.BrutException in project Apktool by iBotPeaches.

the class TestUtils method copyResourceDir.

public static void copyResourceDir(Class class_, String dirPath, Directory out) throws BrutException {
    if (class_ == null) {
        class_ = Class.class;
    }
    URL dirURL = class_.getClassLoader().getResource(dirPath);
    if (dirURL != null && dirURL.getProtocol().equals("file")) {
        try {
            DirUtil.copyToDir(new FileDirectory(dirURL.getFile()), out);
        } catch (UnsupportedEncodingException ex) {
            throw new BrutException(ex);
        }
        return;
    }
    if (dirURL == null) {
        String className = class_.getName().replace(".", "/") + ".class";
        dirURL = class_.getClassLoader().getResource(className);
    }
    if (dirURL.getProtocol().equals("jar")) {
        String jarPath;
        try {
            jarPath = URLDecoder.decode(dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")), "UTF-8");
            DirUtil.copyToDir(new FileDirectory(jarPath), out);
        } catch (UnsupportedEncodingException ex) {
            throw new BrutException(ex);
        }
    }
}
Also used : BrutException(brut.common.BrutException) URL(java.net.URL)

Example 2 with BrutException

use of brut.common.BrutException in project Apktool by iBotPeaches.

the class Jar method extractToTmp.

public static File extractToTmp(String resourcePath, String tmpPrefix) throws BrutException {
    try {
        InputStream in = Class.class.getResourceAsStream(resourcePath);
        if (in == null) {
            throw new FileNotFoundException(resourcePath);
        }
        File fileOut = File.createTempFile(tmpPrefix, null);
        fileOut.deleteOnExit();
        OutputStream out = new FileOutputStream(fileOut);
        IOUtils.copy(in, out);
        in.close();
        out.close();
        return fileOut;
    } catch (IOException ex) {
        throw new BrutException("Could not extract resource: " + resourcePath, ex);
    }
}
Also used : BrutException(brut.common.BrutException)

Example 3 with BrutException

use of brut.common.BrutException 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)

Example 4 with BrutException

use of brut.common.BrutException in project Apktool by iBotPeaches.

the class AndrolibResources method getAaptBinaryFile.

/**
     * Using a prebuilt aapt and forcing its use, allows us to prevent bugs from older aapt's
     * along with having a finer control over the build procedure.
     *
     * Aapt can still be overridden via --aapt/-a on build, but specific features will be disabled
     *
     * @url https://github.com/iBotPeaches/platform_frameworks_base
     * @throws AndrolibException
     */
public File getAaptBinaryFile() throws AndrolibException {
    File aaptBinary;
    try {
        if (OSDetection.isMacOSX()) {
            if (OSDetection.is64Bit()) {
                aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/macosx/64/aapt");
            } else {
                aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/macosx/32/aapt");
            }
        } else if (OSDetection.isUnix()) {
            if (OSDetection.is64Bit()) {
                aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/linux/64/aapt");
            } else {
                aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/linux/32/aapt");
            }
        } else if (OSDetection.isWindows()) {
            aaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/windows/aapt.exe");
        } else {
            LOGGER.warning("Unknown Operating System: " + OSDetection.returnOS());
            return null;
        }
    } catch (BrutException ex) {
        throw new AndrolibException(ex);
    }
    if (aaptBinary.setExecutable(true)) {
        return aaptBinary;
    }
    System.err.println("Can't set aapt binary as executable");
    throw new AndrolibException("Can't set aapt binary as executable");
}
Also used : BrutException(brut.common.BrutException) AndrolibException(brut.androlib.AndrolibException) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 5 with BrutException

use of brut.common.BrutException in project Apktool by iBotPeaches.

the class Androlib method buildLibrary.

public void buildLibrary(File appDir, String folder) throws AndrolibException {
    File working = new File(appDir, folder);
    if (!working.exists()) {
        return;
    }
    File stored = new File(appDir, APK_DIRNAME + "/" + folder);
    if (apkOptions.forceBuildAll || isModified(working, stored)) {
        LOGGER.info("Copying libs... (/" + folder + ")");
        try {
            OS.rmdir(stored);
            OS.cpdir(working, stored);
        } catch (BrutException ex) {
            throw new AndrolibException(ex);
        }
    }
}
Also used : BrutException(brut.common.BrutException) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Aggregations

BrutException (brut.common.BrutException)13 ExtFile (brut.directory.ExtFile)8 ZipFile (java.util.zip.ZipFile)5 AndrolibException (brut.androlib.AndrolibException)2 File (java.io.File)2 SAXException (org.xml.sax.SAXException)2 InFileNotFoundException (brut.androlib.err.InFileNotFoundException)1 OutDirExistsException (brut.androlib.err.OutDirExistsException)1 IOException (java.io.IOException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1