如何使用Gradle中的不同资源文件夹为每个flavor创建两个应用程序? [英] How to create two applications for each flavor with different assets folders in Gradle?

查看:498
本文介绍了如何使用Gradle中的不同资源文件夹为每个flavor创建两个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在开发一个有5种口味的应用程序,这是我认为重要的build.gradle文件的一部分:

Currently I'm working on an application that has 5 flavors and this is the part of my build.gradle files that matters:

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.

问题:我要求在资产文件夹中为每一种风格提供一组不同的资产文件一个不同的apk文件,但将具有相同的包名称。这些apk文件将作为相同的应用程序上传到Google Play,但是针对不同的地区。

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. Those apk files will be uploaded to the Google play as the same application but for different regions.

因此包名必须保持不变。所以基本上我需要创建一种机制,当它们每两个具有相同的包名但具有不同的资产文件夹时,而不是5种风格将创建10种风味。如何使用gradle完成?

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. How can this be done using gradle?

我尝试使用类似的BuildTypes来实现它:

I have tried implementing this using BuildTypes like so:

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

但由于某种原因,releaseAlt还会获取位于assets目录中的文件,而不是assetsalt目录中的文件。

But for some reason the releaseAlt also takes the files located in the assets directory and not the assetsalt directory.

推荐答案

你可以使用buildTypes。

You can use buildTypes for that.

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

现在文件层次结构:

你应该

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/

使用此文件层次结构,当您构建 flavor1Release 变体时,您将获得来自 flavor1 / assets / 的logo.png文件,但是当您构建 flavor1ReleaseAlt 变体时,此png将被替换来自 flavor1ReleaseAlt / assets / 文件夹。

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.

说明:

Gradle使用约定优于配置(默认情况下)。特别是在项目结构方面。当正在构建flavor1ReleaseAlt Variant时,Gradle(实际上是Android插件;))正在寻找一个名为flavor1ReleaseAlt /的文件夹,里面有一些资产,资源,java等...这些是Gradle可以为此Variant找到的最具体的应用资源。然后Gradle会为一些不太具体的应用资源寻找一个名为flavor1 /的文件夹。然后到一个名为releaseAlt /的更小的特定文件夹,最后到通用文件夹(main /).

Gradle is using conventions over configurations (by default). 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. Theses are the most specific app resources that Gradle could find for this Variant. Then Gradle will look for a folder simply called flavor1/ for some less specifics app resources. Then to an even lesser specific folder called releaseAlt/ and finally to the generic folder (main/).

不同的文件夹必须有非常严格的名称才能匹配变体查找:

The different folders have to have very strict names in order to match the Variant lookup :


  • flavorBuildType /。 (订单很重要)。

  • flavor /

  • buildType /

  • main /

  • flavorBuildType/. (order is important).
  • flavor/
  • buildType/
  • main/

希望这有帮助。

这篇关于如何使用Gradle中的不同资源文件夹为每个flavor创建两个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆