如何在我的 android gradle groovy dsl 构建文件中配置 ksp

how to configure ksp in my android gradle groovy dsl build files

我正在调查我当前 android 项目中的 io.arrow.kt 函数式编程库。

我在配置使用 ksp 生成源代码的光学模块时遇到困难

我的项目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
}

我的 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"

虽然我的 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)
}

因为在注释掉的代码中找不到公司

ksp 是否适用于 android? 我在 gradle 构建文件中犯了什么错误?

我找到的解决方案是降级箭头 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"

我还必须添加

sourceSets {
    main {
        java {
            srcDir "${buildDir.absolutePath}/generated/ksp/"
        }
    }
}