Multi-flavor variants and dependencies

4,316 views
Skip to first unread message

Paweł Stankowski

unread,
Mar 22, 2014, 3:06:49 PM3/22/14
to adt...@googlegroups.com
I have two flavor groups: "api" (froyo and gingerbread flavors) and "variant" (free and pro flavors).

How to add dependency only to one build variant, ie only for froyo-free version? 'froyoFreeCompile' is not recognized by gradle (both freeCompile and froyoCompile are working as expected). If this is not possible directly, may I add such dependency using customized task?

My build.gradle:

...
    flavorGroups "variant", "api"

    productFlavors {
        free {
            flavorGroup "variant"
            packageName "..."
        }
        pro {
            flavorGroup "variant"
            packageName "..."
        }

        froyo {
            flavorGroup "api"
            minSdkVersion 8
        }
        gingerbread {
            flavorGroup "api"
            minSdkVersion 9
        }
    }
}

dependencies {
    freeFroyoCompile files(getSdkDir() + '/extras/google/admob_ads_sdk/GoogleAdMobAdsSdk-6.4.1.jar')
    freeGingerbreadCompile 'com.google.android.gms:play-services:+'
}

...

Croc

unread,
Mar 27, 2014, 10:37:07 AM3/27/14
to adt...@googlegroups.com
"froyoFreeCompile" is probably not recognized because I think you used the wrong order of flavors. The order should be the same as was declared in "flavorGroups", so it should actually be "freeFroyoCompile".

Xavier Ducrohet

unread,
Mar 27, 2014, 11:34:43 AM3/27/14
to adt...@googlegroups.com
It's not directly possible right now due to some issue in how we setup the tasks and variants. This should be possible in the long term though.

Right now you could do something like this:

configurations {
  freeFroyoCompile
}

dependencies {
  freeFroyoCompile ...
}

android.applicationVariants.all { variant ->
  if (variant is freeFroyo) {
    variant.javaCompile.classpath += freeFroyoCompile.files
  }
}




--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Paweł Stankowski

unread,
Apr 2, 2014, 5:01:45 PM4/2/14
to adt...@googlegroups.com
You're right Croc, I made a mistake in problem's description - it should be "freeFroyoCompile". However, I wrote it correctly in my gradle script.

Thank you Xavier, it almost work for me. Your example was not compiling, but I did my best to use your idea.

The code is below. Now I can compile for Froyo, but not for Gingerbread because resources from dependency are not accessible:
".../build/manifests/freegingerbread/debug/AndroidManifest.xml:40: error: Error: No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version')."

When I run froyo version compiled with this script, admob jar seems not to be attached:
"E/dalvikvm﹕ Could not find class 'com.google.ads.AdView', referenced from method pl.aambitny.spellscontainer.view.common.AdUtils.reloadAd"

task fixDependencies << {
    def freeFlavor = android.productFlavors.free;
    def froyoFlavor = android.productFlavors.froyo;
    def gingerbreadFlavor = android.productFlavors.gingerbread;

    android.applicationVariants.all { variant ->
        def flavors = variant.productFlavors;
        if (flavors.contains(freeFlavor) && flavors.contains(froyoFlavor)) {
            variant.javaCompile.classpath += configurations.freeFroyoCompile;
        } else if (flavors.contains(freeFlavor) && flavors.contains(gingerbreadFlavor)) {
            variant.javaCompile.classpath += configurations.freeGingerbreadCompile
        }
    }
}

tasks.whenTaskAdded { theTask ->
    if (theTask.name.startsWith("compile")) {
        theTask.dependsOn "fixDependencies"

Xavier Ducrohet

unread,
Apr 2, 2014, 7:08:04 PM4/2/14
to adt...@googlegroups.com
Yes this is only going to work for jar dependencies. If you have an aar dependency, it's not going to work.

We're going to allow this but there's some work that needs to happen first before we can make progress on this.

Paweł Stankowski

unread,
Apr 3, 2014, 4:21:41 PM4/3/14
to adt...@googlegroups.com
I found some workaround, which I described here:
http://stackoverflow.com/questions/22313632/multi-flavor-compile-dependencies-in-gradle

Generally I parse start task (eg. "assembleFreeFroyoDebug") given to gradle and check what flavours are specified. I store flavours in global variables and use them in following way:

def getAdmobDependency() {
  return  groupFlavor == "Froyo" ? files(getSdkDir() + '/extras/google/admob_ads_sdk/GoogleAdMobAdsSdk-6.4.1.jar') : 'com.google.android.gms:play-services:+'
}

[...]

dependencies{
  freeCompile getAdmobDependency()
}

W dniu 2014-04-03 01:08, Xavier Ducrohet pisze:
You received this message because you are subscribed to a topic in the Google Groups "adt-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adt-dev/JRUksl4XpWk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adt-dev+u...@googlegroups.com.

Stefano IceCubeR

unread,
May 5, 2014, 12:33:36 PM5/5/14
to adt...@googlegroups.com
I'm new to gradle, i'm trying to build 2 flavor,
Froyo - 2.2 only and
GringerBreadPlus - 2.3 and newer

in myProject\MyApp\build.gradle i try this:

    productFlavors {
        GringerBreadPlus {
                   minSdkVersion 9
        }
         Froyo{
             minSdkVersion 8
        }
    }


dependencies {
    compile project(':ckChangeLogmaster')
    compile project(':appirater')
    compile 'com.android.support:support-v4:+'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    //compile 'com.google.android.gms:play-services:+'
    GringerBreadPlusCompile 'com.google.android.gms:play-services:+'
    //FroyoCompile 'com.google.android.gms:play-services:3.2.+'
...
}

> startup failed:
  build file 'C:\Users\sgirotti\AndroidStudio\aJobSearch\aJobSearch\build.gradle
': 54: unexpected token: com.google.android.gms:play-services:+ @ line 54, colum
n 26.
        GringerBreadPlusCompile 'com.google.android.gms:play-services:+'
                              ^

i don't understand.... i try to import from eclipse with the latest Studio.... i also tryed to define productFlavors & dependencies on

myProject\build.gradle

what's wrong?!

Thanks in advance

Mateusz Grzechociński

unread,
May 5, 2014, 8:37:53 PM5/5/14
to adt...@googlegroups.com
Hi,

change 'Ginger' to 'ginger' and 'Froyo' to 'froyo' in both productFlavors{} and dependencies{} sections. 
Then everything would be ok.

M.
Reply all
Reply to author
Forward
0 new messages