/** * Extracts the package name from an apk file. * * @param apkFile apk file to extract package name from. * @return the package name from inside the apk file. */ protected String extractPackageNameFromApk(File apkFile) throws MojoExecutionException { CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor(); executor.setLogger(this.getLog()); executor.setCaptureStdOut(true); executor.setCaptureStdErr(true); AaptCommandBuilder commandBuilder = AaptCommandBuilder.dump(getLog()) .xmlTree() .setPathToApk(apkFile.getAbsolutePath()) .addAssetFile("AndroidManifest.xml"); getLog().info(getAndroidSdk().getAaptPath() + " " + commandBuilder.toString()); try { executor.executeCommand(getAndroidSdk().getAaptPath(), commandBuilder.build(), false); final String xmlTree = executor.getStandardOut(); return extractPackageNameFromAndroidManifestXmlTree(xmlTree); } catch (ExecutionException e) { throw new MojoExecutionException( "Error while trying to figure out package name from inside apk file " + apkFile); } finally { String errout = executor.getStandardError(); if ((errout != null) && (errout.trim().length() > 0)) { getLog().error(errout); } } }
/** * Executes aapt to generate the R class for the given apklib. * * @param apklibArtifact apklib for which to generate the R class. * @throws MojoExecutionException if it fails. */ private void generateRForApkLibDependency(Artifact apklibArtifact) throws MojoExecutionException { final File unpackDir = getUnpackedLibFolder(apklibArtifact); getLog() .debug( "Generating incomplete R file for apklib: " + apklibArtifact.getGroupId() + ":" + apklibArtifact.getArtifactId()); final File apklibManifest = new File(unpackDir, "AndroidManifest.xml"); final File apklibResDir = new File(unpackDir, "res"); List<File> dependenciesResDirectories = new ArrayList<File>(); final Set<Artifact> apklibDeps = getDependencyResolver().getLibraryDependenciesFor(project, apklibArtifact); getLog().debug("apklib=" + apklibArtifact + " dependencies=" + apklibDeps); for (Artifact dependency : apklibDeps) { // Add in the resources that are dependencies of the apklib. final String extension = dependency.getType(); final File dependencyResDir = getUnpackedLibResourceFolder(dependency); if ((extension.equals(APKLIB) || extension.equals(AAR)) && dependencyResDir.exists()) { dependenciesResDirectories.add(dependencyResDir); } } // Create combinedAssets for this apklib dependency - can't have multiple -A args final File apklibCombAssets = new File(getUnpackedLibFolder(apklibArtifact), "combined-assets"); for (Artifact dependency : apklibDeps) { // Accumulate assets for dependencies of the apklib (if they exist). final String extension = dependency.getType(); final File dependencyAssetsDir = getUnpackedLibAssetsFolder(dependency); if ((extension.equals(APKLIB) || extension.equals(AAR))) { copyFolder(dependencyAssetsDir, apklibCombAssets); } } // Overlay the apklib dependency assets (if they exist) final File apkLibAssetsDir = getUnpackedLibAssetsFolder(apklibArtifact); copyFolder(apkLibAssetsDir, apklibCombAssets); final CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor(); executor.setLogger(getLog()); AaptCommandBuilder commandBuilder = new AaptCommandBuilder() .packageResources() .makeResourcesNonConstant() .makePackageDirectories() .setWhereToOutputResourceConstants(genDirectory.getAbsolutePath()) .generateRIntoPackage(extractPackageNameFromAndroidManifest(apklibManifest)) .setPathToAndroidManifest(apklibManifest.getAbsolutePath()) .addResourceDirectoryIfExists(apklibResDir) .addResourceDirectoriesIfExists(dependenciesResDirectories) .autoAddOverlay() .addRawAssetsDirectoryIfExists(apklibCombAssets) .addExistingPackageToBaseIncludeSet(getAndroidSdk().getAndroidJar().getAbsolutePath()) .addConfigurations(configurations) .addExtraArguments(aaptExtraArgs) .setVerbose(aaptVerbose) // We need to generate R.txt for all projects as it needs to be consumed when generating // R class. // It also needs to be consumed when packaging aar. .generateRTextFile(unpackDir.getAbsolutePath()); getLog().debug(getAndroidSdk().getAaptPath() + " " + commandBuilder.toString()); try { executor.setCaptureStdOut(true); List<String> commands = commandBuilder.build(); executor.executeCommand(getAndroidSdk().getAaptPath(), commands, project.getBasedir(), false); } catch (ExecutionException e) { throw new MojoExecutionException("", e); } }