Search in sources :

Example 21 with ExtFile

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

the class Androlib method build.

public void build(ExtFile appDir, File outFile) throws BrutException {
    LOGGER.info("Using Apktool " + Androlib.getVersion());
    MetaInfo meta = readMetaFile(appDir);
    apkOptions.isFramework = meta.isFrameworkApk;
    apkOptions.resourcesAreCompressed = meta.compressionType;
    apkOptions.doNotCompress = meta.doNotCompress;
    mAndRes.setSdkInfo(meta.sdkInfo);
    mAndRes.setPackageId(meta.packageInfo);
    mAndRes.setPackageRenamed(meta.packageInfo);
    mAndRes.setVersionInfo(meta.versionInfo);
    mAndRes.setSharedLibrary(meta.sharedLibrary);
    if (meta.sdkInfo != null && meta.sdkInfo.get("minSdkVersion") != null) {
        mMinSdkVersion = Integer.parseInt(meta.sdkInfo.get("minSdkVersion"));
    }
    if (outFile == null) {
        String outFileName = meta.apkFileName;
        outFile = new File(appDir, "dist" + File.separator + (outFileName == null ? "out.apk" : outFileName));
    }
    new File(appDir, APK_DIRNAME).mkdirs();
    buildSources(appDir);
    buildNonDefaultSources(appDir);
    File manifest = new File(appDir, "AndroidManifest.xml");
    File manifestOriginal = new File(appDir, "AndroidManifest.xml.orig");
    if (manifest.isFile() && manifest.exists()) {
        try {
            if (manifestOriginal.exists()) {
                manifestOriginal.delete();
            }
            FileUtils.copyFile(manifest, manifestOriginal);
            ResXmlPatcher.fixingPublicAttrsInProviderAttributes(manifest);
        } catch (IOException ex) {
            throw new AndrolibException(ex.getMessage());
        }
    }
    buildResources(appDir, meta.usesFramework);
    buildLib(appDir);
    buildLibs(appDir);
    buildCopyOriginalFiles(appDir);
    buildApk(appDir, outFile);
    // we must go after the Apk is built, and copy the files in via Zip
    // this is because Aapt won't add files it doesn't know (ex unknown files)
    buildUnknownFiles(appDir, outFile, meta);
    // lets restore the unedited one, to not change the original
    if (manifest.isFile() && manifest.exists()) {
        try {
            if (new File(appDir, "AndroidManifest.xml").delete()) {
                FileUtils.moveFile(manifestOriginal, manifest);
            }
        } catch (IOException ex) {
            throw new AndrolibException(ex.getMessage());
        }
    }
}
Also used : MetaInfo(brut.androlib.meta.MetaInfo) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 22 with ExtFile

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

the class Androlib method buildCopyOriginalFiles.

public void buildCopyOriginalFiles(File appDir) throws AndrolibException {
    if (apkOptions.copyOriginalFiles) {
        File originalDir = new File(appDir, "original");
        if (originalDir.exists()) {
            try {
                LOGGER.info("Copy original files...");
                Directory in = (new ExtFile(originalDir)).getDirectory();
                if (in.containsFile("AndroidManifest.xml")) {
                    LOGGER.info("Copy AndroidManifest.xml...");
                    in.copyToDir(new File(appDir, APK_DIRNAME), "AndroidManifest.xml");
                }
                if (in.containsDir("META-INF")) {
                    LOGGER.info("Copy META-INF...");
                    in.copyToDir(new File(appDir, APK_DIRNAME), "META-INF");
                }
            } catch (DirectoryException ex) {
                throw new AndrolibException(ex);
            }
        }
    }
}
Also used : ExtFile(brut.directory.ExtFile) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 23 with ExtFile

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

the class Androlib method buildNonDefaultSources.

public void buildNonDefaultSources(ExtFile appDir) throws AndrolibException {
    try {
        // loop through any smali_ directories for multi-dex apks
        Map<String, Directory> dirs = appDir.getDirectory().getDirs();
        for (Map.Entry<String, Directory> directory : dirs.entrySet()) {
            String name = directory.getKey();
            if (name.startsWith("smali_")) {
                String filename = name.substring(name.indexOf("_") + 1) + ".dex";
                if (!buildSourcesRaw(appDir, filename) && !buildSourcesSmali(appDir, name, filename)) {
                    LOGGER.warning("Could not find sources");
                }
            }
        }
        // loop through any classes#.dex files for multi-dex apks
        File[] dexFiles = appDir.listFiles();
        if (dexFiles != null) {
            for (File dex : dexFiles) {
                // skip classes.dex because we have handled it in buildSources()
                if (dex.getName().endsWith(".dex") && !dex.getName().equalsIgnoreCase("classes.dex")) {
                    buildSourcesRaw(appDir, dex.getName());
                }
            }
        }
    } catch (DirectoryException ex) {
        throw new AndrolibException(ex);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 24 with ExtFile

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

the class Androlib method buildManifest.

public boolean buildManifest(ExtFile appDir, UsesFramework usesFramework) throws BrutException {
    try {
        if (!new File(appDir, "AndroidManifest.xml").exists()) {
            return false;
        }
        if (!apkOptions.forceBuildAll) {
            LOGGER.info("Checking whether resources has changed...");
        }
        File apkDir = new File(appDir, APK_DIRNAME);
        if (apkOptions.forceBuildAll || isModified(newFiles(APK_MANIFEST_FILENAMES, appDir), newFiles(APK_MANIFEST_FILENAMES, apkDir))) {
            LOGGER.info("Building AndroidManifest.xml...");
            File apkFile = File.createTempFile("APKTOOL", null);
            apkFile.delete();
            File ninePatch = new File(appDir, "9patch");
            if (!ninePatch.exists()) {
                ninePatch = null;
            }
            mAndRes.aaptPackage(apkFile, new File(appDir, "AndroidManifest.xml"), null, ninePatch, null, parseUsesFramework(usesFramework));
            Directory tmpDir = new ExtFile(apkFile).getDirectory();
            tmpDir.copyToDir(apkDir, APK_MANIFEST_FILENAMES);
        }
        return true;
    } catch (IOException | DirectoryException ex) {
        throw new AndrolibException(ex);
    } catch (AndrolibException ex) {
        LOGGER.warning("Parse AndroidManifest.xml failed, treat it as raw file.");
        return buildManifestRaw(appDir);
    }
}
Also used : ExtFile(brut.directory.ExtFile) ZipFile(java.util.zip.ZipFile) ExtFile(brut.directory.ExtFile)

Example 25 with ExtFile

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

the class AndResGuardTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception, BrutException {
    TestUtils.cleanFrameworkFile();
    sTmpDir = new ExtFile(OS.createTempDirectory());
    TestUtils.copyResourceDir(LargeIntsInManifestTest.class, "brut/apktool/issue1170/", sTmpDir);
}
Also used : ExtFile(brut.directory.ExtFile)

Aggregations

ExtFile (brut.directory.ExtFile)31 ZipFile (java.util.zip.ZipFile)12 File (java.io.File)10 BeforeClass (org.junit.BeforeClass)8 Test (org.junit.Test)5 MetaInfo (brut.androlib.meta.MetaInfo)4 AndrolibException (brut.androlib.AndrolibException)2 BrutException (brut.common.BrutException)1 Directory (brut.directory.Directory)1 DirectoryException (brut.directory.DirectoryException)1 FileDirectory (brut.directory.FileDirectory)1