Marian Klühspies
Marian Klühspies

Reputation: 17637

Detect if library is build independently or as application dependency with Gradle

My project has the following structure:

--\Project
  --\Library Module A
  --\Library Module B    
  --\Library Module C    
  --\Application Module

I´m using android-fat-aar, to merge all library modules into one single aar for other projects, which works like a charm.

To test my library, I have also an application module, which includes the library.

After upgrading to the latest gradle version and fat-aar version, building the application module is impossible

Github Issue - com.android.dex.DexException: Multiple dex files define ...

To merge Library Module B and Library Module C into A, I have to declare those dependencies as embedded instead of compile.

dependencies {
    testCompile 'junit:junit:4.12'
    compile('com.android.support:support-v4:10.0.1')
    embedded project(':Library-Module-B')
    embedded project(':Library-Module-C')
}

As I mentioned, this works when I build the library independently, but not if I build the application module.

I don´t know why this error happens, because none of those libraries gets included more than one time according to the log output, so I thought coming up with a small workaround would do the trick:

  1. Check if library is build independently or not
  2. Change embedded to compile if true (because then it works again) OR add all embedded dependencies to the default dependency set in fat-aar

Is it possible to let the library detect, if the library is build independently, or if the application is built together with the library?

Upvotes: 0

Views: 1830

Answers (2)

Marian Klühspies
Marian Klühspies

Reputation: 17637

I could simply solve it by using an ext-property

Therefore I´m setting it in the application module

android {
    rootProject.ext.set("applicationBuild", true)
    compileSdkVersion 23
    .
    .
    .
}

and in the submodule

  boolean applicationBuild = rootProject.hasProperty("applicationBuild")

  if (!applicationBuild) {
     embedded .....
     embedded .....
     embedded .....
  }else{
     compile ....
     compile ....
     compile ....
  }

The property is only set when I build the application module

Upvotes: 2

Vampire
Vampire

Reputation: 38669

As I'm not really familiar with building Android libraries with Gradle, (and never heard of android-fat-aar, I cannot say whether there is a better solution that works properly, so maybe someone else can come up with something.

To do the detection you have in mind, you can e. g. use gradle.taskGraph.whenReady { } and in there check the taskGraph for the presence of the application tasks. The taskGraph represents the tasks that will be executed in this build, which is the explicitly specified ones (or defaultTasks if none) and all their dependencies.

But actually afair this point in time should be after the configuration phase, so might be too late to change whether a dependency is compile or embedded. But it might be just in time to change the fat-aar configuration like you suggested. Just play with it and see how you get it to work from within the whenReady closure.

Upvotes: 0

Related Questions