1

The android project I'm working on has 3 build types - debug, qa and release:

buildTypes {
    release {
        ...
    }
    qa {
        ...
    }
    debug {
        ...
    }
}

I would like to have a dependency (crash reporting library) only apply to debug and qa but not release.

I guess it could be done by duplicating build-type specific compile lines like this:

dependencies {
    ...
    debugCompile 'com.mindscapehq.android:raygun4android:1.3.0:sources'
    qaCompile    'com.mindscapehq.android:raygun4android:1.3.0:sources'
}

Is there a way to do this without duplication?

user1405990
  • 806
  • 1
  • 8
  • 19
  • 1
    what's your concern about the duplication? is it that you when versions change you have to do it in multiple places? if so, just use a variable for the version and use that as the dependency. `ext { raygunVersion = 'com.mind....:1.3.0:sources' }` then `dependencies { debugCompile raygunVersion }`, is how we do it in builds. – Mark Fisher Aug 18 '15 at 08:57

1 Answers1

1

You can iterate over build types in dependencies

dependencies {
    ...
    android.buildTypes.each { type ->
        if(type.name.equals("debug") || type.name.equals("qa")) {
            compile('com.mindscapehq.android:raygun4android:1.3.0:sources')
        }
    }
}
Stas Parshin
  • 7,973
  • 3
  • 24
  • 43
  • Thanks for the response but this didn't seem to compile the dependency into the project. There were compilation errors indicating source files from this library weren't found. – user1405990 Aug 19 '15 at 22:46
  • Try without sources compile('com.mindscapehq.android:raygun4android:1.3.0') – Stas Parshin Aug 20 '15 at 10:11
  • @user1405990 I tried on my project, everything is fine. post your whole gradle file, because this is correct answer – Stas Parshin Aug 20 '15 at 14:18
  • I tried it but in both cases, "release" and "build" Build Variant Android Studio tries to use the the libs selected for "build" only: dependencies { android.buildTypes.each { type -> if (type.name == "debug") { compile(name:'library-debug', ext:'aar') } else { compile(name:'library-release', ext:'aar') } } – hb0 Sep 04 '18 at 07:53
  • Error is: "Multiple dex files define Lcom/my/package/Classname" – hb0 Sep 04 '18 at 08:35