예제 #1
0
  public void build(ExtFile appDir, File outFile) throws BrutException {
    LOGGER.info("Using Apktool " + Androlib.getVersion());

    Map<String, Object> meta = readMetaFile(appDir);
    Object t1 = meta.get("isFrameworkApk");
    apkOptions.isFramework = (t1 == null ? false : (Boolean) t1);
    apkOptions.resourcesAreCompressed =
        meta.get("compressionType") == null
            ? false
            : Boolean.valueOf(meta.get("compressionType").toString());

    mAndRes.setSdkInfo((Map<String, String>) meta.get("sdkInfo"));
    mAndRes.setPackageId((Map<String, String>) meta.get("packageInfo"));
    mAndRes.setPackageInfo((Map<String, String>) meta.get("packageInfo"));
    mAndRes.setVersionInfo((Map<String, String>) meta.get("versionInfo"));

    if (outFile == null) {
      String outFileName = (String) meta.get("apkFileName");
      outFile =
          new File(
              appDir, "dist" + File.separator + (outFileName == null ? "out.apk" : outFileName));
    }

    new File(appDir, APK_DIRNAME).mkdirs();
    buildSources(appDir);
    buildNonDefaultSources(appDir);
    buildResources(appDir, (Map<String, Object>) meta.get("usesFramework"));
    buildLib(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);
  }
예제 #2
0
  public boolean buildManifest(ExtFile appDir, Map<String, Object> 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.debugMode) {
        mAndRes.remove_application_debug(new File(apkDir, "AndroidManifest.xml").getAbsolutePath());
      }

      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);
    }
  }
예제 #3
0
 public ResPackage getPackage(int id) throws AndrolibException {
   ResPackage pkg = mPackagesById.get(id);
   if (pkg != null) {
     return pkg;
   }
   if (mAndRes != null) {
     return mAndRes.loadFrameworkPkg(this, id, mFrameTag);
   }
   throw new UndefinedResObject(String.format("package: id=%d", id));
 }
예제 #4
0
 public void buildApk(File appDir, File outApk) throws AndrolibException {
   LOGGER.info("Building apk file...");
   if (outApk.exists()) {
     outApk.delete();
   } else {
     File outDir = outApk.getParentFile();
     if (outDir != null && !outDir.exists()) {
       outDir.mkdirs();
     }
   }
   File assetDir = new File(appDir, "assets");
   if (!assetDir.exists()) {
     assetDir = null;
   }
   mAndRes.aaptPackage(outApk, null, null, new File(appDir, APK_DIRNAME), assetDir, null);
 }
예제 #5
0
  public boolean buildResourcesFull(File appDir, Map<String, Object> usesFramework)
      throws AndrolibException {
    try {
      if (!new File(appDir, "res").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(APP_RESOURCES_FILENAMES, appDir),
              newFiles(APK_RESOURCES_FILENAMES, apkDir))) {
        LOGGER.info("Building resources...");

        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"),
            new File(appDir, "res"),
            ninePatch,
            null,
            parseUsesFramework(usesFramework));

        Directory tmpDir = new ExtFile(apkFile).getDirectory();
        tmpDir.copyToDir(
            apkDir,
            tmpDir.containsDir("res")
                ? APK_RESOURCES_FILENAMES
                : APK_RESOURCES_WITHOUT_RES_FILENAMES);

        // delete tmpDir
        apkFile.delete();
      }
      return true;
    } catch (IOException | BrutException ex) {
      throw new AndrolibException(ex);
    }
  }
예제 #6
0
  private File[] parseUsesFramework(Map<String, Object> usesFramework) throws AndrolibException {
    if (usesFramework == null) {
      return null;
    }

    List<Integer> ids = (List<Integer>) usesFramework.get("ids");
    if (ids == null || ids.isEmpty()) {
      return null;
    }

    String tag = (String) usesFramework.get("tag");
    File[] files = new File[ids.size()];
    int i = 0;
    for (int id : ids) {
      files[i++] = mAndRes.getFrameworkApk(id, tag);
    }
    return files;
  }
예제 #7
0
 public void installFramework(File frameFile) throws AndrolibException {
   mAndRes.installFramework(frameFile);
 }
예제 #8
0
 public void publicizeResources(File arscFile) throws AndrolibException {
   mAndRes.publicizeResources(arscFile);
 }
예제 #9
0
 public ResTable getResTable(ExtFile apkFile, boolean loadMainPkg) throws AndrolibException {
   return mAndRes.getResTable(apkFile, loadMainPkg);
 }
예제 #10
0
 public ResTable getResTable(ExtFile apkFile) throws AndrolibException {
   return mAndRes.getResTable(apkFile, true);
 }
예제 #11
0
 public Androlib() {
   this.apkOptions = new ApkOptions();
   mAndRes.apkOptions = this.apkOptions;
 }
예제 #12
0
 public Androlib(ApkOptions apkOptions) {
   this.apkOptions = apkOptions;
   mAndRes.apkOptions = apkOptions;
 }
예제 #13
0
 public void decodeManifestWithResources(ExtFile apkFile, File outDir, ResTable resTable)
     throws AndrolibException {
   mAndRes.decodeManifestWithResources(resTable, apkFile, outDir);
 }
예제 #14
0
 public void decodeResourcesFull(ExtFile apkFile, File outDir, ResTable resTable)
     throws AndrolibException {
   mAndRes.decode(resTable, apkFile, outDir);
 }