Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different resources (graphics and strings) for different application flavors using gradle

There are already several questions on this (different icons which didn't work and different names which did work), but the answers to those aren't yet quite enough to get my app working the way I need. I'm brand-new to Gradle, just started converting to it yesterday, and there are just a couple things I haven't been able to figure out.

I have 3+ apps working off the same codebase, each using different app launcher icons and names, URLs, but right now I can't get the different icons and app names to work for each app. Before I added the sourceSets, my appName and other custom strings worked, but the icon didn't, and now nothing works.

Inside my src directory, I have 4 directories: flav1, flav2, flav3, and main (contains all my core code and default resources). Android Studio has tacked .res onto each the end of each flav Each directory has a drawable and values directory with the custom strings.xml and icon.png, respectively, inside. I don't if drawable is supposed to have a specific name: drawable, drawable-mdpi, etc. If it's just whatever I decide, I want drawable.

I'm not getting any errors, just the default strings defined in main and the default icon Android Studio, except my default AndroidManifest.xml inside main won't recognize my icon and them anymore.

Here's my build.gradle:

apply plugin: 'android'

android {
   compileSdkVersion 19
   buildToolsVersion '19.0.2'

   defaultConfig {
      minSdkVersion 8
      targetSdkVersion 19
   }

   buildTypes {
      release {
         runProguard false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
      }
   }

   productFlavors {
      flav1 {
         packageName "com.example.flav1"
         versionCode 32
         versionName "1.0.5"
      }

      flav2 {
         packageName "com.example.flav2"
         versionCode 33
         versionName "1.0.6"
      }

      flav3 {
         packageName "com.example.flav3"
         versionCode 27
         versionName "1.0.0"
      }
   }

   sourceSets {
      main {
          // I don't understand how these are supposed to work
          java.srcDirs = ['java']
          resources.srcDirs = ['src']
          aidl.srcDirs = ['src']
          renderscript.srcDirs = ['src']
          // res.srcDirs = ['res']
          assets.srcDirs = ['assets']
      }

      flav1 {
          // I can't figure out the path to these
          res.srcDirs = ['res', '/flav1']
      }

      flav2 {
          res.srcDirs = ['res', '/flav2']
      }

      flav3 { 
          res.srcDirs = ['res', '/flav3']
      }
   }

   // I read that these are necessary, but I don't know how to use them
   sourceSets.flav1 {
      res {
          srcDir 'flav1'
      }
      resources {
          srcDir 'flav1'
      }
   }
   sourceSets.flav2 {
      res {
          srcDir 'flav2'
      }
      resources {
          srcDir 'flav2'
      }
   }
   sourceSets.flav3 {
      res {
          srcDir 'flav3'
      }
      resources {
          srcDir 'flav3'
      }
   }
}

dependencies {
   // code
}

I appreciate any help!

like image 346
craned Avatar asked Apr 22 '14 17:04

craned


1 Answers

If you can rearrange your files, that's the simplest way to fix this.

You want to put them like this:

yourApp/
  src/
    main/
      java/
      res/
    flav1/
      java/
      res/
    flav2/
      java/
      res/
    flav3/
      java/
      res/

Then in build.gradle, you can get rid of sourceSets entirely. Keep productFlavors.

As far as the overriding of resources, it should be automatic: "The build system merges all the resources from the all the source directories. If different folders contain resources with the same name for a build variant, the priority order is the following: build type resources override those from the product flavor, which override the resources in the main source directory."

like image 157
Sofi Software LLC Avatar answered Oct 23 '22 12:10

Sofi Software LLC