I have an Android
project, where I want to refactor specific functions into one module
. My app structure looks like this:
MyApp
|--app
|----build.gradle
|--myNewModule
|----build.gradle
|build.gradle (MyApp Project)
|settings.gradle
In the project, I have some dependencies that are only needed in the myNewModule and some that are needed by the whole project, e.g. okHttpClient
.
Currently, my gradle files look like this:
build.gradle (Project MyApp)
Defining variables for setting the same library versions in the whole project.
...
ext {
butterknifeVersion = "7.0.1"
daggerVersion = "2.6"
moshiVersion = "1.1.0"
okhttpVersion = "3.4.1"
...
}
...
build.gradle (MyNewModule) Define dependencies needed for this module
...
dependencies {
compile ("com.squareup.retrofit2:converter-simplexml:$retrofitVersion") {
exclude group: 'stax', module: 'stax-api'
exclude group: 'stax', module: 'stax'
exclude group: 'xpp3', module: 'xpp3'
}
compile "com.squareup.okhttp3:okhttp-urlconnection:$project.okHttpVersion"
compile "com.squareup.okhttp3:okhttp:$project.okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$project.okHttpVersion"
....
}
...
build.gradle (app) Define other dependencies, can be overlapping with dependencies in MyNewModule?
...
dependencies {
compile "com.jakewharton:butterknife:$project.butterknifeVersion"
compile "com.jakewharton.timber:timber:$project.timberVersion"
compile "com.jakewharton.threetenabp:threetenabp:$project.threetenabpVersion"
compile "com.squareup.okhttp3:okhttp-urlconnection:$project.okHttpVersion"
compile "com.squareup.okhttp3:okhttp:$project.okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$project.okHttpVersion"
...
}
...
build.gradle (Project MyApp)
Add all dependencies for the whole project here?
...
dependencies {
compile "com.jakewharton:butterknife:$project.butterknifeVersion"
compile "com.jakewharton.timber:timber:$project.timberVersion"
compile "com.jakewharton.threetenabp:threetenabp:$project.threetenabpVersion"
compile "com.squareup.okhttp3:okhttp-urlconnection:$project.okHttpVersion"
compile "com.squareup.okhttp3:okhttp:$project.okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$project.okHttpVersion"
...
}
...
I just want to follow good design patterns and don't increase the dexcount of the project as it uses many libraries and is close to multi dex, which I want to avoid.
EDIT: I have changed the wording of my second question as it was misleading. I was just thinking of adding some of the same dependencies into both modules as I might make a separate library out of the myNewModule and then it still needs e.g. an okHttpClient and cannot rely on the dependency in the main project any longer.
If I have a dependency for the same library in different modules, will Android recognize it as one dependency or will it double the dexcount for the apk?
Gradle handles it automatically.
If you are adding the same depedencies in the library and in the module which uses the library, gradle will add them only once.
Would this be a good way to define dependencies? In my case, the myNewModule will contain dependencies, which are only needed in it, but also dependencies, that are needed in the app module.
Not in my opinion.
Your library should contains only the dependencies needed.
There is no reason to include also the dependencies that will be used by the module that will use it.
Or else, could I have put common dependencies directly into the build.gradle file from the Project?
The question is not so clear.
Each module should have its dependencies but you can centralize them in the same way you described in the question.