5

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!

Community
  • 1
  • 1
craned
  • 2,795
  • 2
  • 31
  • 38

1 Answers1

8

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

Sofi Software LLC
  • 3,516
  • 1
  • 31
  • 31
  • 1
    Thank you for your quick response! Through your answer, and other help, I was actually able to realize that just having the `drawable` in each flavor wasn't enough to overwrite it. So I added the other folders within res with the icon within them: `drawable-hdpi`, `drawable-ldpi`, etc. I don't actually know which one overwrote the default, but that gave me success. Am I right in assuming that the `java` directory would hold any custom code for a specific class or something for that particular flavor? – craned Apr 22 '14 at 20:37
  • One more question: what is meant by the `build type resources`? – craned Apr 22 '14 at 20:40
  • I am just full of questions today: Is there an easy way to extend this solution to include `debug` and `release` versions for each of the above flavors so I can specify different URLs for each? – craned Apr 22 '14 at 20:46
  • build type is something like release or debug. You already have a buildTypes for release in your example. Yes, you can define different properties for a build type. To do that, in addition to main and flav1 etc, you can have a directory debug/ and release/, each with their own res and java subdirs. – Sofi Software LLC Apr 23 '14 at 03:20
  • Yes, java would hold custom code. This is unusual, but not unknown. – Sofi Software LLC Apr 23 '14 at 03:20
  • Thanks for your answers! However, I'm a bit confused on how to create a debug and release version for each flavor. All I really need is a different url for each. I don't know how to use the release section of the BuildType to specify different urls for debug and release versions of each flavor, and the website isn't helpful there. Wouldn't a directory debug/ and release/ just create two new flavors, rather than two types for each already-existing flavor? Minus the java directories, what you show above is what i have. Do I make a debug and release directory for each flavor? Thanks again! – craned Apr 23 '14 at 20:36
  • You are talking about build variants: http://tools.android.com/tech-docs/new-build-system/build-system-concepts. In your environment they'd be named flav1Debug, flav1Release, etc. Also see http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Type-Product-Flavor-Build-Variant – Sofi Software LLC Apr 24 '14 at 00:26