簡體   English   中英

Android項目如何使用/合並多個AndroidManifest.xml

[英]Android project How to use/merge multiple AndroidManifest.xml

我想將我的項目拆分為多個sourceSets,所以我想使用多個AndroidManifest.xml聲明當前sourceSets的組件,使用build apk合並主要的AndroidManifest.xml,但是那里僅支持一個主要的AndroidManifest.xml設置,所以我該怎么辦它?

為什么我要使用多個sourceSet? 因為如果使用多個模塊,編譯速度會很慢。

這是我的項目設置

  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)
        }
    }

我從gradle中看到了一些代碼,其中多個模塊合並了,但是仍然不知道如何

清單合並2
工藝清單

通過Android Studio使用多個模塊。 在模塊之間創建依賴關系。 要使ModuleA依賴ModuleB ,請將以下內容放入ModuleA build.gradle中:

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

AndroiManifest.xml ModuleA時,這應該合並兩個模塊的AndroiManifest.xml

我只是使用ManifestMerger2來做到這一點。

 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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM