简体   繁体   English

生命周期Android插件Gradle

[英]Lifecycle Android plugin gradle

I am looking for a way to include flavors dynamically 我正在寻找一种动态包含风味的方法

To do so, I try to include subproject with attributes to configure the main Android project. 为此,我尝试包含具有属性的子项目以配置主要的Android项目。 Here is my example: 这是我的示例:

app
|-> src
|    |-> flavor
|           |-> build.gradle
|-> build.gradle
|-> settings.gradle

app/settings.gradle 应用程序/ settings.gradle

include ':src:flavor'

app/src/flavor/build.gradle 应用程序/ src目录/风味/的build.gradle

ext {
    isFlavor = true
}

app/build.gradle 应用程序/的build.gradle

...

android {

    ...

    productFlavors {

        getAllprojects().each { project ->
            if (project.ext.has("isFlavor")) {
                def name = project.getName()
                "$name" {
                    applicationId "com.example.app.$name"
                }
            }
        }
    }

}

This looks amazing.. But it doesn't work! 这看起来很棒。。但是它不起作用! Because Android is configured before gradle checks the subprojects and flavor.ext.isFlavor doesn't exists yet. 因为Android是在gradle检查子项目之前配置的,所以flavor.ext.isFlavor还不存在。 And if I use configuration injection, Android spit on my face that I wouldn't dare! 而且,如果我使用配置注入,Android会吐我不敢吐!

Android tasks have already been created

I have read on stackoverflow that I have to create my attributes in my rootProject and configure Android in each subprojects. 我已经阅读了stackoverflow,我必须在rootProject中创建属性并在每个子项目中配置Android。

But in my case subprojects aren't projects but flavors.. 但就我而言,子项目不是项目而是风味。

Does anyone has an idea to configure attribute in subprojects and uses them in the rootProject ? 有没有人有想法在子项目中配置属性并在rootProject中使用它们?

I search near the apply from: feature right now.. 我现在在靠近“ apply from:功能附近进行搜索。

PS: Here is my main thread with an other solution: Gradle dynamic flavor PS:这是我的另一种解决方案的主线: Gradle动态风格

I just used a conditional and apply from: to force the lifecycle to apply my values: 我只是使用了一个条件,然后apply from:来强制生命周期应用我的值:

if (new File(project(":app").getProjectDir().getPath() + "/flavors.gradle").exists()) {
    apply from: "flavors.gradle"
}

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

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