Skip to content

Commit

Permalink
optimize AndroidManifest merge as library
Browse files Browse the repository at this point in the history
  • Loading branch information
kezong committed Jul 8, 2019
1 parent 71e0084 commit c76300c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FatLibraryPlugin implements Plugin<Project> {
public static final String ARTIFACT_TYPE_JAR = 'jar'

private Project project

private Configuration embedConf

private Set<ResolvedArtifact> artifacts
Expand Down
19 changes: 3 additions & 16 deletions source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,18 @@ class VariantProcessor {

/**
* merge manifest
*
* TODO process each variant.getOutputs()
* TODO "InvokeManifestMerger" deserve more android plugin version check
* TODO add setMergeReportFile
* TODO a better temp manifest file location
*/
private void processManifest() {
Class invokeManifestTaskClazz = null
String className = 'com.android.build.gradle.tasks.InvokeManifestMerger'
try {
invokeManifestTaskClazz = Class.forName(className)
} catch (ClassNotFoundException ignored) {
}
if (invokeManifestTaskClazz == null) {
throw new RuntimeException("Can not find class ${className}!")
}
Task processManifestTask = getProcessManifest()
File manifestOutputBackup
if (mGradlePluginVersion != null && Utils.compareVersion(mGradlePluginVersion, "3.3.0") >= 0) {
manifestOutputBackup = mProject.file("${mProject.buildDir.path}/intermediates/library_manifest/${mVariant.name}/AndroidManifest.xml")
} else {
manifestOutputBackup = mProject.file(processManifestTask.getManifestOutputDirectory().absolutePath + '/AndroidManifest.xml')
}

InvokeManifestMerger manifestsMergeTask = mProject.tasks.create('merge' + mVariant.name.capitalize() + 'Manifest', invokeManifestTaskClazz)
InvokeManifestMerger manifestsMergeTask = mProject.tasks.create("merge${mVariant.name.capitalize()}Manifest", LibraryManifestMerger.class)
manifestsMergeTask.setGradleVersion(mProject.getGradle().getGradleVersion())
manifestsMergeTask.setGradlePluginVersion(mGradlePluginVersion)
manifestsMergeTask.setVariantName(mVariant.name)
manifestsMergeTask.setMainManifestFile(manifestOutputBackup)
List<File> list = new ArrayList<>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.w3c.dom.Element;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down
78 changes: 78 additions & 0 deletions source/src/main/java/com/kezong/fataar/LibraryManifestMerger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.kezong.fataar;

import com.android.build.gradle.internal.LoggerWrapper;
import com.android.build.gradle.tasks.InvokeManifestMerger;
import com.android.manifmerger.ManifestMerger2;
import com.android.manifmerger.ManifestProvider;
import com.android.manifmerger.MergingReport;
import com.android.utils.ILogger;

import org.apache.tools.ant.BuildException;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* ManifestMerger for Library
* @author yangchao on 2019/7/8.
*/
public class LibraryManifestMerger extends InvokeManifestMerger {

private String mGradlePluginVersion;

private String mGradleVersion;

public void setGradlePluginVersion(String gradlePluginVersion) {
mGradlePluginVersion = gradlePluginVersion;
}

public void setGradleVersion(String gradleVersion) {
mGradleVersion = gradleVersion;
}

@TaskAction
protected void doFullTaskAction() throws ManifestMerger2.MergeFailureException, IOException {
try {
ILogger iLogger = new LoggerWrapper(getLogger());
ManifestMerger2.Invoker mergerInvoker = ManifestMerger2.
newMerger(getMainManifestFile(), iLogger, ManifestMerger2.MergeType.LIBRARY);
List<File> secondaryManifestFiles = getSecondaryManifestFiles();
List<ManifestProvider> manifestProviders = new ArrayList<>();
for (final File file : secondaryManifestFiles) {
manifestProviders.add(new ManifestProvider() {
@Override
public File getManifest() {
return file.getAbsoluteFile();
}

@Override
public String getName() {
return file.getName();
}
});
}
mergerInvoker.addManifestProviders(manifestProviders);
MergingReport mergingReport = mergerInvoker.merge();
if (mergingReport.getResult().isError()) {
getLogger().error(mergingReport.getReportString());
mergingReport.log(iLogger);
throw new BuildException(mergingReport.getReportString());
}
try (FileWriter fileWriter = new FileWriter(getOutputFile())) {
fileWriter.append(mergingReport
.getMergedDocument(MergingReport.MergedManifestKind.MERGED));
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Gradle Plugin Version:" + mGradlePluginVersion);
System.out.println("Gradle Version:" + mGradleVersion);
System.out.println("If you see this error message, please submit issue to " +
"https://github.com/kezong/fat-aar-android/issues with Gradle version. Thank you.");
super.doFullTaskAction();
}
}
}

0 comments on commit c76300c

Please sign in to comment.