Do Something Different in Android Library Depending on Parent Project


(Didier Garcia) #1

I have a Android Library project that takes a config file and it uses to generate all the required BuildConfig values. The config file is given via a project property PROPS and used passed in via command line:

./gradlew -PPROPS=../somefile.xml build 

When the Android Library project is included in an Android Application project, the same PROPS file is used via the command line. All good so far.

The root project has 2 Android Application sub-projects and both depend on the Android Library project.

Today they have to be built separately and with the config file passed in:

// First project
./gradlew -PPROPS=../some-file-a.xml projecta:build

// Second project
./gradlew -PPROPS=../some-file-b.xml projectb:build

I don’t like this and would like to just be able to say:

./gradlew build

And have it build both projects, but each of the projects requires that the common Android Library project be given a different config file.

I’ve tried setting the PROPS property during the configuration step in each of the Android application projects:

// In projecta/build.gradle

// In projectb/build.gradle

But since that both projects set the same property, once the Android Library project runs it just sees the last one that gets set. This breaks my build because it configures the Android Library the same (based on the last config file set).


Ideally, I’d like to be able to, at build-time, determine what file the Android Library project should use based on which top-level Android Application project it is part of.

I understand that what I’m asking for may not be the “Gradle Way” so I’m also interested in learning about the proper way to do this, if one exist.


Using configuration on demand (Set org.gradle.configureondemand=true in I am able to build each project individually using a default config file for each project:

// First Project
./gradlew projecta:build

// Second Project
./gradlew projectb:build

But still can’t build both at once:

./gradlew build