Does anyone know how to use a .klib like a depende...
# kotlin-native
j
Does anyone know how to use a .klib like a dependency? If I wanted to use 1.klib and 2.klib in a project3 and spit out an iOS framework that contains all of the code from 1, 2, &3 is that possible?
d
Yes this is possible.
The quickest way would be to execute
publishToMavenLocal
.
Then consume the library via
mavenLocal()
repo in
project3
.
j
If I understand properly, this
.klib
would only be available for the
ios
project in project3. For the other’s I’d still need to use the
.jar
files right? I haven’t seen a
common
dependency that generates.
d
You should be able to reference the klib for any platform (it would fail on the wrong one).
Have you enabled common source sets?
j
Yes. Do you want to see my build gradle?
d
Yes please.
j
Copy code
buildscript {
    ext.ios_framework_name = 'KSynapseModels'
    ext.serialization_version = "0.11.0"
    ext.kotlin_version = '1.3.31'
    ext.kotlinx_version = '1.3.30'

}

plugins {
    id 'kotlin-multiplatform' version '1.3.31'
    id 'kotlinx-serialization' version '1.3.30'
}

repositories {
    mavenCentral()
    jcenter()
    maven { url "<https://kotlin.bintray.com/kotlinx>" }
}

group 'com.jacob.rakidzich'
version '0.0.1'

apply plugin: 'maven-publish'

kotlin {
//    jvm()

    targets {
        fromPreset(presets.jvm, 'jvm')

        fromPreset(presets.iosX64, 'ios') {
            binaries {
                framework("$ios_framework_name") {
                    embedBitcode('disable')
                }
            }
        }
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"

            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation kotlin('stdlib')
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"

            }
        }
        jvmTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        iosMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
            }

        }
        iosTest {
        }
    }
}


task packForXCode {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String buildType = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'

    def keyFrameworkPrefix = "$ios_framework_name${buildType.toLowerCase().capitalize()}"
    dependsOn "link${keyFrameworkPrefix}FrameworkIos"

    doLast {
        def srcFile = kotlin.targets.ios.binaries.getFramework("$ios_framework_name", buildType).outputFile

        copy {
            from srcFile.parent
            into frameworkDir
        }

        new File(frameworkDir, 'gradlew').with {
            text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
            setExecutable(true)
        }
    }
}


tasks.build.dependsOn packForXCode


configurations {
    compileClasspath
}
d
Have you also enabled metadata?
j
Yes.
Copy code
pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "kotlin-multiplatform") {
                useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
            }
            if (requested.id.id == "kotlinx-serialization") {
                useModule("org.jetbrains.kotlin:kotlin-serialization:${requested.version}")
            }
        }
    }
}

rootProject.name = 'KSynapseModels'


enableFeaturePreview('GRADLE_METADATA')
That’s the settings.gradle
d
What does the
.m2
folder in your home directory look like? Does it have the jars and libs?
j
message has been deleted
There is a .klib in iOS and all the standard .jars in jvm
Not sure if this is relevant or not. I’m using a Kotlin Native Shared Library project in IDEA CE not a Multiplatform project.
d
Ohhhhhhhh.
That's deprecated now.
j
BLAST!
d
Although it shouldn't be a problem yet.
It was deprecated yesterday aha.
How are you referencing this in
project3
?
j
LOL I’m a day behind
I’m not currently. I’m trying to wrap my head around how to get the assets in the first place. Gradle is not my strong suit
d
Oh, the assets are in the build directory as you have found.
j
so module is the common code I can import?
I thought I needed a .klib or a .jar
d
Yup!
Actually it's kinda like a jar.
j
HA! All this time there’s been no problem but my understanding.
d
It's some weird gradle metadata stuff, I'm not too familiar with the details.
j
Well thank you for walking through my confusion and helping me out.
d
No problem!
j
Hey Dominic. I’ve since tried to consume the common code in another project and I’m getting a
Could not find
in my gradle build output. I’m using maven local
implementation "/Users/xxxx/.m2/repository/com/jacob/rakidzich/KSynapseModels:$k_synapse_models_version"
I’ve also tried to include the url in the required repositories
maven { url "/Users/xxxx/.m2/repository/com/jacob/rakidzich" }
I’ve been searching online but I’m not seeing what I’m looking for. Do you have any insight?
d
Yup
You need to add
mavenLocal()
to your repositories block.
j
I have that already.
👍🏼 1
Unless I need to have that outside of the build script too
d
Then use
implementation "com.jacob.rakidzich:KSynapseModels:0.0.1"
.
j
Thanks! I was accidentally putting a
.
instead of a
:
before
KSynapseModels
🤦
d
Did that work?
j
Yes.
d
Sweet!
120 Views