简体   繁体   English

如何使用Gradle中的不同资源文件夹为每个flavor创建两个应用程序?

[英]How to create two applications for each flavor with different assets folders in Gradle?

Currently I'm working on an application that has 5 flavors and this is the part of my build.gradle files that matters: 目前我正在开发一个有5种口味的应用程序,这是我的build.gradle文件的一部分:

buildscript {
   repositories {
       mavenCentral()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:0.14.0'
   }
}

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

    signingConfigs {
        release {
            storeFile file("")
            storePassword "password"
            keyAlias "alias"
            keyPassword "password"
        }
    }

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 20

        applicationId 'application.package'
        signingConfig signingConfigs.release
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }

    productFlavors {
        flavor1{
            applicationId 'com.flavor1.package'
        }
        flavor2{
            applicationId 'com.flavor2.package'
        }
        flavor3{
            applicationId 'com.flavor3.package'
        }
        flavor4{
            applicationId 'com.flavor4.package'
        }
        flavor5{
            applicationId 'com.flavor5.package'
        }
    }
}

dependencies {
    compile project(':SDK')
}

I had to make some changes in the file, but basically this is it. 我不得不在文件中做一些更改,但基本上就是这样。

The question: I have a requirement to provide for each one of those flavors a different set of assets files in the assets folder that will create a different apk file but will have the same package name. 问题是:我要求在assets文件夹中为每一种风格提供一组不同的资产文件,这些文件将创建一个不同的apk文件,但具有相同的包名。 Those apk files will be uploaded to the Google play as the same application but for different regions. 这些apk文件将作为相同的应用程序上传到Google Play,但针对不同的地区。

So the package name have to stay the same. 所以包名必须保持不变。 So basically I need to create a mechanism that instead of 5 flavors will created 10 flavor when every two of them have the same package name but a different assets folder. 所以基本上我需要创建一种机制,当它们中的每两个具有相同的包名但具有不同的资产文件夹时,而不是5种风格将创建10种风味。 How can this be done using gradle? 如何使用gradle完成?

I have tried implementing this using BuildTypes like so: 我尝试使用BuildTypes实现这一点,如下所示:

buildTypes {
    release {
        signingConfig signingConfigs.release
        sourceSets.main.assets.srcDirs = ['assets']
    }
    releaseAlt {
        signingConfig signingConfigs.release
        sourceSets.main.assets.srcDirs = ['assetsalt']
    }
}

But for some reason the releaseAlt also takes the files located in the assets directory and not the assetsalt directory. 但由于某种原因,releaseAlt还会获取位于assets目录中的文件,而不是assetsalt目录。

You can use buildTypes for that. 您可以使用buildTypes。

buildTypes {
  release {
    // ... the usual stuff here
  }
  releaseAlt {
    // .. the usual stuff here too like signing config etc...
  }
}

Now the file hierarchy : 现在文件层次结构:

You should have 你应该有

project/
- app/
 - src/
  - main/
   - assets/
    - logo.png // Generic assets go here
   - java/
   - res/
   - ...

  - flavor1/
   - assets/
    - logo.png // Specific assets for all the flavor1 Variants

  - releaseAlt/
   - asset/
    - logo.png // Specific assets for all the releaseAlt Variants.

  - flavor1ReleaseAlt/
   - assets/
    - logo.png // very specific assets for the flavor1ReleaseAlt Variant
- SDK/

With this file hierarchy, when you build the flavor1Release variant, you will have the logo.png file from flavor1/assets/ , but when you will build the flavor1ReleaseAlt variant, this png will be replaced by the on from flavor1ReleaseAlt/assets/ folder. 使用此文件层次结构,当您构建flavor1Release变体时,您将获得来自flavor1/assets/的logo.png文件,但是当您构建flavor1ReleaseAlt变体时,此png将替换为来自flavor1ReleaseAlt/assets/文件夹的on。

Explanation : 说明:

Gradle is using conventions over configurations (by default). Gradle使用约定而不是配置(默认情况下)。 Especially when it comes to project structure. 特别是在项目结构方面。 When the flavor1ReleaseAlt Variant is being built, Gradle (the Android-plugin actually ;) ) is looking for a folder called flavor1ReleaseAlt/ with some assets, resources, java etc... inside. 当构建flavor1ReleaseAlt Variant时,Gradle(实际上是Android插件;))正在寻找一个名为flavor1ReleaseAlt /的文件夹,里面有一些资产,资源,java等... Theses are the most specific app resources that Gradle could find for this Variant. 这些是Gradle可以为此Variant找到的最具体的应用资源。 Then Gradle will look for a folder simply called flavor1/ for some less specifics app resources. 然后Gradle会为一些不太具体的应用资源寻找一个名为flavor1 /的文件夹。 Then to an even lesser specific folder called releaseAlt/ and finally to the generic folder (main/). 然后到一个名为releaseAlt /的更小的特定文件夹,最后到通用文件夹(main /)。

The different folders have to have very strict names in order to match the Variant lookup : 不同的文件夹必须具有非常严格的名称才能匹配Variant查找:

  • flavorBuildType/. flavorBuildType /。 (order is important). (顺序很重要)。
  • flavor/ 味道/
  • buildType/ buildType /
  • main/ 主要/

Hope this helps. 希望这可以帮助。

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

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