diff --git a/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy b/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy index 88511704..747880dc 100644 --- a/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy +++ b/source/src/main/groovy/com/kezong/fataar/FatLibraryPlugin.groovy @@ -21,6 +21,7 @@ class FatLibraryPlugin implements Plugin { public static final String ARTIFACT_TYPE_JAR = 'jar' private Project project + private Configuration embedConf private Set artifacts diff --git a/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy b/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy index abf95d7f..6c549064 100644 --- a/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy +++ b/source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy @@ -135,22 +135,8 @@ 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) { @@ -158,8 +144,9 @@ class VariantProcessor { } 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 list = new ArrayList<>() diff --git a/source/src/main/java/com/kezong/fataar/AndroidArchiveLibrary.java b/source/src/main/java/com/kezong/fataar/AndroidArchiveLibrary.java index 92558329..5d1d688a 100644 --- a/source/src/main/java/com/kezong/fataar/AndroidArchiveLibrary.java +++ b/source/src/main/java/com/kezong/fataar/AndroidArchiveLibrary.java @@ -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; diff --git a/source/src/main/java/com/kezong/fataar/LibraryManifestMerger.java b/source/src/main/java/com/kezong/fataar/LibraryManifestMerger.java new file mode 100644 index 00000000..6a0eed65 --- /dev/null +++ b/source/src/main/java/com/kezong/fataar/LibraryManifestMerger.java @@ -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 secondaryManifestFiles = getSecondaryManifestFiles(); + List 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(); + } + } +}