/**
   * Tests all options, checks if their default values are correct.
   *
   * @throws Exception
   */
  public void testDefaultConfig() throws Exception {
    ZipalignMojo mojo = createMojo("zipalign-config-project0");
    final ConfigHandler cfh = new ConfigHandler(mojo);
    cfh.parseConfiguration();

    Boolean skip = Whitebox.getInternalState(mojo, "parsedSkip");
    assertTrue("zipalign 'skip' parameter should be true", skip);

    Boolean verbose = Whitebox.getInternalState(mojo, "parsedVerbose");
    assertFalse("zipalign 'verbose' parameter should be false", verbose);

    MavenProject project = Whitebox.getInternalState(mojo, "project");

    String inputApk = Whitebox.getInternalState(mojo, "parsedInputApk");
    File inputApkFile =
        new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".apk");
    assertEquals(
        "zipalign 'inputApk' parameter should be equal", inputApkFile.getAbsolutePath(), inputApk);

    String outputApk = Whitebox.getInternalState(mojo, "parsedOutputApk");
    File outputApkFile =
        new File(
            project.getBuild().getDirectory(), project.getBuild().getFinalName() + "-aligned.apk");
    assertEquals(
        "zipalign 'outputApk' parameter should be equal",
        outputApkFile.getAbsolutePath(),
        outputApk);
  }
Example #2
0
  /**
   * @throws MojoExecutionException
   * @throws MojoFailureException
   */
  public void execute() throws MojoExecutionException, MojoFailureException {

    // Make an early exit if we're not supposed to generate the APK
    if (!generateApk) {
      return;
    }

    ConfigHandler cfh = new ConfigHandler(this, this.session, this.execution);

    cfh.parseConfiguration();

    generateIntermediateApk();

    // Compile resource exclusion patterns, if any
    if (excludeJarResources != null && excludeJarResources.length > 0) {
      getLog().debug("Compiling " + excludeJarResources.length + " patterns");

      excludeJarResourcesPatterns = new Pattern[excludeJarResources.length];

      for (int index = 0; index < excludeJarResources.length; ++index) {
        excludeJarResourcesPatterns[index] = Pattern.compile(excludeJarResources[index]);
      }
    }

    // Initialize apk build configuration
    File outputFile =
        new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + "." + APK);
    final boolean signWithDebugKeyStore = getAndroidSigner().isSignWithDebugKeyStore();

    if (getAndroidSigner().shouldCreateBothSignedAndUnsignedApk()) {
      getLog().info("Creating debug key signed apk file " + outputFile);
      createApkFile(outputFile, true);
      final File unsignedOutputFile =
          new File(
              project.getBuild().getDirectory(),
              project.getBuild().getFinalName() + "-unsigned." + APK);
      getLog().info("Creating additional unsigned apk file " + unsignedOutputFile);
      createApkFile(unsignedOutputFile, false);
      projectHelper.attachArtifact(
          project, unsignedOutputFile, classifier == null ? "unsigned" : classifier + "_unsigned");
    } else {
      createApkFile(outputFile, signWithDebugKeyStore);
    }

    if (classifier == null) {
      // Set the generated .apk file as the main artifact (because the pom states
      // <packaging>apk</packaging>)
      project.getArtifact().setFile(outputFile);
    } else {
      // If there is a classifier specified, attach the artifact using that
      projectHelper.attachArtifact(project, outputFile, classifier);
    }
  }
  /**
   * Tests all parameters parsing
   *
   * <p>Probably not needed since it is like testing maven itself
   *
   * @throws Exception
   */
  public void testConfigParse() throws Exception {
    ZipalignMojo mojo = createMojo("zipalign-config-project1");
    final ConfigHandler cfh = new ConfigHandler(mojo);
    cfh.parseConfiguration();

    Boolean skip = Whitebox.getInternalState(mojo, "parsedSkip");
    assertFalse("zipalign 'skip' parameter should be false", skip);

    Boolean verbose = Whitebox.getInternalState(mojo, "parsedVerbose");
    assertTrue("zipalign 'verbose' parameter should be true", verbose);

    String inputApk = Whitebox.getInternalState(mojo, "parsedInputApk");
    assertEquals("zipalign 'inputApk' parameter should be equal", "app.apk", inputApk);

    String outputApk = Whitebox.getInternalState(mojo, "parsedOutputApk");
    assertEquals("zipalign 'outputApk' parameter should be equal", "app-updated.apk", outputApk);
  }