简体   繁体   English

Android项目如何使用/合并多个AndroidManifest.xml

[英]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? 我想将我的项目拆分为多个sourceSets,所以我想使用多个AndroidManifest.xml声明当前sourceSets的组件,使用build apk合并主要的AndroidManifest.xml,但是那里仅支持一个主要的AndroidManifest.xml设置,所以我该怎么办它?

Why I want to use multiple sourceSets? 为什么我要使用多个sourceSet? 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 我从gradle中看到了一些代码,其中多个模块合并了,但是仍然不知道如何

ManifestMerger2 清单合并2
ProcessManifest 工艺清单

Use multiple modules via Android Studio. 通过Android Studio使用多个模块。 Create dependencies between modules. 在模块之间创建依赖关系。 To make ModuleA depend on ModuleB , put the following inside build.gradle of ModuleA : 要使ModuleA依赖ModuleB ,请将以下内容放入ModuleA build.gradle中:

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

This should merge AndroiManifest.xml from both modules when buiding ModuleA AndroiManifest.xml ModuleA时,这应该合并两个模块的AndroiManifest.xml

I just use ManifestMerger2 to do it. 我只是使用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.

相关问题 在android studio的库项目中重用androidmanifest.xml - re-use the androidmanifest.xml in library project in android studio 单个Android项目中如何有两个AndroidManifest.xml文件 - How can it be two AndroidManifest.xml files on a single android project 一个项目有多个 AndroidManifest.xml 文件? - Multiple AndroidManifest.xml files for one project? 无法阅读AndroidManiFest.xml Android项目 - Unable to read AndroidManiFest.xml Android Project 如何在AndroidManifest.xml中的android:name中使用破折号和点字符? - How to use dash and dot characters in android:name in AndroidManifest.xml? 如何在Android Studio中验证AndroidManifest.xml? - How is AndroidManifest.xml validated in android studio? Android Studio:新的电视项目将合并行插入AndroidManifest.xml,格式不正确 - Android Studio: New tv project inserts merge lines into AndroidManifest.xml and is not well formed 如何在Android上以编程方式编译AndroidManifest.xml? - How to compile AndroidManifest.xml programmatically on Android? 如何获取 android 应用程序的 AndroidManifest.xml - How to get AndroidManifest.xml of an android app 使用git时,Android Studio不会将AndroidManifest.xml识别为项目的一部分 - Android studio doesn't recognize AndroidManifest.xml as part of project when use git
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM