简体   繁体   English

Gradle 模块构建顺序

[英]Gradle Module Build Order

I have a Gradle root project P with two subprojects P:foo and P:bar .我有一个 Gradle 根项目P有两个子项目P:fooP:bar Naturally, Gradle builds them in alphabetical order: bar , foo .自然地,Gradle 按字母顺序构建它们: barfoo But I need foo to be built first when I say gradle build in the P root directory.但是当我在P根目录中说gradle build时,我需要先构建foo This is because bar depends on the AAR (Android library) artifact that foo publishes to the local Maven repository.这是因为bar依赖于foo发布到本地 Maven 存储库的 AAR(Android 库)工件。 Both bar and foo are such Android-library projects. barfoo都是这样的 Android 库项目。

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:" .我读到了evaluationDependsOn ,所以在bar/build.gradle ,我在第一行说: evaluationDependsOn "foo:" Alas, this does not seem to have any effect.唉,这似乎没有任何影响。 Is this Gradle feature broken in the end?这个 Gradle 功能到底是不是坏掉了?

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.使用compile project ,我可以强制首先构建foo ,但这会将编译的类直接添加到bar ,这是我不想要的。

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.我可以将foo重命名为aaa_foo并且我所有的问题都会消失,但我犹豫是否将其称为解决方案。

Okay, so let me answer this question myself.好吧,让我自己来回答这个问题。 I think I found a decent solution.我想我找到了一个不错的解决方案。

A project dependency in Gradle is usually 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. Gradle 中的一个项目依赖通常通过compile 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 have在我的 Android 环境中,在bar/build.gradle ,我有

preBuild.dependsOn ":foo:build"

and all is well.一切都很好。 Now foo is always built before bar .现在foo总是在bar之前构建。

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 .假设我有一个模块base ,它生成主模块app所需的输出,所以我希望在app之前构建base

You can check your settings.gradle in the project root directory and examine the config.您可以检查项目根目录中的settings.gradle并检查配置。

I found my previous config was我发现我以前的配置是

include ':app', ':base'

Change it to将其更改为

include ':base', ':app'

solved the building order.解决了建筑秩序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM