[Gradle] Fix common sources are not included for kaptGenerateStubs task
^KT-61622 Fixed
This commit is contained in:
committed by
Space Team
parent
005e3c0939
commit
ac18f6e16e
+30
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.gradle.android
|
package org.jetbrains.kotlin.gradle.android
|
||||||
|
|
||||||
import org.gradle.api.JavaVersion
|
import org.gradle.api.JavaVersion
|
||||||
|
import org.gradle.api.logging.LogLevel
|
||||||
import org.gradle.util.GradleVersion
|
import org.gradle.util.GradleVersion
|
||||||
import org.jetbrains.kotlin.gradle.Kapt3BaseIT
|
import org.jetbrains.kotlin.gradle.Kapt3BaseIT
|
||||||
import org.jetbrains.kotlin.gradle.testbase.*
|
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",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+7
@@ -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
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
#Android
|
||||||
|
android.useAndroidX=true
|
||||||
|
android.nonTransitiveRClass=true
|
||||||
|
|
||||||
|
#MPP
|
||||||
|
kotlin.mpp.enableCInteropCommonization=true
|
||||||
|
kotlin.mpp.androidSourceSetLayoutVersion=2
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
include(":shared")
|
||||||
+42
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
|
||||||
+20
@@ -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"
|
||||||
|
}
|
||||||
+16
@@ -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
|
||||||
+13
@@ -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()
|
||||||
+17
@@ -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"
|
||||||
|
}
|
||||||
+23
-7
@@ -8,6 +8,8 @@
|
|||||||
package org.jetbrains.kotlin.gradle.plugin.mpp.compilationImpl
|
package org.jetbrains.kotlin.gradle.plugin.mpp.compilationImpl
|
||||||
|
|
||||||
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
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.KotlinCompilation
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle
|
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
|
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.plugin.sources.defaultSourceSetLanguageSettingsChecker
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
|
||||||
import org.jetbrains.kotlin.gradle.utils.addExtendsFromRelation
|
import org.jetbrains.kotlin.gradle.utils.addExtendsFromRelation
|
||||||
|
import org.jetbrains.kotlin.gradle.utils.whenKaptEnabled
|
||||||
import org.jetbrains.kotlin.tooling.core.extrasFactoryProperty
|
import org.jetbrains.kotlin.tooling.core.extrasFactoryProperty
|
||||||
import java.util.concurrent.Callable
|
import java.util.concurrent.Callable
|
||||||
|
|
||||||
@@ -89,13 +92,26 @@ internal class KotlinCompilationSourceSetInclusion(
|
|||||||
object DefaultAddSourcesToCompileTask : AddSourcesToCompileTask {
|
object DefaultAddSourcesToCompileTask : AddSourcesToCompileTask {
|
||||||
override fun addSources(
|
override fun addSources(
|
||||||
compilation: KotlinCompilation<*>, sourceSet: KotlinSourceSet, addAsCommonSources: Lazy<Boolean>
|
compilation: KotlinCompilation<*>, sourceSet: KotlinSourceSet, addAsCommonSources: Lazy<Boolean>
|
||||||
) = addSourcesToKotlinCompileTask(
|
) {
|
||||||
project = compilation.project,
|
addSourcesToKotlinCompileTask(
|
||||||
taskName = compilation.compileKotlinTaskName,
|
project = compilation.project,
|
||||||
sourceFileExtensions = sourceSet.customSourceFilesExtensions,
|
taskName = compilation.compileKotlinTaskName,
|
||||||
addAsCommonSources = addAsCommonSources,
|
sourceFileExtensions = sourceSet.customSourceFilesExtensions,
|
||||||
sources = { sourceSet.kotlin }
|
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 {
|
object NativeAddSourcesToCompileTask : AddSourcesToCompileTask {
|
||||||
|
|||||||
Reference in New Issue
Block a user