[Gradle] Fix common sources are not included for kaptGenerateStubs task

^KT-61622 Fixed
This commit is contained in:
Yahor Berdnikau
2023-09-15 21:59:37 +02:00
committed by Space Team
parent 005e3c0939
commit ac18f6e16e
11 changed files with 178 additions and 7 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.gradle.android
import org.gradle.api.JavaVersion
import org.gradle.api.logging.LogLevel
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.Kapt3BaseIT
import org.jetbrains.kotlin.gradle.testbase.*
@@ -210,4 +211,33 @@ open class Kapt3AndroidExternalIT : Kapt3BaseIT() {
}
}
}
@DisplayName("KT-61622: common sources are attached in MPP + Android project")
@GradleAndroidTest
fun testMppAndroidKapt(
gradleVersion: GradleVersion,
agpVersion: String,
jdkVersion: JdkVersions.ProvidedJdk
) {
project(
"mpp-android-kapt".withPrefix,
gradleVersion,
buildOptions = defaultBuildOptions.copy(androidVersion = agpVersion, logLevel = LogLevel.DEBUG),
buildJdk = jdkVersion.location
) {
build(":shared:compileDebugKotlinAndroid") {
assertTasksExecuted(
":shared:kaptGenerateStubsDebugKotlinAndroid",
":shared:kaptDebugKotlinAndroid",
":shared:compileDebugKotlinAndroid",
)
assertCompilerArguments(
":shared:kaptGenerateStubsDebugKotlinAndroid",
"src/commonMain/kotlin/hilt/error/sampleapp/Annotations.kt",
"src/commonMain/kotlin/hilt/error/sampleapp/CommonMainViewModel.kt",
)
}
}
}
}
@@ -0,0 +1,7 @@
plugins {
id("com.android.application").apply(false)
id("com.android.library").apply(false)
kotlin("android").apply(false)
kotlin("multiplatform").apply(false)
id("com.google.dagger.hilt.android") version "2.47" apply false
}
@@ -0,0 +1,7 @@
#Android
android.useAndroidX=true
android.nonTransitiveRClass=true
#MPP
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.androidSourceSetLayoutVersion=2
@@ -0,0 +1,42 @@
plugins {
kotlin("multiplatform")
id("com.android.library")
kotlin("kapt")
}
repositories {
mavenLocal()
google()
mavenCentral()
}
kotlin {
jvmToolchain(17)
android {}
sourceSets {
val androidMain by getting {
dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
implementation("androidx.hilt:hilt-navigation-compose:1.1.0-alpha01")
implementation("com.google.dagger:hilt-android:2.4.7")
configurations.getByName("kapt").dependencies.add(
org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency(
"com.google.dagger",
"hilt-compiler",
"2.47"
)
)
}
}
}
}
android {
namespace = "hilt.error.sampleapp"
compileSdk = 34
defaultConfig {
minSdk = 24
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
@@ -0,0 +1,20 @@
package hilt.error.sampleapp
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
/***
* This is just a standard HiltViewModel. This works as intended
*/
@HiltViewModel
class AndroidMainViewModel @Inject constructor(
dependency: AndroidDependency
) : ViewModel() {
val text: String = dependency.text
}
class AndroidDependency @Inject constructor() {
val text = "Hello AndroidMain"
}
@@ -0,0 +1,16 @@
@file:OptIn(ExperimentalMultiplatform::class)
package hilt.error.sampleapp
/**
* Issue: prior to 1.9 this would be fine. Now, Hilt cannot find viewmodels marked with this typealias
*/
actual typealias HiltViewModel = dagger.hilt.android.lifecycle.HiltViewModel
@AllowDifferentMembersInActual
actual typealias ViewModel = androidx.lifecycle.ViewModel
actual typealias Inject = javax.inject.Inject
actual typealias AutoCloseable = java.lang.AutoCloseable
actual typealias Closeable = java.io.Closeable
@@ -0,0 +1,13 @@
package hilt.error.sampleapp
expect annotation class HiltViewModel()
expect interface AutoCloseable {
fun close()
}
expect interface Closeable : AutoCloseable
expect abstract class ViewModel()
expect annotation class Inject()
@@ -0,0 +1,17 @@
package hilt.error.sampleapp
/**
* This is a shared view model. It is marked with a typealias to a HiltViewModel
* But does not get added to Hilts view model map (from kotlin 1.90 onwards)
*/
@HiltViewModel
class CommonMainViewModel @Inject constructor(
dependency: CommonMainDependency
) : ViewModel() {
val text = dependency.text
}
class CommonMainDependency @Inject constructor() {
val text = "Hello CommonMain"
}
@@ -8,6 +8,8 @@
package org.jetbrains.kotlin.gradle.plugin.mpp.compilationImpl
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.internal.KAPT_GENERATE_STUBS_PREFIX
import org.jetbrains.kotlin.gradle.internal.getKaptTaskName
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
@@ -17,6 +19,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.addSourcesToKotlinCompileTask
import org.jetbrains.kotlin.gradle.plugin.sources.defaultSourceSetLanguageSettingsChecker
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.gradle.utils.addExtendsFromRelation
import org.jetbrains.kotlin.gradle.utils.whenKaptEnabled
import org.jetbrains.kotlin.tooling.core.extrasFactoryProperty
import java.util.concurrent.Callable
@@ -89,13 +92,26 @@ internal class KotlinCompilationSourceSetInclusion(
object DefaultAddSourcesToCompileTask : AddSourcesToCompileTask {
override fun addSources(
compilation: KotlinCompilation<*>, sourceSet: KotlinSourceSet, addAsCommonSources: Lazy<Boolean>
) = addSourcesToKotlinCompileTask(
project = compilation.project,
taskName = compilation.compileKotlinTaskName,
sourceFileExtensions = sourceSet.customSourceFilesExtensions,
addAsCommonSources = addAsCommonSources,
sources = { sourceSet.kotlin }
)
) {
addSourcesToKotlinCompileTask(
project = compilation.project,
taskName = compilation.compileKotlinTaskName,
sourceFileExtensions = sourceSet.customSourceFilesExtensions,
addAsCommonSources = addAsCommonSources,
sources = { sourceSet.kotlin }
)
compilation.project.whenKaptEnabled {
val kaptGenerateStubsTaskName = getKaptTaskName(compilation.compileKotlinTaskName, KAPT_GENERATE_STUBS_PREFIX)
addSourcesToKotlinCompileTask(
project = compilation.project,
taskName = kaptGenerateStubsTaskName,
sourceFileExtensions = sourceSet.customSourceFilesExtensions,
addAsCommonSources = addAsCommonSources,
sources = { sourceSet.kotlin }
)
}
}
}
object NativeAddSourcesToCompileTask : AddSourcesToCompileTask {