build: convert to Gradle Kotlin DSL

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-11-06 17:28:26 +05:30
parent 9839bf4014
commit 1e2fef884f
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
19 changed files with 514 additions and 230 deletions

View file

@ -0,0 +1,98 @@
/*
* Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.android.build.gradle.TestedExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.api.tasks.wrapper.Wrapper
import org.gradle.kotlin.dsl.repositories
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
/**
* Configure root project.
* Note that classpath dependencies still need to be defined in the `buildscript` block in the top-level build.gradle.kts file.
*/
internal fun Project.configureForRootProject() {
// register task for cleaning the build directory in the root project
tasks.register("clean", Delete::class.java) {
delete(rootProject.buildDir)
}
tasks.withType<Wrapper> {
gradleVersion = "6.7"
distributionType = Wrapper.DistributionType.ALL
distributionSha256Sum = "0080de8491f0918e4f529a6db6820fa0b9e818ee2386117f4394f95feb1d5583"
}
}
/**
* Configure all projects including the root project
*/
internal fun Project.configureForAllProjects() {
repositories {
google()
jcenter()
maven { setUrl("https://jitpack.io") }
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
freeCompilerArgs = freeCompilerArgs + additionalCompilerArgs
languageVersion = "1.4"
}
}
tasks.withType<Test> {
maxParallelForks = Runtime.getRuntime().availableProcessors() * 2
testLogging {
events(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}
}
/**
* Apply configuration options for Android Application projects.
*/
@Suppress("UnstableApiUsage")
internal fun BaseAppModuleExtension.configureAndroidApplicationOptions(project: Project) {
project.tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + listOf(
"-Xopt-in=kotlin.RequiresOptIn",
"-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-Xopt-in=androidx.compose.material.ExperimentalMaterialApi"
)
}
}
}
/**
* Apply baseline configurations for all Android projects (Application and Library).
*/
@Suppress("UnstableApiUsage")
internal fun TestedExtension.configureCommonAndroidOptions() {
compileSdkVersion(30)
defaultConfig {
minSdkVersion(23)
targetSdkVersion(30)
}
packagingOptions {
exclude("**/*.version")
exclude("**/*.txt")
exclude("**/*.kotlin_module")
exclude("**/plugin.properties")
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
testOptions.animationsDisabled = true
}

View file

@ -0,0 +1,34 @@
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.TestedExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
/**
* A plugin that enables Java 8 desugaring for consuming new Java language APIs.
*
* Apply this plugin to the build.gradle.kts file in Android Application or Android Library projects:
* ```
* plugins {
* `core-library-desugaring`
* }
* ```
*/
class CoreLibraryDesugaringPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.plugins.withType<AppPlugin> {
project.extensions.getByType<TestedExtension>().configure(project)
}
project.plugins.withType<LibraryPlugin> {
project.extensions.getByType<TestedExtension>().configure(project)
}
}
private fun TestedExtension.configure(project: Project) {
compileOptions.isCoreLibraryDesugaringEnabled = true
project.dependencies.add("coreLibraryDesugaring", Dependencies.AndroidX.coreLibraryDesugaring)
}
}

View file

@ -0,0 +1,111 @@
/*
* Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
private const val ANDROIDX_HILT_VERSION = "1.0.0-alpha02"
private const val DAGGER_HILT_VERSION = "2.29.1-alpha"
object Dependencies {
const val COMPOSE_VERSION = "1.0.0-alpha06"
object Kotlin {
object Coroutines {
private const val version = "1.4.1"
const val android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version"
const val core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version"
}
}
object AndroidX {
const val activityKtx = "androidx.activity:activity-ktx:1.2.0-beta01"
const val appCompat = "androidx.appcompat:appcompat:1.3.0-alpha02"
const val browser = "androidx.browser:browser:1.3.0-beta01"
const val coreKtx = "androidx.core:core-ktx:1.5.0-alpha04"
const val coreLibraryDesugaring = "com.android.tools:desugar_jdk_libs:1.0.10"
const val material = "com.google.android.material:material:1.3.0-alpha03"
object Compose {
const val compiler = "androidx.compose.compiler:compiler:$COMPOSE_VERSION"
const val foundation = "androidx.compose.foundation:foundation:$COMPOSE_VERSION"
const val foundationLayout = "androidx.compose.foundation:foundation-layout:$COMPOSE_VERSION"
const val foundationText = "androidx.compose.foundation:foundation-text:$COMPOSE_VERSION"
const val material = "androidx.compose.material:material:$COMPOSE_VERSION"
const val navigation = "androidx.navigation:navigation-compose:1.0.0-alpha01"
const val runtime = "androidx.compose.runtime:runtime:$COMPOSE_VERSION"
const val ui = "androidx.compose.ui:ui:$COMPOSE_VERSION"
const val uiText = "androidx.compose.ui:ui-text:$COMPOSE_VERSION"
const val uiTextAndroid = "androidx.compose.ui:ui-text-android:$COMPOSE_VERSION"
const val uiUnit = "androidx.compose.ui:ui-unit:$COMPOSE_VERSION"
const val uiTooling = "androidx.ui:ui-tooling:$COMPOSE_VERSION"
}
object Hilt {
const val dagger = "com.google.dagger:hilt-android:$DAGGER_HILT_VERSION"
const val daggerCompiler = "com.google.dagger:hilt-compiler:$DAGGER_HILT_VERSION"
const val daggerHiltCompiler = "androidx.hilt:hilt-compiler:$ANDROIDX_HILT_VERSION"
const val hiltLifecycleViewmodel =
"androidx.hilt:hilt-lifecycle-viewmodel:$ANDROIDX_HILT_VERSION"
}
object Lifecycle {
private const val version = "2.3.0-beta01"
const val runtimeKtx = "androidx.lifecycle:lifecycle-runtime-ktx:$version"
const val viewmodelKtx = "androidx.lifecycle:lifecycle-viewmodel-ktx:$version"
}
object Room {
private const val version = "2.3.0-alpha03"
const val compiler = "androidx.room:room-compiler:$version"
const val ktx = "androidx.room:room-ktx:$version"
const val runtime = "androidx.room:room-runtime:$version"
}
}
object ThirdParty {
const val accompanist = "dev.chrisbanes.accompanist:accompanist-coil:0.3.2"
const val customtabs = "saschpe.android:customtabs:3.0.2"
object Moshi {
private const val version = "1.11.0"
const val codegen = "com.squareup.moshi:moshi-kotlin-codegen:$version"
const val lib = "com.squareup.moshi:moshi:$version"
}
object Retrofit {
private const val version = "2.9.0"
const val lib = "com.squareup.retrofit2:retrofit:$version"
const val moshi = "com.squareup.retrofit2:converter-moshi:$version"
}
object Roomigrant {
private const val version = "0.2.0"
const val compiler = "com.github.MatrixDev.Roomigrant:RoomigrantCompiler:$version"
const val runtime = "com.github.MatrixDev.Roomigrant:RoomigrantLib:$version"
}
}
object Testing {
const val daggerHilt = "com.google.dagger:hilt-android-testing:$DAGGER_HILT_VERSION"
const val junit = "junit:junit:4.13.1"
const val mockWebServer = "com.squareup.okhttp3:mockwebserver:3.14.9"
const val uiTest = "androidx.ui:ui-test:$COMPOSE_VERSION"
object AndroidX {
private const val version = "1.3.1-alpha02"
const val runner = "androidx.test:runner:$version"
const val rules = "androidx.test:rules:$version"
}
}
}

View file

@ -0,0 +1,17 @@
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.jetbrains.kotlin.gradle.plugin.KaptExtension
/**
* Apply default kapt configs to the [Project].
*/
internal fun Project.configureKapt() {
extensions.configure<KaptExtension> {
javacOptions {
option("-Adagger.fastInit=enabled")
option("-Adagger.experimentalDaggerErrorMessages=enabled")
option("-Xmaxerrs", 500)
option("-Adagger.moduleBindingValidation=ERROR")
}
}
}

View file

@ -0,0 +1,9 @@
/*
* Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
internal val additionalCompilerArgs = listOf(
"-Xallow-jvm-ir-dependencies",
"-Xskip-prerelease-check"
)

View file

@ -0,0 +1,52 @@
/*
* Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.android.build.gradle.TestedExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.android.build.gradle.internal.plugins.AppPlugin
import com.android.build.gradle.internal.plugins.LibraryPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaLibraryPlugin
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin
class LobstersPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.configureForAllProjects()
if (project.isRoot) {
project.configureForRootProject()
}
project.plugins.all {
when (this) {
is JavaPlugin,
is JavaLibraryPlugin -> {
project.tasks.withType<JavaCompile> {
options.compilerArgs.add("-Xlint:unchecked")
options.isDeprecation = true
}
}
is LibraryPlugin -> {
project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions()
}
is AppPlugin -> {
project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions()
project.extensions.getByType<BaseAppModuleExtension>().configureAndroidApplicationOptions(project)
}
is Kapt3GradleSubplugin -> {
project.configureKapt()
}
}
}
}
}
private val Project.isRoot get() = this == this.rootProject