简体   繁体   中英

Android project How to use/merge multiple AndroidManifest.xml

I want to split my project to multiple sourceSets, so I want use multiple AndroidManifest.xml to declare current sourceSets's component,util build apk merge the main AndroidManifest.xml, but there only support one main AndroidManifest.xml setup, so how can I do it?

Why I want to use multiple sourceSets? Because if using multiple modules it compile so slow.

This is my project settings

  def moduleDirs = project.projectDir.listFiles()
            .findAll {
        def name = it.name
        // filter all inner's module
        it.isDirectory() && msExtension.modulePrefix.any { name.startsWith(it) }
    }

    Set<File> manifestSet = new HashSet<>()

    Set<String> modules = new HashSet<>()
    def sourceSetConf = { AndroidSourceSet sourceSet ->
        moduleDirs.each {
            def moduleName = it.name
            def dir = sourceSet.name
            sourceSet.assets.srcDirs "${moduleName}/src/${dir}/assets"
            sourceSet.java.srcDirs "${moduleName}/src/${dir}/java"
            sourceSet.res.srcDirs "${moduleName}/src/${dir}/res"
            sourceSet.aidl.srcDirs "${moduleName}/src/${dir}/aidl"

            // each AndroidManifest.xml
            def manifestFile = project.file("${moduleName}/AndroidManifest.xml")
            if (manifestFile != null && manifestFile.exists()) {
                manifestSet.add(manifestFile)
            }

            modules.add(moduleName)
        }
    }

    // for default main sources
    sourceSetConf(android.sourceSets.main)

    // for buildType sources
    android.buildTypes.each {
        def buildType = it.name
        android.sourceSets.getByName(buildType) {
            sourceSetConf(it)
        }
    }

    // for flavor sources
    android.productFlavors.each {
        def flavor = it.name
        android.sourceSets.getByName(flavor) {
            sourceSetConf(it)
        }
    }

I see some code from gradle what multiple modules merge, but still no idea how to

ManifestMerger2
ProcessManifest

Use multiple modules via Android Studio. Create dependencies between modules. To make ModuleA depend on ModuleB , put the following inside build.gradle of ModuleA :

dependencies {
  // ...
  compile project(':ModuleB')
  // ...
}

This should merge AndroiManifest.xml from both modules when buiding ModuleA

I just use ManifestMerger2 to do it.

 private static void merge(File reportFile, File mainManifest, File... libraryManifests) {
    if (libraryManifests == null || libraryManifests.length == 0) {
        return
    }

    ILogger logger = new StdLogger(Level.VERBOSE)
    Invoker manifestMergerInvoker = ManifestMerger2.newMerger(mainManifest, logger, MergeType.APPLICATION)
    manifestMergerInvoker.setMergeReportFile(reportFile)
    manifestMergerInvoker.addLibraryManifests(libraryManifests)

    println "start merge..."

    MergingReport mergingReport = manifestMergerInvoker.merge()

    println "end merge..."

    if (MergingReport.Result.SUCCESS) {
        XmlDocument xmlDocument = mergingReport.getMergedXmlDocument(MergingReport.MergedManifestKind.MERGED)
        try {
            String annotatedDocument = mergingReport.getActions().blame(xmlDocument)
            logger.verbose(annotatedDocument)
        } catch (Exception e) {
            logger.error(e, "cannot print resulting xml")
        }
        save(xmlDocument, mainManifest)
    }
}

private static void save(XmlDocument xmlDocument, File out) {
    try {
        Files.write(xmlDocument.prettyPrint(), out, Charsets.UTF_8)
    } catch (IOException e) {
        throw new RuntimeException(e)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM