Skip to content

Commit cd0d8b1

Browse files
committed
Migrate to Kotlin DSL Plugins
1 parent ba86982 commit cd0d8b1

File tree

10 files changed

+197
-120
lines changed

10 files changed

+197
-120
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,5 @@
11
plugins {
2-
id("com.android.application")
3-
kotlin("android")
4-
id("kotlin-android-extensions")
5-
}
6-
7-
android {
8-
compileSdkVersion(Sdk.COMPILE_SDK_VERSION)
9-
10-
defaultConfig {
11-
minSdkVersion(Sdk.MIN_SDK_VERSION)
12-
targetSdkVersion(Sdk.TARGET_SDK_VERSION)
13-
14-
applicationId = App.APP_ID
15-
versionCode = App.APP_VERSION_CODE
16-
versionName = App.APP_VERSION_NAME
17-
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18-
}
19-
compileOptions {
20-
sourceCompatibility = JavaVersion.VERSION_1_8
21-
targetCompatibility = JavaVersion.VERSION_1_8
22-
}
23-
kotlinOptions {
24-
jvmTarget = "1.8"
25-
}
26-
sourceSets {
27-
getByName("main").java.srcDirs("src/main/kotlin")
28-
}
29-
buildTypes {
30-
getByName("debug") {
31-
isMinifyEnabled = false
32-
isDebuggable = true
33-
applicationIdSuffix = ".debug"
34-
versionNameSuffix = "-dev"
35-
}
36-
getByName("release") {
37-
isMinifyEnabled = true
38-
isDebuggable = false
39-
isShrinkResources = true
40-
isZipAlignEnabled = true
41-
isJniDebuggable = false
42-
isRenderscriptDebuggable = false
43-
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
44-
}
45-
}
46-
47-
lintOptions {
48-
isWarningsAsErrors = true
49-
isAbortOnError = true
50-
}
2+
id("app-plugin")
513
}
524

535
dependencies {

app/src/main/kotlin/com/purewowstudio/template/MainActivity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package com.purewowstudio.template
33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
55
import androidx.fragment.app.commit
6-
import com.google.android.material.floatingactionbutton.FloatingActionButton
7-
import com.google.android.material.snackbar.Snackbar
86

97
class MainActivity : AppCompatActivity(R.layout.activity_main) {
108

build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
plugins {
2-
id("com.android.application") version BuildPluginsVersion.AGP apply false
3-
id("com.android.library") version BuildPluginsVersion.AGP apply false
4-
kotlin("android") version BuildPluginsVersion.KOTLIN apply false
52
id("io.gitlab.arturbosch.detekt") version BuildPluginsVersion.DETEKT
63
id("org.jlleitschuh.gradle.ktlint") version BuildPluginsVersion.KTLINT
7-
id("com.github.ben-manes.versions") version BuildPluginsVersion.VERSIONS_PLUGIN
84
}
95

106
allprojects {

buildSrc/build.gradle.kts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Build_gradle.PluginsVersions.JACOCO
2-
31
repositories {
42
jcenter()
53
}
@@ -8,10 +6,49 @@ plugins {
86
`kotlin-dsl`
97
}
108

11-
object PluginsVersions {
12-
const val JACOCO = "0.8.5"
9+
gradlePlugin {
10+
plugins {
11+
register("library-plugin") {
12+
id = "library-plugin"
13+
implementationClass = "com.purewowstudio.template.plugins.LibraryPlugin"
14+
}
15+
}
16+
}
17+
18+
gradlePlugin {
19+
plugins {
20+
register("app-plugin") {
21+
id = "app-plugin"
22+
implementationClass = "com.purewowstudio.template.plugins.AppPlugin"
23+
}
24+
}
25+
}
26+
27+
buildscript {
28+
29+
repositories {
30+
google()
31+
jcenter()
32+
}
33+
34+
dependencies {
35+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0")
36+
}
37+
}
38+
39+
repositories {
40+
jcenter()
41+
google()
42+
}
43+
44+
val compileKotlin: org.jetbrains.kotlin.gradle.tasks.KotlinCompile by tasks
45+
compileKotlin.kotlinOptions {
46+
languageVersion = "1.3"
1347
}
1448

1549
dependencies {
16-
implementation("org.jacoco:org.jacoco.core:${JACOCO}")
17-
}
50+
implementation("com.android.tools.build:gradle:4.0.1")
51+
implementation("com.android.tools.build:gradle-api:4.0.1")
52+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72")
53+
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0")
54+
}
Binary file not shown.

buildSrc/src/main/kotlin/com/purewowstudio/template/Dependencies.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ object BuildPluginsVersion {
2525
const val VERSIONS_PLUGIN = "0.28.0"
2626
}
2727

28+
object ModulePlugins {
29+
const val ANDROID_LIBRARY = "com.android.library"
30+
const val ANDROID_APP = "com.android.application"
31+
const val KOTLIN_ANDROID = "kotlin-android"
32+
const val KOTLIN_ANDROID_EXT = "kotlin-android-extensions"
33+
}
34+
2835
object SupportLibs {
2936
const val ANDROIDX_NAV_UI = "androidx.navigation:navigation-ui-ktx:${Versions.NAVIGATION}"
3037
const val ANDROIDX_NAV_FRAGMENT = "androidx.navigation:navigation-fragment-ktx:${Versions.NAVIGATION}"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.purewowstudio.template.plugins
2+
3+
import com.android.build.gradle.BaseExtension
4+
import org.gradle.api.JavaVersion
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.Project
7+
8+
class AppPlugin : Plugin<Project> {
9+
10+
private val Project.android: BaseExtension
11+
get() = extensions.findByName("android") as? BaseExtension
12+
?: error("Not an Android module: $name")
13+
14+
override fun apply(project: Project) =
15+
with(project) {
16+
applyPlugins()
17+
androidConfig()
18+
}
19+
20+
private fun Project.applyPlugins() {
21+
plugins.run {
22+
apply(ModulePlugins.ANDROID_APP)
23+
apply(ModulePlugins.KOTLIN_ANDROID)
24+
apply(ModulePlugins.KOTLIN_ANDROID_EXT)
25+
}
26+
}
27+
28+
private fun Project.androidConfig() {
29+
android.run {
30+
compileSdkVersion(Sdk.COMPILE_SDK_VERSION)
31+
32+
defaultConfig {
33+
minSdkVersion(Sdk.MIN_SDK_VERSION)
34+
targetSdkVersion(Sdk.TARGET_SDK_VERSION)
35+
36+
applicationId = App.APP_ID
37+
versionCode = App.APP_VERSION_CODE
38+
versionName = App.APP_VERSION_NAME
39+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
40+
}
41+
compileOptions {
42+
sourceCompatibility = JavaVersion.VERSION_1_8
43+
targetCompatibility = JavaVersion.VERSION_1_8
44+
}
45+
project.tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).configureEach {
46+
kotlinOptions {
47+
jvmTarget = "1.8"
48+
}
49+
}
50+
sourceSets {
51+
getByName("main").java.srcDirs("src/main/kotlin")
52+
}
53+
buildTypes {
54+
getByName("debug") {
55+
isMinifyEnabled = false
56+
isDebuggable = true
57+
applicationIdSuffix = ".debug"
58+
versionNameSuffix = "-dev"
59+
}
60+
getByName("release") {
61+
isMinifyEnabled = true
62+
isDebuggable = false
63+
isShrinkResources = true
64+
isZipAlignEnabled = true
65+
isJniDebuggable = false
66+
isRenderscriptDebuggable = false
67+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
68+
}
69+
}
70+
71+
lintOptions {
72+
isWarningsAsErrors = true
73+
isAbortOnError = true
74+
}
75+
}
76+
}
77+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.purewowstudio.template.plugins
2+
3+
import com.android.build.gradle.BaseExtension
4+
import org.gradle.api.JavaVersion
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.Project
7+
8+
class LibraryPlugin : Plugin<Project> {
9+
10+
private val Project.android: BaseExtension
11+
get() = extensions.findByName("android") as? BaseExtension
12+
?: error("Not an Android module: $name")
13+
14+
override fun apply(project: Project) =
15+
with(project) {
16+
applyPlugins()
17+
androidConfig()
18+
}
19+
20+
private fun Project.applyPlugins() {
21+
plugins.run {
22+
apply(ModulePlugins.ANDROID_LIBRARY)
23+
apply(ModulePlugins.KOTLIN_ANDROID)
24+
apply(ModulePlugins.KOTLIN_ANDROID_EXT)
25+
}
26+
}
27+
28+
private fun Project.androidConfig() {
29+
android.run {
30+
compileSdkVersion(Sdk.COMPILE_SDK_VERSION)
31+
32+
defaultConfig {
33+
minSdkVersion(Sdk.MIN_SDK_VERSION)
34+
targetSdkVersion(Sdk.TARGET_SDK_VERSION)
35+
36+
versionCode = ModuleVersions.LIBRARY_VERSION_CODE
37+
versionName = ModuleVersions.LIBRARY_VERSION
38+
39+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
40+
consumerProguardFiles("consumer-rules.pro")
41+
}
42+
43+
compileOptions {
44+
sourceCompatibility = JavaVersion.VERSION_1_8
45+
targetCompatibility = JavaVersion.VERSION_1_8
46+
}
47+
48+
buildTypes {
49+
getByName("release") {
50+
isMinifyEnabled = false
51+
proguardFiles(
52+
getDefaultProguardFile("proguard-android-optimize.txt"),
53+
"proguard-rules.pro"
54+
)
55+
}
56+
}
57+
58+
sourceSets {
59+
getByName("main").java.srcDirs("src/main/kotlin")
60+
}
61+
62+
lintOptions {
63+
isWarningsAsErrors = true
64+
isAbortOnError = true
65+
}
66+
}
67+
}
68+
}

modules/android/build.gradle.kts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,10 @@
11
version = ModuleVersions.LIBRARY_VERSION
22

33
plugins {
4-
id("com.android.library")
5-
kotlin("android")
6-
id("kotlin-android-extensions")
7-
id("jacoco")
4+
id("library-plugin")
85
}
96

107
android {
11-
compileSdkVersion(Sdk.COMPILE_SDK_VERSION)
12-
13-
defaultConfig {
14-
minSdkVersion(Sdk.MIN_SDK_VERSION)
15-
targetSdkVersion(Sdk.TARGET_SDK_VERSION)
16-
17-
versionCode = ModuleVersions.LIBRARY_VERSION_CODE
18-
versionName = ModuleVersions.LIBRARY_VERSION
19-
20-
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
21-
consumerProguardFiles("consumer-rules.pro")
22-
}
23-
24-
compileOptions {
25-
sourceCompatibility = JavaVersion.VERSION_1_8
26-
targetCompatibility = JavaVersion.VERSION_1_8
27-
}
28-
29-
buildTypes {
30-
getByName("release") {
31-
isMinifyEnabled = false
32-
proguardFiles(
33-
getDefaultProguardFile("proguard-android-optimize.txt"),
34-
"proguard-rules.pro"
35-
)
36-
}
37-
}
38-
39-
sourceSets {
40-
getByName("main").java.srcDirs("src/main/kotlin")
41-
}
42-
43-
lintOptions {
44-
isWarningsAsErrors = true
45-
isAbortOnError = true
46-
}
478
}
489

4910
dependencies {

settings.gradle.kts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
pluginManagement {
2-
resolutionStrategy {
3-
eachPlugin {
4-
if (requested.id.id == "com.android.library") {
5-
useModule("com.android.tools.build:gradle:${requested.version}")
6-
}
7-
if (requested.id.id == "com.android.application") {
8-
useModule("com.android.tools.build:gradle:${requested.version}")
9-
}
10-
}
11-
}
12-
repositories {
13-
gradlePluginPortal()
14-
google()
15-
mavenCentral()
16-
jcenter()
17-
}
18-
}
19-
201
include(":app", "modules:android")
212
rootProject.name = "template"

0 commit comments

Comments
 (0)