简体   繁体   English

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

[英]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.

相关问题 如何在 Android Studio Bumblebee 中将 build.gradle 从 Groovy 迁移到 Kotlin DSL? - How to Migrate build.gradle from Groovy to Kotlin DSL in Android Studio Bumblebee? 如何将保护级别 build.gradle 的文件从 Gradle Groovy 迁移到 Gradle Kotlin DSL - How to migrate protect level build.gradle file from Gradle Groovy to Gradle Kotlin DSL 如何在androidstudio和我的android项目中配置gradle - how to configure gradle in androidstudio and my android project Gradle突然无法建立。 找不到Gradle DSL方法:“ android()” - Gradle suddenly will not build. Gradle DSL method not found: 'android()' Android Gradle构建错误:(65,0)未找到Gradle DSL方法:“ compile()” - Android gradle build Error:(65, 0) Gradle DSL method not found: 'compile()' Android gradle build错误:(9,0)未找到Gradle DSL方法:'compile()'。 - Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'. Android Gradle构建错误:(10,0)未找到Gradle DSL方法:“ compile()” - Android gradle build Error:(10, 0) Gradle DSL method not found: 'compile()' 如何正确配置AdobeCreative SDK for Android Gradle构建..正确 - How to Configure AdobeCreative SDK for Android gradle build..Correctly 如何在我的项目的build.gradle文件中添加Android库(在Android Studio中)? - How to add an Android library to my project's build.gradle files (in Android Studio)? 如何配置build.gradle - How to configure build.gradle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM