Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp Gradle scripts #588

Merged
merged 4 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 10 additions & 182 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,186 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.ltgt.gradle.errorprone.CheckSeverity
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile



buildscript {
apply from: rootProject.file('gradle/dependencies.gradle')
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath deps.build.gradlePlugins.android
classpath deps.build.gradlePlugins.errorprone
classpath deps.build.gradlePlugins.kotlin
classpath deps.build.gradlePlugins.spotless
classpath deps.build.gradlePlugins.gradleMavenPublish
}
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.kotlinKapt) apply false
alias(libs.plugins.mavenPublish) apply false
alias(libs.plugins.errorprone) apply false
alias(libs.plugins.nullaway) apply false
alias(libs.plugins.intellij) apply false
alias(libs.plugins.spotless) apply false
}

Set<String> useErrorProneProjects = [
"memory-leaks",
"tutorial1",
"tutorial2"
]

def moduleFriends = [
'rib-android': 'rib-base',
'rib-debug-utils': 'rib-base',
'rib-router-navigator': 'rib-base',
'rib-test': 'rib-base',
'rib-workflow-test': 'rib-workflow',
'rib-coroutines-test': 'rib-coroutines'
]

subprojects {
buildscript {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}

repositories {
google()
mavenCentral()
}

apply plugin: 'com.diffplug.spotless'
spotless {
format 'misc', {
target '**/*.md', '**/.gitignore'

trimTrailingWhitespace()
endWithNewline()
}
kotlin {
target "**/*.kt"
ktlint(deps.versions.ktlint).editorConfigOverride(['indent_size': '2', 'continuation_indent_size' : '4'])
ktfmt(deps.versions.ktfmt).googleStyle()
licenseHeaderFile rootProject.file('config/spotless/copyright.kt')
trimTrailingWhitespace()
endWithNewline()
}
java {
target "src/*/java/**/*.java"
googleJavaFormat(deps.versions.gjf)
licenseHeaderFile rootProject.file('config/spotless/copyright.java')
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
}
groovyGradle {
target '**/*.gradle'
trimTrailingWhitespace()
endWithNewline()
}
}

afterEvaluate {
boolean isAndroidApp = project.plugins.hasPlugin("com.android.application")
boolean isAndroidLibrary = project.plugins.hasPlugin("com.android.library")
boolean isIntelliJPlugin = project.plugins.hasPlugin("com.android.library")
boolean usesErrorProne = project.name in useErrorProneProjects
boolean isKotlinLibrary = project.plugins.hasPlugin("org.jetbrains.kotlin.jvm") || project.plugins.hasPlugin("org.jetbrains.kotlin.android")

if ((isAndroidLibrary || isAndroidApp) && usesErrorProne) {
def configurer = { variant ->
variant.getJavaCompileProvider().configure {
options.errorprone.nullaway {
severity = CheckSeverity.ERROR
annotatedPackages.add("com.uber")
}
options.errorprone.excludedPaths = ".*/build/generated/.*"
}
}
if (isAndroidLibrary) {
project.android.libraryVariants.configureEach(configurer)
}
if (isAndroidApp) {
project.android.applicationVariants.configureEach(configurer)
}
project.android.testVariants.configureEach(configurer)
project.android.unitTestVariants.configureEach(configurer)
}
if (isAndroidLibrary || isAndroidApp) {
// TODO replace with https://issuetracker.google.com/issues/72050365 once released.
project.android {
if (isAndroidLibrary) {
variantFilter { variant ->
if (variant.buildType.name == 'debug') {
variant.setIgnore(true)
}
}
}
if (isAndroidApp) {
buildTypes {
debug {
matchingFallbacks = ['release']
}
}
variantFilter { variant ->
if (variant.buildType.name == "release") {
variant.setIgnore(true)
}
}
}
}
}

if (isKotlinLibrary) {
def extraCompilerArgs = [
// See https://github.com/bazelbuild/bazel/issues/15144
"-Xjvm-default=enable",
"-opt-in=kotlin.RequiresOptIn",
]
if (project.name in moduleFriends.keySet()) {
def friendName = moduleFriends[project.name]
def friendProject = rootProject.subprojects.stream().filter { it.name == friendName }.findFirst().get()
def outputJarPath = friendProject.plugins.hasPlugin("com.android.library")
? "build/intermediates/compile_library_classes_jar/release/classes.jar"
: "build/libs/$friendName-${project.property('VERSION_NAME')}.jar"
def friendPath="${project.rootDir}/libraries/${moduleFriends[project.name]}/$outputJarPath"
extraCompilerArgs.add("-Xfriend-paths=$friendPath")
}
if (isAndroidLibrary || isAndroidApp) {
project.android.sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}

tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
freeCompilerArgs = extraCompilerArgs
jvmTarget = "11"
}
}
} else if(!isIntelliJPlugin) {
project.compileKotlin {
kotlinOptions {
freeCompilerArgs = extraCompilerArgs
jvmTarget = "11"
}
}
project.compileTestKotlin {
kotlinOptions {
freeCompilerArgs = extraCompilerArgs
jvmTarget = "11"
}
}
}
}
}
}

tasks.register('clean', Delete) {
delete rootProject.buildDir
}

apply from: rootProject.file('gradle/japicmp.gradle')
37 changes: 37 additions & 0 deletions android/conventions/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2023. Uber Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
}

repositories {
google()
mavenCentral()
gradlePluginPortal()
}

dependencies {
// Workaround for using version catalog on precompiled scripts.
// https://github.com/gradle/gradle/issues/15383#issuecomment-779893192
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
implementation(gradleApi())
implementation(libs.gradle.android.plugin)
implementation(libs.gradle.kotlin.plugin)
implementation(libs.gradle.errorprone.plugin)
implementation(libs.gradle.nullaway.plugin)
implementation(libs.gradle.spotless.plugin)
}
24 changes: 24 additions & 0 deletions android/conventions/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2023. Uber Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}

rootProject.name = "conventions"
47 changes: 47 additions & 0 deletions android/conventions/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2023. Uber Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.android.build.gradle.AbstractAppExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.TestedExtension
import com.android.build.gradle.api.BaseVariant
import net.ltgt.gradle.errorprone.CheckSeverity
import net.ltgt.gradle.errorprone.errorprone
import net.ltgt.gradle.nullaway.nullaway

fun AbstractAppExtension.errorprone() {
applicationVariants.configureEach { errorprone() }
testErrorprone()
}

fun LibraryExtension.errorprone() {
libraryVariants.configureEach { errorprone() }
testErrorprone()
}

fun TestedExtension.testErrorprone() {
testVariants.configureEach { errorprone() }
unitTestVariants.configureEach { errorprone() }
}

fun BaseVariant.errorprone() {
javaCompileProvider.configure {
options.errorprone.nullaway {
severity.set(CheckSeverity.ERROR)
annotatedPackages.add("com.uber")
}
options.errorprone.excludedPaths.set(".*/build/generated/.*")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2023. Uber Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("UnstableApiUsage")

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("android")
id("com.android.application")
id("ribs.spotless-convention")
}

kotlin {
jvmToolchain(11)
}

android {
compileSdk = 33

defaultConfig {
minSdk = 21
targetSdk = 28
versionCode = 1
versionName = "1.0"
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

// No need for lint. Those are just tutorials.
lint {
abortOnError = false
quiet = true
}

buildTypes {
debug {
matchingFallbacks.add("release")
}
}

sourceSets {
getByName("main").java.srcDir("src/main/kotlin")
getByName("test").java.srcDir("src/test/kotlin")
getByName("androidTest").java.srcDir("src/androidTest/kotlin")
}
}

androidComponents {
beforeVariants { variantBuilder ->
if (variantBuilder.buildType == "release") {
variantBuilder.enable = false
}
}
}

tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
freeCompilerArgs.addAll(
"-Xjvm-default=enable",
"-opt-in=kotlin.RequiresOptIn",
)
}
}
Loading