preface
I have written two previous articles on managing dependencies in projects:
What? There are over 200 lines of Gradle code in this project! You may need the Kotlin+buildSrc Plugin
How do you configure dependencies like this in addition to buildSrc? Use opportunely includeBuild
The Android Detail project currently uses the approach described in Article 2.
The Android-detail and a version control plug-in are managed with composing builds.
Today we’ll talk about how to extract common configuration in this way so that the build.gradle file has as little content as possible, or even empty content.
As you read this article, you will know:
- How do I extract common configurations from build.gradle files
- How to switch local Module and remote dependency with one click
The Demo in this
Extract the configuration of the Android closure
In Android Detail, baselib is library module and all other modules are app Modules. There are many of the same configurations in the App Module, as shown below:
We can write an extension function for BaseExtension called applyBaseCommons. In this method, we configure common Settings under Android closures, such as compileSdkVersion, versionCode, etc.
We then call this method in the Version Plugin to configure the common configuration within the Android closure for all modules.
Extract common dependencies
Each App Module has common dependencies, such as test-related dependencies, Kotlin library dependencies, and references to the Baselib Module
Along the same lines, we can configure a dependency extension function
This method is then called in the Version Plugin
Extract common plug-ins
Each app Module has two extensions kotlin-Android and Kotlin-Android-Extensions
Following this thread, let’s write an extension function that configures the common plug-in
So far, there are only two lines in the App Module
I have severe obsessive-compulsive disorder and would like to eliminate these two lines.
Can it be done?
Must be!
Gradle under project has an allprojects{} closure where we can uniformly configure content for allprojects.
There is also a subprojects{} that configures content for sub-projects. We can configure the Version plugin for all modules in this closure, com.android.library plugin for Baselib, Configure the com.android.application Plugin for the rest of the app Module
To enable project build.gradle to find our version plugin, we also need to declare plugins{} with apply set to false
Of course, the criteria for using the Application/Library plugin can be configured to suit your situation, for example, all modules with the lib_ prefix use the Library plugin
Switch remote dependent/local modules using composite builds
A composite build can build multiple projects together, such as my Settings in Android-detail. Gradle introduced another project of mine via the includeBuild keyword, which was published to the Jitpack, Can use com. Making. Flywith24: Flywith24 – Permission: introducing 1.0.1
The environment variable useLocal is used to determine whether to includeBuild Permission projects. If false, remote dependencies are used; if true, local Module: Library is used
Switch other positions for remote dependent/local modules with one click
Using a similar configuration in Project, you can also switch remote dependent/local modules with one click
In this way, you do not need to send snapshots frequently during debugging
About me
I am Flywith24, and my blog content has been classified here. You can click Watch in the upper right corner to get updates of my articles in time, oh 😉
-
The Denver nuggets
-
Small column
-
Github