简体   繁体   English

有没有办法为 Android 中的不同构建变体配置维护不同的 .gradle 文件?

[英]Is there a way to maintain different .gradle files for different build variant configurations in Android?

My project has multiple build variants and each build variant has 15-20 buildConfigFields which can increase further.我的项目有多个构建变体,每个构建变体都有15-20 个可以进一步增加的buildConfigFields Because of which my build.gradle (app level) file is getting bigger so I need to traverse/search whole file to look for a specific build variant.因此,我的 build.gradle(应用程序级别)文件变得越来越大,因此我需要遍历/搜索整个文件以查找特定的构建变体。

I'm looking for a solution where I can have different files for each build variant and I can easily import them into my build.gradle (app level).我正在寻找一种解决方案,我可以为每个构建变体使用不同的文件,并且可以轻松地将它们导入到我的 build.gradle(应用程序级别)中。

You can also share if any other recommended solutions for this if available.如果可用,您还可以分享是否有任何其他推荐的解决方案。

The whole project would be like this:整个项目应该是这样的:

Library build.gradle:库 build.gradle:

apply plugin: 'com.android.library'

android {        
    ....
    publishNonDefault true
    productFlavors {
        market1 {}
        market2 {}
    }
}

project build.gradle:项目 build.gradle:

apply plugin: 'com.android.application'

android {
    ....
    productFlavors {
        market1 {}
        market2 {}
    }
}

dependencies {
    ....
    market1Compile project(path: ':lib', configuration: 'market1Release')
    market2Compile project(path: ':lib', configuration: 'market2Release')
}

Now you can select the app flavor and Build Variants panel and the library will be selected accordingly and all build and run will be done based on the selected flavor.现在您可以选择应用程序风格和构建变体面板,将相应地选择库,所有构建和运行都将根据所选风格完成。

If you have multiple app modules based on the library Android Studio will complain about Variant selection conflict, It's ok, just ignore it.如果你有多个基于库的应用模块,Android Studio 会抱怨 Variant selection 冲突,没关系,忽略它。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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