JBM
JBM

Reputation: 2962

Enforcing the priority of libraries in Android gradle build

I work on a framework which is delivered to the customer as a set of .aar files, which are then included into their project, like this:

...
+-- assets
+-- libs
      +-- framework-module1.aar
      +-- framework-module2.aar
      +-- framework-module3.aar
      +-- customer.aar
+-- main
...

The framework consists of a number of modules which are generic for all customers, and one customer.aar which provides the customer-specific code and configuration. The idea is that code/resources in customer.aar override the default code/resources per customer's needs.

The problem is: when the final .apk is built, sometimes the values are custom (as intended) but sometimes they are still the default ones. I guess, the order of .aars being assembled is not correct those times.

There are mentions of library priority in the documentation of the build tools, but they are only relevant to the old Eclipse-based build system and not to Gradle-based one.

Here is my build.gradle file:

...
repositories {
    flatDir {
        dirs file('libs')
    }
}

dependencies {
    compile(name:'framework-module1', ext:'aar')
    compile(name:'framework-module2', ext:'aar')
    compile(name:'framework-module3', ext:'aar')

    compile(name:'customer', ext:'aar')
}
...

Is there a way I can tell Gradle that customer.aar must always come last in the build?

Upvotes: 3

Views: 2171

Answers (1)

JBM
JBM

Reputation: 2962

Resources are processed by Android build tools in the order opposite to that declared in Gradle script. The code responsible for the order is found here, if you're queries.

Swapping just one line around has solved the problem:

dependencies {
    compile(name:'customer', ext:'aar')

    compile(name:'framework-module1', ext:'aar')
    compile(name:'framework-module2', ext:'aar')
    compile(name:'framework-module3', ext:'aar')
}

Remember to clean your build after altering the order of dependencies! Gradle does not detect it automatically.

Note that this "reversed" order applies only when resources are coming from android libraries, i.e. when the dependency is aar file. In the case when dependency is another project or module, the normal (non-reversed) order is in force.

Upvotes: 3

Related Questions