简体   繁体   中英

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

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. projlib is a lib folder that compiles and generates a lib(jar) file. projA is an Android Application and projB is another Android Application that needs to reference code in projA. Right now what I have added in the projB build.gradle file is

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

So say if there's a class FooProjLib in projlib and FooProjA in projA

Then In projB I can do

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.

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. projB can only depend on projA if projA is an Android library, meaning in its build file you have this declaration:

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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