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

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

问题描述

目前我正在研究一个具有5种风格的应用程序,这是我的build.gradle文件的一部分:

  buildscript {
repositories {
mavenCentral()
}
依赖关系{
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( )
storePasswordpassword
keyAlias别名
keyPassword密码
}
}

lintOptions {
abortOnError false
}

defaultConfig {
minSdkVersion 14
targetSdkVersion 20

applicationId'application.package'
signedConfig 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'
}
}
}

依赖关系{
编译项目(':SDK')
}

我必须对文件进行一些更改,但基本上就是这样。



问题:我需要为资产文件夹中的每一种风格提供一组不同的资产文件,以创建一个不同的apk文件,但将具有相同的包名称。这些apk文件将作为相同的应用程序上传到Google play上,但不同地区。所以包名必须保持不变。所以基本上我需要创建一个机制,而不是5个风味时,他们每两个具有相同的包名称,但不同的资产文件夹时会创建10个风味。如何使用gradle完成这项工作?



我尝试过使用BuildTypes来实现这个功能:

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

但由于某些原因,releaseAlt也会将资产目录中的文件而不是assetsalt目录中的文件。有人能指出我对这个方向是正确的吗?

解决方案

您可以使用buildTypes。

  buildTypes {
release {
// ...这里常见的东西
}
releaseAlt {
// ..这里常见的东西,如签署配置等...
}
}

现在文件层次结构:



您应该有

 项目/ 
- app /
- src /
- main /
- 资产/
- logo.png //通用资产转到此处
- java /
- res /
- ...

- flavor1 /
- 资产/
- logo.png //所有flavor1的特定资产变量

- releaseAlt /
- 资产/
- logo.png //所有releaseAlt变体的特定资产。

- flavor1ReleaseAlt /
- assets /
- logo.png //非常具体的flavor1ReleaseAlt变体
- SDK /

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



解释:



Gradle通过配置使用约定(默认情况下)。特别是涉及到项目结构。当构建flavor1ReleaseAlt Variant时,Gradle(Android插件实际上;))正在寻找名为flavor1ReleaseAlt /的文件夹,其中包含一些资产,资源,Java等等。这些是Gradle可以为这个Variant找到的最具体的应用程序资源。然后,Gradle将寻找一个简单地称为flavor1 /的文件夹,用于一些不太具体的应用程序资源。然后到一个名为releaseAlt /的较小特定文件夹,最后到通用文件夹(main /)。



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


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

  • flavor /

  • buildType /

  • main /



希望这有助于您。


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.

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. 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?

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']
    }
}

But for some reason the releaseAlt also takes the files located in the assets directory and not the assetsalt directory. Could someone point me in the right direction regarding this one? Thanks in advance.

解决方案

You can use buildTypes for that.

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.

Explanation :

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/. (order is important).
  • flavor/
  • buildType/
  • main/

Hope this helps.

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

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