简体   繁体   中英

What is the proper Gradle project structure for switching between 'project' and 'external' dependencies

I have this small set of libraries and app:

  • forge-base - java project (Intellij IDEA project)
  • forge-android - android library project, depends on forge-base (AS project)
  • forge-android-skeleton - sample android app, depends on forge-android (AS project)

During the initial development I used structure with project dependencies like:

settings.gradle:

...

include ':forge-base'
project(':forge-base').projectDir=new File("$settingsDir/../forge/base")

...

and then in build.gradle:

compile project(':forge-base')

This worked like a charm but later I needed to publish the libs on maven repo and dependencies had to be changed like:

build.gradle:

compile 'com.bolyartech.forge:forge-base:2.0-SNAPSHOT'

The problem that I am facing now is that I am trying to do some major refactoring in all the 3 projects and I need the old deps structure in order easily to confirm the consistency of the projects, eg to build just the skeleton app and all the recursive recompile/building to take place automatically (as it does when a lib project is referenced with compile project(':forge-base') ). If I use the 'new' structure with publishing to the (local) maven I have to publish the lib each time (with incremented version) I change something in it in order changes to be visible by the other two projects.

What (is there) is the usual/canonical why to handle situations like this?

Is there an easy way to switch between the two 'modes', eg 'internal' / 'external' dependencies?

It turns out it is pretty easy to do it. You can use different dependencies for the different build types, ie debug/release so for example for my forge-android-skeleton project now I have the following:

in settings.gradle :

include ':app'

include ':forge-base' project(':forge-base').projectDir=new
File("$settingsDir/../../../forge/base")

include ':forge-android' project(':forge-android').projectDir=new
File("$settingsDir/../../../forge-android/forge-android")

in app/build.gradle :

...
releaseCompile ('com.bolyartech.forge:forge-android:2.7-SNAPSHOT')

debugCompile project(':forge-android')
...

Please note that in the settings.gradle you need to have all the dependencies back to the bottom otherwise it will not work (that is the reason forge-base is defined there even not excplicitly used).

You can also define yet another build type like direct-deps and use it in case you don't like to mess with debug/release types.

Please note that in case you are using different IDEs for some of the projects (like in my case IDEA and AS) probably it will be good idea to ensure that both are using same (version of) gradle otherwise unexpected problems may occur.

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