简体   繁体   English

Gradle Android项目依赖项不起作用

[英]Gradle Android project dependency not working

I'm trying to migrate from maven to Gradle for an Android project and I am facing the following issue: I have three projects ie 我正在尝试从maven迁移到Gradle以进行Android项目,并且面临以下问题:我有三个项目,即

root
- projlib
   build.gradle
- projA
   build.gradle
- projB
   build.gradle
build.gradle
settings.gradle

Basically what I want to achieve is that projB depends on projA and projlib. 基本上,我要实现的是projB依赖于projA和projlib。 projlib is a lib folder that compiles and generates a lib(jar) file. projlib是一个lib文件夹,可编译并生成一个lib(jar)文件。 projA is an Android Application and projB is another Android Application that needs to reference code in projA. projA是一个Android应用程序,而projB是另一个需要在projA中引用代码的Android应用程序。 Right now what I have added in the projB build.gradle file is 现在我在projB build.gradle文件中添加的是

dependencies {
   compile project(':projlib')
   compile project(':projA')
}

So say if there's a class FooProjLib in projlib and FooProjA in projA 所以说如果projlib中有FooProjLib类,而projA中有FooProjA类

Then In projB I can do 然后在projB中我可以做

FooProjLib foo = new FooProjLib

which works fine 哪个很好

but when I do 但是当我这样做

FooProjA foo = new FooProjA 

Gradle gives me package projA does not exist, what I have observed is that both dependency is resolved but only the lib can be reference and not the apk. Gradle给我的软件包projA不存在,我观察到的是这两个依赖关系都已解决,但是只有lib可以被引用,而apk不能。

Does anyone have an idea how to solve this? 有谁知道如何解决这个问题?

You can't do exactly what you want. 您不能完全按照自己的意愿做。 projA can't build an application (ie an APK) and also have other things depend on it. projA无法构建应用程序(即APK),并且还依赖于它。 projB can only depend on projA if projA is an Android library, meaning in its build file you have this declaration: 如果projA是Android库,则projB只能依赖projA,这意味着在其构建文件中您具有以下声明:

apply plugin: 'android-library'

instead of 代替

apply plugin: 'android'

Of course, this means that projA won't build an APK, but will build an AAR instead. 当然,这意味着projA不会构建APK,而是会构建AAR。

If projA needs to also be an APK, you'll have to restructure things such that the common code that's in projA that projB also needs is moved out to a shared library. 如果projA也需要是APK,则必须重新组织事情,以便projB也需要的projA中的通用代码移到共享库中。 If both projects are similar, perhaps you could have just one module for them and use project flavors to differentiate them; 如果两个项目都相似,也许您可​​能只需要一个模块,并使用项目风格来区分它们。 it's hard to say whether this is a good approach without a lot more information. 如果没有很多信息,很难说这是否是一个好方法。

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

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