Gradle: How to create two applications for each flavor with different resource folders:

I am currently working on an application that has 5 options, and this is part of my build.gradle files that matter:

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 to the file, but mostly it.

Question: I have a requirement to provide each of these flavors with a different set of asset files in the resource folder, which will create a different apk file but have the same package name. These apk files will be uploaded to the Google game as the same application, but for different regions. Therefore, the package name must remain unchanged. So basically I need to create a mechanism that instead of 5 flavors will create 10 flavors when each of them will have the same package name, but in a different folder with resources. How can this be done using gradle?

I tried to implement this using BuildTypes, for example:

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

But for some reason, releaseAlt also accepts files located in the assets directory rather than in the assetsalt directory. Can someone point me in the right direction regarding this? Thanks in advance.

+6
source share
1 answer

You can use buildTypes for this.

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

Now the file hierarchy:

You must 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 hierarchy of files, when you create the flavor1Release variant, you will have the logo.png file from flavor1/assets/ , but when you build the flavor1ReleaseAlt variant, this png will be replaced by flavor1ReleaseAlt/assets/ .

Explanation:

Gradle uses configuration conventions (default). Especially when it comes to project structure. When the flavor1ReleaseAlt Variant constructor is created, Gradle (actually, an Android plugin;)) looks for a folder called flavor1ReleaseAlt / with some resources, resources, java, etc. Inside. Abstracts are the most specific application resources that Gradle can find for this option. Gradle will then search for a folder simply called flavor1 / for some less specific application resources. Then to an even less specific folder named releaseAlt / and finally to a shared folder (main /).

Different folders must have very strong names to match the search option:

  • flavorBuildType /. (order is important).
  • taste /
  • buildType /
  • Primary /

Hope this helps.

+16
source

Source: https://habr.com/ru/post/978070/


All Articles