Gradle group module dependencies based on flavors - android

The problem that I have is that the regular
dependencies {
compile project(path: ':moduleOne', configuration: "typeRelease")
}
is not enough for my app's purposes. I have quite a list of dependencies with different configurations (and flavors + build types) involved, and what I want to do now is to add some kind of a method to leverage those dependencies.
For example,
dependencies {
flavorOneAnalyticsCompile project(path: ':moduleOne', configuration: "typeRelease")
flavorOneFlurryCompile project(path: ':moduleOne', configuration: "typeRelease")
flavorOneCrashlyticsCompile project(path: ':moduleOne', configuration: "typeRelease")
flavorTwoAnalyticsCompile project(path: ':moduleOne', configuration: "typeStaging")
flavorTwoLoggingCompile project(path: ':moduleOne', configuration: "typeStaging")
}
Is it possible to say "I want all my build variants with FlavorOne to compile moduleOne with typeRelease configuration, and all build varians with FlavorTwo to compile moduleOne with typeStaging configuration"?
Does anybody know how to do it? Is this even possible? I have found here how to use flavor + build type dependencies, but it's quite ugly to have this big fat list of similarly-looking dependencies.
Thanks! :)

Related

Exclude specific sub-modules from product build

Okay, so I have different product flavors and I have different submodules. Right to implement submodule, I do this:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':A-Android')
implementation project(path: ':B-Android')
implementation project(path: ':C-Android')
}
Is it possible to implement, for instance, submodule :A-Android only for specific product flavor? How?
Yes, can configure dependencies only for specific flavor. Here an excerpt from Declare dependencies documentation :
Declare dependencies
You can configure a dependency for a specific build variant or
testing source set by prefixing the name of the build variant or
testing source set before the Implementation keyword, as shown in the
following example.
dependencies {
// Adds the local "mylibrary" module as a dependency to the "free" flavor.
freeImplementation project(":mylibrary")
// Adds a remote binary dependency only for local tests.
testImplementation 'junit:junit:4.12'
// Adds a remote binary dependency only for the instrumented test APK.
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
For more information, see Add build dependencies.
So, if you have two flavors something like production and development flavor, then you can add the dependencies like this:
dependencies {
productionImplementation project(path: ':A-Android')
DevelopmentImplementation project(path: ':B-Android')
..
}

Android Gradle 3.0.0 - add library dependency for specific flavors

My app includes a library and a 3 flavor debug, release and custom.
I don't want my app to include the library in 'custom' flavor
Prior to gradle 3.0 I used :
releaseCompile project(path: ':myLib', configuration: "release")
debugCompile project(path: ':..:myLib', configuration: "debug")
// 'custom' ignored
according to google migrate to android plugin for 3.0 I need to use implementation keyword with matchingFallbacks for 'custom' flavor.
I don't want to use 'matchingFallbacks' because I don't want my app to include the lib in 'custom' flavor.
Any idea how can I compile the lib only in debug and release?
edit
maybe its possible to add 'if' statement i.e:
if(flavor != custom){
implementation project 'myLib'
}
I think you can achieve it by using something like this:
dependencies {
// use only mylib for debug and release
releaseImplementation project(path: ':mylib')
debugImplementation project(path: ':mylib')
// this will be used by all the flavor
implementation "com.android.support:appcompat-v7:27.0.2'
}

gradle configurations/artifacts only uses "default" configuration

submodule (locallibs) build.gradle looks like the following:
configurations.create("default")
artifacts.add("default", file('defaultdistro.aar'))
configurations.create("distro2")
artifacts.add("distro2", file('distro2.aar'))
configurations.create("distro3")
artifacts.add("distro3", file('distro3.aar'))
the build.gradle for the app has a dependencies{} section that looks like the following:
dependencies {
debugCompile project(':locallibs')
flavor1ReleaseCompile project(path: ':locallibs', configuration: 'distro2')
flavor2ReleaseCompile project(path: ':locallibs', configuration: 'distro3')
}
The issue I'm having is that regardless of the flavor/build type the "default" configuration is always compiled so all of my flavors are including the "defaultdistro.aar" instead of the correct .aar distro.
I am expecting for flavor1 release build type to compile the distro2.aar but all flavors are compiling the defaultdistro.aar
EDIT: fixed the locallibs build.gradle representation
EDIT #2: the .aar distros are mutually exclusive

Multiple Flavors and Modules

I have a multiple module app with multiple favors.
Is there a way to only change the top 'app' Build Variant and make the sub-modules build the same way? Or do I need to change each module?
Is there a way to configure build types-flavor dependency in less code? This is 'stupid' config
//Module 1 dependency configuration
flavor1DebugCompile project(path: ':module1', configuration: 'flavor1DebugCompile')
flavor2DebugCompile project(path: ':module1', configuration: 'flavor2DebugCompile')
flavor1ReleaseCompile project(path: ':module1', configuration: 'flavor1ReleaseCompile')
flavor2ReleaseCompile project(path: ':module1', configuration: 'flavor2ReleaseCompile')
//Module 2 dependency configuration
flavor1DebugCompile project(path: ':module2', configuration: 'flavor1DebugCompile')
flavor2DebugCompile project(path: ':module2', configuration: 'flavor2DebugCompile')
flavor1ReleaseCompile project(path: ':module2', configuration: 'flavor1ReleaseCompile')
flavor2ReleaseCompile project(path: ':module2', configuration: 'flavor2ReleaseCompile')
//Module 3 dependency configuration
...
//Module 4 dependency configuration
...

How to depend on specific class or package in another Gradle module?

I have two gradle modules, :app and :backend. The backend contains some classes that I need in :app. So normally I would add compile project(':backend') to my app module's dependencies.
However, this adds all of backend and its dependencies to :app. Is there any way to tell gradle to add only specific classes or packages (and their dependencies) from :backend to :app?
EDIT: :backend is a AppEngine module, which contains a whole host of duplicate google dependencies that clash with my android app when I don't use the android-endpoints configuration.
For removing all the transitive dependencies you can
compile (project(path: ':backend', configuration: 'android-endpoints')) {
transitive = false
}
For ignoring some of them
compile (project(path: ':backend', configuration: 'android-endpoints')) {
exclude(group: 'some.group', module: 'some.module')
}

Categories

Resources