I'm working in a client-server project. Client is ...
# multiplatform
m
I'm working in a client-server project. Client is android of course, server is rails but has the ability to call javascript code(I'm unfamiliar with rails and the actually mechanism that allows for this). We have some common logic for some features that are shared between client side and server side. I'm currently rewriting one of these features and am considering if it is worth the effort to create a multiplatform library. Could I write a pure kotlin implementation inside my android project and compile that to JS?
j
Yes, you can add a multiplatform module to your Android project and add an Android (or JVM) as well as JS target. You'd only need the Android target if you need to access Android-specific APIs, otherwise Android can consume a regular JVM target.
m
Copy code
settings.gradle
include ':app'
include ':template'
Copy code
template/build.gradle.kts
plugins {
    //id("com.android.library")
    kotlin("multiplatform")
}


kotlin {
    jvm()
    android()
    js {
        browser()
        nodejs()
    }
}

android {
    compileSdk = 33
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}
Copy code
template/src/main/.../Template.kt

interface Template {
    fun hello() : String = "Hello"
}
Copy code
app/build.gradle
implementation project(':template')
I'm unable to resolve the Template interface in my app module. I would think I still need to include plugin
com.android.library
for my application module to actually import this new module. But this results in some errors around gradle tasks like
generateDebugBuildConfig
The cause of these errors are all
Copy code
> Failed to calculate the value of task ':template:packageDebugResources' property 'namespace'.
      > Failed to calculate the value of property 'namespace'.
j
Template.kt
should be in template/src/_commonMain_. Do you intend to use with a non-Android JVM client?
And do you need to use Android-specific APIs in the template module?
k
Ah ok, I will try moving it into the correct folder No I don't need to use Android specific apis. My goal was to have essentially one class that is going to be shared between my Android project in the app module and a separate JavaScript back end.
So I would be calling this library directly in my Android project but the back end would use the compiled jar file or I will publish to maven
j
If you don't require Android-specific APIs, you can just use the JVM target, as Android can consume JVM libraries. This way you won't require the Android library plugin as well.