简体   繁体   English

使用 Android 库 (AAR) 作为 Gradle 的 Java (Jar) 项目的依赖项?

[英]Use an Android Library (AAR) as dependency for a Java (Jar) Project with Gradle?

I have the same requirement as this post: same issue我和这篇文章有同样的要求: same issue

As the author on the above issue said in the comment, I am also stuck in the same place.正如上述问题的作者在评论中所说,我也卡在了同一个地方。 Any solution?任何解决方案?

PS: I have to post this because I don't have enough stack overflow reputations yet to make comment on the same post and ask an author if he was able to find a solution. PS:我必须发布这个,因为我还没有足够的堆栈溢出声誉来对同一个帖子发表评论并询问作者是否能够找到解决方案。

Java modules can't depend on Android Modules, only Android Modules can depend on Java Modules, as stated in the linked questions error as well. Java 模块不能依赖 Android 模块,只有 Android 模块可以依赖 Java 模块,如链接问题错误中所述。

If you are building a module for testing, to be used in you app's unit tests, that module can be simply an Android Library momdule, doesn't need to be a Java Module.如果你正在构建一个用于测试的模块,用于你的应用程序的单元测试,该模块可以只是一个 Android 库模块,不需要是一个 Java 模块。 In an Application module (or Android Library module) you can have aar as test dependencies, like testImplementation "io.reactivex.rxjava2:rxandroid:2.0.1" .在应用程序模块(或 Android 库模块)中,您可以将 aar 作为测试依赖项,例如testImplementation "io.reactivex.rxjava2:rxandroid:2.0.1"

However, if your code under test actually interacts with Android SDK, you will need to mock those.但是,如果您的测试代码实际与 Android SDK 交互,您将需要模拟这些。 In such case, first you will need to consider to just write AndroidTest instead.在这种情况下,首先您需要考虑只编写 AndroidTest。 If it is important to be unit class, then you might want to look into using robolectric .如果成为 class 单元很重要,那么您可能需要考虑使用robolectric

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

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