Gradle Module Build Order

倖福魔咒の 提交于 2020-02-25 04:22:06

问题


I have a Gradle root project P with two subprojects P:foo and P:bar. Naturally, Gradle builds them in alphabetical order: bar, foo. But I need foo to be built first when I say gradle build in the P root directory. This is because bar depends on the AAR (Android library) artifact that foo publishes to the local Maven repository. Both bar and foo are such Android-library projects.

This looks like an easy problem, but I can't figure it out. I read about evaluationDependsOn, so in bar/build.gradle, I say in the first line: evaluationDependsOn "foo:". Alas, this does not seem to have any effect. Is this Gradle feature broken in the end?

Using compile project, I could enforce foo to be built first, but that would add the compiled classes directly to bar, which I don't want.

So I'm stuck. I could rename foo to aaa_foo and all my problems would be gone, but I hesitate to call that a solution.


回答1:


Okay, so let me answer this question myself. I think I found a decent solution.

A project dependency in Gradle is usally expressed by means of compile project, which not only builds the other project but also adds the other project's classes to the classpath of the current project. If you only want to make sure another project is built before yours, you can use a task dependency.

In my Android environment, in bar/build.gradle, I say

preBuild.dependsOn ":foo:build"

and all is well. Now foo is always built before bar.




回答2:


This may help those with maybe a different cause of the problem.

Say I have a module base which generates output needed by the main module app, so I want base to be built before app.

You can check your settings.gradle in the project root directory and examine the config.

I found my previous config was

include ':app', ':base'

Change it to

include ':base', ':app'

solved the building order.



来源:https://stackoverflow.com/questions/42329484/gradle-module-build-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!