Maintaining multiple multi-module Android project often requires copying project configuration across different projects.
Even when project reaches more advanced stage it is still required to put non minimal effort to maintain its configuration.
Starting a new project, from the scratch, takes more than a day to configure every tool you usually want to use.
Sometimes people create template project or another way of keeping your project configuration in a good shape is using buildSrc
plugins.
Less code written, ease of sharing between projects but still some part of the code needed to be copied.
This project goes further and addresses that issue by exposing set of plugins useful when approaching multi-module setup with Gradle build system.
Repository consists of several plugins that makes initial project configuration effortless and easily extensible. Each module consists of configuration code most commonly used in Android project configuration.
Add to your root project build.gradle
:
buildscript {
repositories {
gradlePluginPortal()
google()
}
dependencies {
classpath 'com.project.starter:plugins:${version}'
}
}
Plugin configures automated code style tasks, hooks for common tasks, sets coverage reports generation or manages versioning of the artifact
Apply plugin to project level build.gradle
apply plugin: 'com.starter.library.kotlin'
// optional config with default values
projectConfig {
javaFilesAllowed false
}
javaFilesAllowed
- defines if the project can contain java files, fails the build otherwise
In addition to customizations made to Kotlin Library Plugin Android plugins
tweak default Android Gradle Plugin setup by disabling BuildConfig file generation
or recognizing src/main/kotlin
(and similar) path as a valid source set.
Android Library plugin requires adding to project level build.gradle
:
apply plugin: 'com.starter.library.android' // or 'com.starter.application.android'
// optional config with default values
projectConfig {
javaFilesAllowed false
generateBuildConfig false // for library plugin only
defaultVariants ["debug"]
coverageExclusions [""]
}
// overridden settings for single project
android {
defaultConfig {
minSdkVersion 21
}
}
javaFilesAllowed
- defines if the project can contain java files, fails the build otherwise.
(Useful in large projects where you want to enforce new code written in new modules to be written in Java.)generateBuildConfig
- defines ifBuildConfig.java
class should be generated or not.
General suggestion is to prefer Dependency Injection over Android's flavor setup for librariesdefaultVariants
- defines build variants used as a dependency for commonprojectXXX
tasks.
for example settingfullDebug
as default variant would maketestFullDebugUnitTest.
as a dependency forprojectTest
task.["freeRelease", "fullRelease"]
would make addtestFreeReleaseUnitTest
toprojectTest
andtestFreeReleaseLint
toprojectLint
.coverageExclusions
- defines jacoco coverage exclusions for specific module
Quality plugin is applied automatically by a Module plugin, but there is a possibility to use it as a standalone plugin.
Apply plugin to project level build.gradle
apply plugin: 'com.starter.quality'
which applies and configures code style tasks for the project automatically.
To execute run: ./gradlew projectCodeStyle
When integrating code style checks into large projects it is almost forbidden to introduce large sets of changes.
It is possible to generate baseline for every quality tool available in the project.
Android Lint
Typerm **/lint-*.xml ; ./gradlew projectLint -PrefreshBaseline --continue
into consoleDetekt
Create baseline using provided configurationCheckstyle
Execute./gradlew generateCheckstyleBaseline
task.ktlint
Unfortunately it is not possible to generatektlint
baseline. Proper code style may be achieved by using./gradlew formatKotlin
task.
Applied automatically. Uses tag-based versioning backed by the allegro/axion-release-plugin (view a full documentation)
To enable it as a standalone plugin, apply plugin to root project build.gradle
apply plugin: 'com.starter.versioning'
Can be disabled using Global Configuration
Regular flow relies on calling
./gradlew cV
or./gradlew currentVersion
./gradlew markNextVersion -Prelease.version=1.0.0
./gradlew release
(which pushes proper tags to remote server)
Additional default configuration can be applied by adding to root project build.gradle
.
All submodules will use this config as default
apply plugin: 'com.starter.config'
commonConfig {
javaVersion JavaVersion.VERSION_1_8
javaFilesAllowed true
androidPlugin {
compileSdkVersion 29
minSdkVersion 23
targetSdkVersion 29
}
qualityPlugin {
enabled true
formatOnCompile false
}
versioningPlugin {
enabled true
}
}
javaVersion
- defines which java version source code is compatible withjavaFilesAllowed
- defines if the project can contain java files, fails the build otherwise.androidPlugin
:- contains values passed to Android Gradle Plugin
qualityPlugin
:enabled
- enables/disables Quality PluginformatOnCompile
- defines if ktlint should format source code on every compilation
versioningPlugin
:enabled
- enables/disables Versioning Plugin
After applying library/application plugin there are appropriate tasks added:
./gradlew projectTest
Runs tests for all modules using either predefined tasks (i.e.test
for kotlin modules ortestDebugUnitTest
for android libraries) or use customized values../gradlew projectLint
Runs Android lint checks against all modules (if custom lint checks are applied then for Kotlin modules too)./gradlew projectCodeStyle
Verifies if code style matches modern standards using tools such asktlint
,Detekt
orCheckstyle
with predefined config../gradlew projectCoverage
Automatically generates test coverage reports for all modules usingJacoco
Those tasks allows you to run tests efficiently for all modules by typing just a single task.
That solves an issue when for example test
task unnecessarily executes tests for all build variants and more strict testDebug
skips executing tests in kotlin only modules.
Sample Github Browser project - a customized, buildSrc
based plugin application.
The library is available under MIT License and highly benefits from binary dependencies: