[英]how to configure ksp in my android gradle groovy dsl build files
im investigating the io.arrow.kt functional programming library in my current android project.我正在调查我当前 android 项目中的 io.arrow.kt 函数式编程库。
im having difficulty in configuring the optics module that employs ksp for source code generation我在配置使用 ksp 生成源代码的光学模块时遇到困难
my project gradle
resembles this我的项目
gradle
类似于这个
buildscript {
ext.kotlin_version = "1.6.10"
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
}
}
allprojects {
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
with my android sub module resembling this与我的 android 子模块类似
plugins {
id "com.android.library"
id "com.google.devtools.ksp" version "1.6.10-1.0.2"
id "kotlin-android"
id "kotlin-kapt"
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.20'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 32
defaultConfig {
minSdkVersion 26
targetSdkVersion 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
...
dependencies {
api(platform("io.arrow-kt:arrow-stack:1.0.6-alpha.1"))
api("io.arrow-kt:arrow-core")
api("io.arrow-kt:arrow-fx-coroutines")
api("io.arrow-kt:arrow-fx-stm")
api("io.arrow-kt:arrow-optics")
api "com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.2"
ksp "io.arrow-kt:arrow-optics-ksp-plugin:1.0.6-alpha.1"
the optics code within my android kotlin code does not compile though我的 android kotlin 代码中的光学代码无法编译
import arrow.optics.dsl.*
import arrow.optics.Optional
import arrow.optics.optics
@optics
data class Street(val number: Int, val name: String) {
companion object
}
@optics
data class Address(val city: String, val street: Street) {
companion object
}
@optics
data class Company(val name: String, val address: Address) {
companion object
}
@optics
data class Employee(val name: String, val company: Company?) {
companion object
}
fun what() {
val john = Employee("John Doe", Company("Kategory", Address("Functional city", Street(42, "lambda street"))))
//
// val optional: Optional<Employee, String> = Employee.company.address.street.name
//
// optional.modify(john, String::toUpperCase)
}
as company is not found in the commented out code因为在注释掉的代码中找不到公司
does ksp work on android? ksp 在 android 上工作吗? what mistakes have i made in my gradle build files?
我在 gradle 构建文件中犯了什么错误?
the solution i found was to downgrade the arrow gradle deps as shown below我找到的解决方案是降级箭头 gradle deps 如下所示
implementation(platform("io.arrow-kt:arrow-stack:1.0.3-alpha.1"))
api("io.arrow-kt:arrow-core")
api("io.arrow-kt:arrow-fx-coroutines")
api("io.arrow-kt:arrow-fx-stm")
api 'io.arrow-kt:arrow-optics'
api "com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.2"
i also had to add我还得补充
sourceSets {
main {
java {
srcDir "${buildDir.absolutePath}/generated/ksp/"
}
}
}
Although @Hector's solution works, it has a drawback.尽管@Hector 的解决方案有效,但它有一个缺点。 If you have multiple flavors, when switching between build variants, you will have to clean the directory with the generated ksp classes, because they will conflict and break the assembly.
如果您有多种风格,则在构建变体之间切换时,您必须使用生成的 ksp 类清理目录,因为它们会发生冲突并破坏程序集。 The snippet below solves such an issue.
下面的代码片段解决了这样的问题。
android {
// ...
androidComponents.onVariants { variant ->
val name = variant.name
sourceSets {
getByName(name).kotlin.srcDir("${buildDir.absolutePath}/generated/ksp/${name}/kotlin")
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.