Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

like image 978
Emil Adz Avatar asked Nov 12 '14 07:11

Emil Adz


1 Answers

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.

like image 142
pdegand59 Avatar answered Oct 19 '22 04:10

pdegand59