Skip to content

Commit

Permalink
Migrate anvil contribution merging to KSP (#902)
Browse files Browse the repository at this point in the history
**Background**

This PR migrates current kapt-based Anvil contribution merging to KSP
one.
Works with K2 🙂

**Changes**

- Replaced `flipper.anvil.kapt` plugin with `flipper.anvil.entrypoint`
- Upgraded Anvil to ZacSweers Anvil 0.3.3
- Upgraded `anvil-utils` to `0.3.0-beta01`. It supports ZacSweers Anvil
plugin.
- Introduced new `commonKsp` configuration to remove warnings about
usage of `ksp` configuration in KMP projects
- Use default kotlin hierarchy instead of specifying `dependsOn`
manually.
- Disabled warning about `expect/actual` classes being in Beta

**Test plan**

If it builds, it works

---------

Co-authored-by: Nikita Kulikov <[email protected]>
Co-authored-by: Roman Makeev <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2024
1 parent 8f293e5 commit 1e2564a
Show file tree
Hide file tree
Showing 27 changed files with 87 additions and 133 deletions.
2 changes: 1 addition & 1 deletion build-logic/plugins/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
implementation(libs.protobuf.gradle)
implementation(libs.grgit.gradle)
implementation(libs.kotlin.ksp.gradle)
implementation(libs.square.anvil.gradle)
implementation(libs.zacsweers.anvil.gradle)
implementation(libs.compose.multiplatform.gradle)
implementation(libs.compose.gradle)
implementation(libs.kotlin.jvm.gradle)
Expand Down
11 changes: 3 additions & 8 deletions build-logic/plugins/convention/src/main/kotlin/commonAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ private fun BaseExtension.configureBuildFeatures() {
// TODO: Disable by default
// BuildConfig is java source code. Java and Kotlin at one time affect build speed.
buildFeatures.buildConfig = true
// Disable by default. ViewBinding needed only for few modules.
// No need to enable this feature for all modules.
buildFeatures.viewBinding = false
buildFeatures.aidl = false
buildFeatures.compose = false
buildFeatures.prefab = false
buildFeatures.renderScript = false
buildFeatures.resValues = false
buildFeatures.shaders = false
}
Expand All @@ -105,6 +98,8 @@ private fun Project.suppressOptIn() {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_11)

freeCompilerArgs.add("-Xexpect-actual-classes")

optIn.addAll(
"com.google.accompanist.pager.ExperimentalPagerApi",
"androidx.compose.ui.ExperimentalComposeUiApi",
Expand All @@ -115,7 +110,7 @@ private fun Project.suppressOptIn() {
"kotlin.time.ExperimentalTime",
"kotlin.RequiresOptIn",
"androidx.compose.animation.ExperimentalAnimationApi",
"com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi", // ktlint-disable max-line-length
"com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi",
"androidx.compose.foundation.layout.ExperimentalLayoutApi"
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import org.gradle.api.Project

fun Project.includeCommonKspConfigurationTo(
vararg toConfigurations: String,
) {
pluginManager.withPlugin("com.google.devtools.ksp") {
val commonKsp = configurations.create("commonKsp")
toConfigurations.forEach { configurationName ->
configurations.getByName(configurationName).extendsFrom(commonKsp)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import com.android.build.gradle.BaseExtension
import com.flipperdevices.buildlogic.ApkConfig
import com.flipperdevices.buildlogic.ApkConfig.IS_SENTRY_PUBLISH
Expand Down Expand Up @@ -54,3 +53,5 @@ configure<SentryPluginExtension> {

autoInstallation.enabled.set(false)
}

includeCommonKspConfigurationTo("ksp")
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ plugins {
configure<BaseExtension> {
commonAndroid(project)
}

includeCommonKspConfigurationTo("ksp")
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.gradle.kotlin.dsl.dependencies

plugins {
id("flipper.multiplatform")
id("com.squareup.anvil")
Expand All @@ -21,7 +19,7 @@ kotlin {

commonMain.dependencies {
implementation(libs.dagger)
implementation(libs.square.anvil.annotations)
implementation(libs.zacsweers.anvil.annotations)
implementation(libs.anvil.utils.annotations)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import com.google.devtools.ksp.gradle.KspExtension

plugins {
id("com.squareup.anvil")
}

anvil {
useKsp(
contributesAndFactoryGeneration = true,
componentMerging = true,
)
}

the<KspExtension>().apply {
arg("anvil.ksp.generateShims", "false")
}

dependencies {
"implementation"(libs.dagger)
"implementation"(libs.anvil.utils.annotations)
"commonKsp"(libs.anvil.utils.compiler)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ anvil {
generateDaggerFactories = true
}

pluginManager.withPlugin("kotlin-kapt") {
error("Please, use `id(\"flipper.anvil.kapt\")` instead")
}

dependencies {
"implementation"(libs.dagger)
"implementation"(libs.anvil.utils.annotations)
"ksp"(libs.anvil.utils.compiler)
"commonKsp"(libs.anvil.utils.compiler)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import com.android.build.gradle.BaseExtension
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
Expand All @@ -11,6 +12,7 @@ configure<BaseExtension> {
commonAndroid(project)
}

@OptIn(ExperimentalKotlinGradlePluginApi::class)
kotlin {
androidTarget {
compilerOptions {
Expand All @@ -19,51 +21,14 @@ kotlin {
}
jvm("desktop")

sourceSets {
val commonMain by getting

/**
* We shouldn't create sources, which is named as original targets sourcesets.
*
* As an example - jvm() target will create sourceSet jvmMain - and we would have conflicts
* with our create jvmMain sourceSet
*
* This is the reason to name it `jvmSharedMain`
*/
val jvmSharedMain by creating {
dependsOn(commonMain)
}

val androidMain by getting {
dependsOn(jvmSharedMain)
}

val desktopMain by getting {
dependsOn(jvmSharedMain)
}

androidMain.dependencies {
}
commonMain.dependencies {
}
desktopMain.dependencies {
}

// Testing
val commonTest by getting

val jvmSharedTest by creating {
dependsOn(commonTest)
}

@Suppress("UnusedPrivateProperty")
val androidUnitTest by getting {
dependsOn(jvmSharedTest)
}

@Suppress("UnusedPrivateProperty")
val desktopTest by getting {
dependsOn(jvmSharedTest)
applyDefaultHierarchyTemplate {
common {
group("jvmShared") {
withAndroidTarget()
withJvm()
}
}
}
}

includeCommonKspConfigurationTo("kspAndroid", "kspDesktop")
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.serialization) apply false
alias(libs.plugins.kotlin.ksp) apply false
alias(libs.plugins.square.anvil) apply false
alias(libs.plugins.zacsweers.anvil) apply false
alias(libs.plugins.protobuf) apply false
alias(libs.plugins.google.gms) apply false
alias(libs.plugins.android.test) apply false
Expand Down
5 changes: 2 additions & 3 deletions components/bridge/connection/sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id("flipper.android-app")
id("flipper.anvil.kapt")
id("kotlin-kapt")
id("flipper.anvil.entrypoint")
id("kotlinx-serialization")
}

Expand Down Expand Up @@ -98,7 +97,7 @@ dependencies {

// Dagger deps
implementation(libs.dagger)
kapt(libs.dagger.kapt)
commonKsp(libs.dagger.compiler)

implementation(libs.timber)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.flipperdevices.bridge.connection

import android.app.Application
import com.flipperdevices.bridge.connection.di.AppComponent
import com.flipperdevices.bridge.connection.di.DaggerAppComponent
import com.flipperdevices.bridge.connection.di.DaggerMergedAppComponent
import com.flipperdevices.core.di.ApplicationParams
import com.flipperdevices.core.di.ComponentHolder
import timber.log.Timber
Expand All @@ -14,7 +14,7 @@ class ConnectionTestApplication : Application() {

override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.factory()
appComponent = DaggerMergedAppComponent.factory()
.create(
context = this,
application = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import com.flipperdevices.core.di.AppGraph
import com.flipperdevices.core.di.ApplicationParams
import com.squareup.anvil.annotations.MergeComponent
import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton

@Singleton
@MergeComponent(AppGraph::class)
interface AppComponent {
fun inject(activity: ConnectionTestActivity)

@Component.Factory
@MergeComponent.Factory
interface Factory {
fun create(
@BindsInstance context: Context,
Expand Down
2 changes: 1 addition & 1 deletion components/bridge/dao/impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {

implementation(libs.room.runtime)
implementation(libs.room.ktx)
ksp(libs.room.ksp)
commonKsp(libs.room.ksp)

implementation(libs.kotlin.immutable.collections)
implementation(libs.okio)
Expand Down
5 changes: 2 additions & 3 deletions components/nfceditor/sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id("flipper.android-app")
id("flipper.anvil.kapt")
id("kotlin-kapt")
id("flipper.anvil.entrypoint")
}

android.namespace = "com.flipperdevices.nfceditor.sample"
Expand Down Expand Up @@ -47,7 +46,7 @@ dependencies {

// Dagger deps
implementation(libs.dagger)
kapt(libs.dagger.kapt)
commonKsp(libs.dagger.compiler)

implementation(libs.timber)
implementation(libs.kotlin.immutable.collections)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.flipperdevices.nfceditor.sample
import android.app.Application
import com.flipperdevices.core.di.ApplicationParams
import com.flipperdevices.nfceditor.sample.di.AppComponent
import com.flipperdevices.nfceditor.sample.di.DaggerAppComponent
import com.flipperdevices.nfceditor.sample.di.DaggerMergedAppComponent
import timber.log.Timber

class NfcEditorApplication : Application() {
Expand All @@ -13,7 +13,7 @@ class NfcEditorApplication : Application() {

override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.factory()
appComponent = DaggerMergedAppComponent.factory()
.create(
context = this,
application = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.flipperdevices.nfceditor.sample.NfcEditorActivity
import com.flipperdevices.shake2report.api.Shake2ReportApi
import com.squareup.anvil.annotations.MergeComponent
import dagger.BindsInstance
import dagger.Component
import javax.inject.Provider
import javax.inject.Singleton

Expand All @@ -18,7 +17,7 @@ interface AppComponent {
val shake2report: Provider<Shake2ReportApi>
fun inject(activity: NfcEditorActivity)

@Component.Factory
@MergeComponent.Factory
interface Factory {
fun create(
@BindsInstance context: Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ androidDependencies {
implementation(projects.components.bridge.rpc.api)

implementation(libs.dagger)
implementation(libs.square.anvil.annotations)
implementation(libs.zacsweers.anvil.annotations)
implementation(libs.ktor.client)
implementation(libs.ktor.serialization)
implementation(libs.ktor.logging)
Expand Down
2 changes: 1 addition & 1 deletion components/remote-controls/api-backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ commonDependencies {
implementation(projects.components.remoteControls.coreModel)

implementation(libs.dagger)
implementation(libs.square.anvil.annotations)
implementation(libs.zacsweers.anvil.annotations)
implementation(libs.ktor.client)
implementation(libs.ktor.serialization)
implementation(libs.ktor.logging)
Expand Down
Loading

0 comments on commit 1e2564a

Please sign in to comment.