For example we have a project structure like this
+ProjectX
\build.gradleX
\----SubProjectA
\--------build.gradleA
\----SubProjectB
\--------build.gradleB
So we have a root project called ProjectX and two sub projects called ProjectA and ProjectB. Each of them has their own build.gradle file for gradle build. By default, they all should be named as build.gradle. To distinguish them, we denote them as
- build.gradleX
- build.gradleA
- build.gradleB
Say we want to use the same version of JUnit in all projects, so we need to use the same JUnit dependency and reference the same dependency across all projects.
In build.gradleX, we define a property
ext { lib = [ junit: 'junit:junit:4.11' ] }
In build.gradleA and build.gradleB we refer the dependency by
dependencies { compile lib.junit //This references the property defined in build.gradleX }
Improvement:
The mechanism of the above code is, the build.gradleX defines a property in root project(ProjectX) so in its subjects, lib.junit will be read inherited from the root project. But if we define ext.lib in PojectA as well like below then the lib.junit will use its local value. This is good if we want to override the value but if we don't know what happens behind the scene, mistake occurs.
in build.gradleA
ext { lib = [ junit: 'junit:junit:3.0.0' ] }
So, to refer the lib.ext we use
rootProject.lib.junit //referencing the absolute root projector
parent.lib.junit //referencing relative parent project which is the root here
This comment has been removed by the author.
ReplyDeleteThis is my 1st visit to your web... But I'm so impressed with your content. Good Job!
ReplyDeletePython training in marathahalli | Python training institute in pune
This comment has been removed by the author.
ReplyDeleteWow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeleteblueprism training in chennai | blueprism training in bangalore | blueprism training in pune | blueprism online training
I have been meaning to write something like this on my website and you have given me an idea. Cheers.
ReplyDeleteJava training in Indira nagar | Java training in Rajaji nagar
Java training in Marathahalli | Java training in Btm layout