8

I am creating a Jetbrains Compose Multiplatform project. But I will only need Desktop (JVM) and Android, so two JVM targets. I want to factor out the logic which needs grpc, so both Android and Desktop can use it without me having to program it twice.

I can't seem to figure out a way of binding in my grpc/proto into the project, so that I can write the logic once and share it between android and desktop. This is what my build.gradle.kts of the common project looks like:

plugins {
    id("com.android.library")
    kotlin("multiplatform")
    id("org.jetbrains.compose")

}

kotlin {
    android()
    jvm("desktop")

    sourceSets {
        named("commonMain") {
            dependencies {
                api(compose.runtime)
                api(compose.foundation)
                api(compose.material)
                api(compose.ui)
                implementation(compose.preview)
                implementation(compose.uiTooling)
            }
        }
    }
}

android {
    compileSdk = 31

    defaultConfig {
        minSdk = 21
        targetSdk = 31
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    sourceSets {
        named("main") {
            manifest.srcFile("src/androidMain/AndroidManifest.xml")
            res.srcDirs("src/androidMain/res")
        }
    }
}

I tried binding my protos into a sourceset but couldn't get it working.


My other approach: Next I tried creating a second submodule where the protos and the logic would be bound, but I couldn't get that working either: Here is the build.gradle.kts for shared-logic:

import com.google.protobuf.gradle.*
import org.gradle.kotlin.dsl.proto

plugins {
    kotlin("jvm")

    idea

    id("com.google.protobuf")
}

version = "unspecified"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib"))
    api(project(":kotlin-common")) {
        exclude(group = "io.grpc", module = "grpc-protobuf")
        exclude(group = "io.grpc", module = "grpc-stub")
    }

    implementation("io.grpc:grpc-okhttp:${Versions.GRPC}")
    api("com.google.protobuf:protobuf-java-util:${Versions.PROTOBUF}")

    api("io.grpc:grpc-stub:${Versions.GRPC}")
    api("io.grpc:grpc-protobuf-lite:${Versions.GRPC}")
    api("io.grpc:grpc-kotlin-stub:${Versions.GRPC_KOTLIN}")
    api("com.google.protobuf:protobuf-kotlin-lite:${Versions.PROTOBUF}")

    api("io.insert-koin:koin-core:${Versions.KOIN}")
    api("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.COROUTINES}")
}

sourceSets {
    main {
        proto {
            srcDirs("../protos/src/main")
        }
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:${Versions.PROTOBUF}"
    }

    plugins {
        id("java") {
            artifact = "io.grpc:protoc-gen-grpc-java:${Versions.GRPC}"
        }

        id("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:${Versions.GRPC}"
        }
        id("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:${Versions.GRPC_KOTLIN}:jdk7@jar"
        }
    }
    generateProtoTasks {
        all().forEach {
            it.plugins {
                id("grpc") {
                    option("lite")
                }
                id("grpckt") {
                    option("lite")
                }
            }

            it.builtins {
                id("kotlin") {
                    option("lite")
                }
            }
        }
    }
}

Here is the build.gradle.kts for kotlin-common:

plugins {
    kotlin("jvm")
}

version = "1.0.0"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib"))

    implementation("io.grpc:grpc-stub:${Versions.GRPC}")
}

Here I get resolution error I tried fixing but couldn't figure out what to exclude:

Duplicate class com.google.protobuf.AbstractMessageLite found in modules jetified-protobuf-java-3.19.1 (com.google.protobuf:protobuf-java:3.19.1) and jetified-protobuf-javalite-3.19.1 (com.google.protobuf:protobuf-javalite:3.19.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules jetified-protobuf-java-3.19.1 (com.google.protobuf:protobuf-java:3.19.1) and jetified-protobuf-javalite-3.19.1 (com.google.protobuf:protobuf-javalite:3.19.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder$LimitedInputStream found in modules jetified-protobuf-java-3.19.1 (com.google.protobuf:protobuf-java:3.19.1) and jetified-protobuf-javalite-3.19.1 (com.google.protobuf:protobuf-javalite:3.19.1)
...
Intektor
  • 321
  • 2
  • 13

0 Answers0