简体   繁体   English

配置特定的gradle风味组合

[英]Config for specific gradle flavor combination

I am wondering if it is possible to supply files or configuration settings for a specific flavor group combination . 我想知道是否可以为特定的风味组组合提供文件或配置设置。 For example, I have 2 flavor groups, with 2 different flavors each: 例如,我有2个风味组,每组有2种不同的风味:

Flavor Group: Version 风味组:版本

  • v2 V2
  • v3 V3

Flavor Group: Type 风味组:类型

  • free 自由
  • full 充分

When ordered "Type", "Version" I am able to build 4 different flavors of my app: 订购“类型”,“版本”时,我可以构建我的应用程序的4种不同风格:

  • FreeV2 FreeV2
  • FreeV3 FreeV3
  • FullV2 FullV2
  • FullV3 FullV3

And my source tree looks like this: 我的源代码树看起来像这样:

/src
    /free
        /res
    /full
        /res
    /v2
        /java
        /res
        AndroidManifest.xml (features and permissions for v2 flavors)
    /v3
        /java
        /res
        AndroidManifest.xml (features and permissions for v3 flavors)

This is exactly what I want, and works very well for my project. 这正是我想要的,对我的项目非常有效。 However, I wish I could provide files for a specific flavor group combination. 但是,我希望我能为特定的风味组合组合提供文件。 For example, I want to provide different AndroidManifests for FullV3 and FullV2. 例如,我想为FullV3和FullV2提供不同的AndroidManifests。 I don't think this is possible, or? 我不认为这是可能的,或者? For example: 例如:

/src
    /free
        /res
    /full
        /res
    /v2
        /java
        /res
        AndroidManifest.xml (features and permissions for v2 flavors)
    /v3
        /java
        /res
        AndroidManifest.xml (features and permissions for v3 flavors)
    /fullv3
        AndroidManifest.xml (features and permissions for full v3 only!)
    /fullv2
        AndroidManifest.xml (features and permissions for full v2 only!)

I would be nice to be able to do this in the gradle build file as well: 我也很高兴能够在gradle构建文件中执行此操作:

productFlavors {
    free {
        packageName ...
        flavorGroup "type"
    }
    full {
        packageName ...
        flavorGroup "type"
    }
    v2 {
        packageName ...
        flavorGroup "version"
    }
    v3 {
        packageName ...
        flavorGroup "version"
    }
    fullv2 {
        ...  <-- specifically for the full-v2 combination
    }
    fullv3 {
        ...  <-- specifically for the full-v3 combination
    }
}

Note: one solution would be to only have 1 flavor group and define the 4 flavors explicitly: 注意:一种解决方案是只有1个flavor组并明确定义4种flavor:

  • freeV2 freeV2
  • freeV3 freeV3
  • fullV2 fullV2
  • fullV3 fullV3

However, this is not a viable solution for me since I would have to duplicate all the version specific code for the free and full flavors of each version. 但是,这对我来说不是一个可行的解决方案,因为我必须复制每个版本的免费和完整版本的所有版本特定代码。

This functionality was added in version 0.7.0 of the Android plugin for Gradle 此功能是在GradleAndroid插件版本0.7.0中添加的

http://tools.android.com/tech-docs/new-build-system <-- As of 1/6/14, this link is active http://tools.android.com/tech-docs/new-build-system < - 截至2014年1月6日,此链接有效

You can now have a variant specific source folder if you have flavors. 如果您有味道,现在可以拥有变体特定的源文件夹。

  • Only for app (not library or test). 仅适用于app(不是库或测试)。 Name is src/flavorDebug/... or src/flavor1Flavor2Debug/ 名称是src/flavorDebug/...src/flavor1Flavor2Debug/
    • Note the camelcase naming, with lower case for first letter. 请注意camelcase命名,首字母为小写。
  • Its components (res, manifest, etc...) have higher priority than components from build type or flavors. 它的组件(res,manifest等)比构建类型或flavor的组件具有更高的优先级。
  • There is also a "flavor combination" source folder available when more than one flavor dimension is used. 当使用多个风味维度时,还有一个“风味组合”源文件夹可用。 For instance src/flavor1Flavor2/ 比如src/flavor1Flavor2/
    • Note that this is for all combinations of all dimensions. 请注意,这适用于所有维度的所有组合。
    • Its priority is higher than single-flavor sourcesets, but lower than build-types. 它的优先级高于单味源集,但低于构建类型。

Update 1/30/2014 2014年1月30日更新

IntelliJ IDEA v13.0.2 (Build 133.696) now supports Android Gradle Plugin changes made in 0.7.0 IntelliJ IDEA v13.0.2 (Build 133.696)现在支持在0.7.0中进行的Android Gradle Plugin更改

Files will be merged when the flavors come together. 当风味融合在一起时,文件将被合并。 So if you have a manifest in full and in v3 , they will be combined when fullV3 is generated. 因此,如果你有一个full的清单和v3的清单,它们将在生成fullV3时合并。 This way you can get the combo generated. 这样您就可以生成组合。

As per the question asked you want different AndroidManifest file for Full version of both v2 and v3. 根据问题,您需要不同的AndroidManifest文件,以获得v2和v3的完整版本。 So instead of going with flavors for full and free, you can use combination of flavors and buildTypes as per your convenience. 因此,您可以根据自己的方便使用flavor和buildType的组合,而不是使用完整和免费的口味。

For example 例如

I know full and free should be product flavors not build types but your requirement says that full and free are your buildTypes and v2 and v3 are your app flavors 我知道完全和免费应该是产品风格而不是构建类型但你的要求说完整和免费是你的buildTypes和v2和v3是你的应用程序风味

buildTypes {

  debug {
     runProguard false
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

  }

  full{
     runProguard false/true
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
     signingConfig signingConfigs.prodSigning // write your signingConfig

   }

  free{
     runProguard false/true
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
     signingConfig signingConfigs.prodSigning // write your signingConfig

    }
 }

 productFlavors{

   v2 {
     //required configuration for full version

   }

   v3 {
      //required configuration for full version

    }

  } 

Now your project structure should look like this . 现在你的项目结构应该是这样的。

  --src
    ---main
      ----java
      ----res
      ----Manifest
    ---full
      ----java
      ----res
      ----Manifest(Manifest you want for Full Version)
    ---free
      ----java
      ----res
      ----Manifest (Manifest you want for Free Version)
    ---v2
      ----java
      ----res
      ----Manifest
    ---v3
      ----java
      ----res
      ----Manifest

After Sync with Gradle this will give combinations of build types in build variant tab in left pane in AS and will create different tasks for the same. 在与Gradle同步之后,这将在AS的左窗格中的构建变量选项卡中提供构建类型的组合,并将为其创建不同的任务。

Go to Build Variant and generate the build from drop down according to your choice. 转到Build Variant并根据您的选择从下拉列表生成构建。

It will give you Build Variants like this 它将为您提供这样的Build Variants

在此输入图像描述

To set configuration for each flavor combination in your build file you could use the solution I proposed here: https://stackoverflow.com/a/26585241 要在构建文件中为每个flavor组合设置配置,您可以使用我在此处提出的解决方案: https//stackoverflow.com/a/26585241

You can then use and variantFilter to iterate through all flavor combinations and match on the name of each combination (just like the name used when creating "flavor combination" source folders where you can place specific resources as mentioned in the answer from @abest): 然后,您可以使用和variantFilter迭代所有风味组合并匹配每个组合的名称(就像创建“风味组合”源文件夹时使用的名称,您可以在其中放置@abest答案中提到的特定资源):

android.variantFilter { variant ->
    def flavorString = ""
    def flavors = variant.getFlavors()
    for (int i = 0; i < flavors.size(); i++) {
        flavorString += flavors[i].name;
    }
    if(flavorString.equalsIgnoreCase("fullv2")) {
        variant.getDefaultConfig().applicationId "com.example.fullv2"
    }

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

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