Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gradle library resources in product flavor

Updated to gradle 1.12, gradle plugin for android studio 0.10.

My project has the following structure:

  • 3rdparty

    • Graphics
      • Iconsets
        • IconsetBase (android-library)
        • Iconset1 (android-library)
        • Iconset2 (android-library)
  • MainProject

    • src
      • main (main project)
      • flavor1
      • flavor2
      • ...

How to get the res folder contents of IconsetBase + Iconset1 to be merged into flavor1 and IconsetBase + Iconset2 into flavor2?

Before upgrading to new gradle this worked as the libraries (IconsetBase, Iconset1 and Iconset2) had the same package name as main

Here is my build.gradle of the main project:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        repositories {
            mavenCentral()
        }
        classpath 'com.android.tools.build:gradle:0.10.0'
    }
}

apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    useOldManifestMerger false
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    lintOptions {
       ...
    } 

sourceSets {
    main.java.srcDirs = ['src/main/java']
    main.resources.srcDirs = ['src/main/res']
}

signingConfigs {
    ...
}

buildTypes {
    ...
}

// Common dependencies
dependencies {
   compile project(':3rdparty:Graphics:Iconsets:IconsetBase')
}

defaultConfig {
    ...
}

productFlavors {
    flavor1         { packageName "..."}
    flavor2         { packageName "..."}
}

android.sourceSets.flavor1 {
    dependencies { compile project(':3rdparty:Graphics:Iconsets:Iconset1') }
    res { srcDir 'flavor1' }
    resources { srcDir 'flavor1' }
}
android.sourceSets.flavor2 {
    dependencies { compile project(':3rdparty:Graphics:Iconsets:Iconset2') }
    res { srcDir 'flavor2' }
    resources { srcDir 'flavor2' }
}
}

dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.android.support:appcompat-v7:19.1.0'
    compile 'com.google.android.gms:play-services:4.3.23'
}

=EDIT=

Further explanation:

What I seek is to merge resources from another location dynamically.

Background: flavor1 has base icon set with images icon1 and icon2, flavor2 has base iconset and icon1 and icon2 also, but the icons are different per icon set. Otherwise I would have to have icon1, icon2 etc. per flavor many times, and on updates /changes of those icons It must be done for every flavor existing (I have more than 20)

Could this be achieved with some custom task without libraries?

like image 860
ramden Avatar asked Mar 20 '23 13:03

ramden


1 Answers

You can define a specific dependency by flavor :

productFlavors {
    flavor1 {
       ...
       dependencies {
         flavor1Compile project(':iconSet1')
       }
    }
    flavor2 {
       ...
       dependencies {
         flavor2Compile project(':iconSet2')
       }
    }
 }

In your build the dependencies are in the SourceSets and that's wrong.


EDIT

Ok, I hope I better understand your goal now. What you can do is defining multiple resource directory for each flavor :

android {
    sourceSets {
        flavor1 {
            res.srcDirs = ['flavor1','../3rdparty/Graphics/Iconsets/Iconset1/res']
        }
        flavor2 {
            res.srcDirs = ['flavor2','../3rdparty/Graphics/Iconsets/Iconset2/res']
        }
    }

}
like image 194
ben75 Avatar answered Mar 22 '23 09:03

ben75