How to setup cross-project task dependency in Android?

Here’s the structure of my project:

AndroidApp
+ app
+ androidlib

In androidlib subproject I define a task makeJar, and I want the task assembleDebug in app depends on makeJar. As we know the task assembleDebug is added dynamically, so I tried this:

// build.gradle - app
afterEvaluate {
    tasks.assembleDebug.dependsOn(rootProject.project(':androidlib').tasks.makeJar)
}

Gradle Sync doesn’t report any errors, but when I try making subproject app it fails with error Could not get unknown property 'makeJar' for task set of type org.gradle.api.internal.tasks.DefaultTaskContainer.

It seems that when making a single subproject, other subprojects will NEVER be evaluated and I can’t execute their tasks. However, due to some reason I can’t add androidlib as the project dependency of app.

So is it possible to make :app:assembleDebug depend on :androidlib:makeJar, with :app not depending on :androidlib?

tasks.assembleDebug.dependsOn( ':androidlib:makeJar' )
1 Like

Thanks! This does solve the problem, but why ':androidlib:makeJar' instead of rootProject.project(':androidlib').tasks.makeJar?

Sorry, I usually try to explain things in more detail up front.

When a build script is evaluated (aka Gradle’s configuration phase), each sub project is evaluated in turn. If app's build.gradle is evaluated before androidlib, then androidlib's makeJar task won’t exist yet, causing the unknown property failure you are seeing. You can use Project.evaluationDependsOn to add control over the evaluation ordering, however I recommend you avoid doing so unless absolutely necessary (chances are there’s a better way to accomplish something if you find yourself needing to force this level of ordering). Since all you need is a reference to the makeJar task and you’re not actually trying to access properties of that task, you can use the task’s path as a string. This string will be resolved to an actual task by Gradle after all of the projects have been evaluated, removing the requirement that one project be evaluated before another.