简体   繁体   中英

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.

im having difficulty in configuring the optics module that employs ksp for source code generation

my project gradle resembles this

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

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

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? what mistakes have i made in my gradle build files?

the solution i found was to downgrade the arrow gradle deps as shown below

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. 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. 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")
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM