1

I'm experimenting with Kotlin native and iOS. I tried to use the example at raywenderlich as a starting point. This example is a bit old , so I have updated the code to fit the Kotlin multiplatform 1.3.61. I'm using AppCode to build the code.

I'm struggling with the Kotlin DSL gradle file ( build.gradle.kts) , the example is using build.gradle :

plugins {
    id "org.jetbrains.kotlin.platform.native" version "1.3.0"
}

components.main {

    def productsDir = new File("").absolutePath

    targets = ['ios_arm64', 'ios_x64']
    outputKinds = [EXECUTABLE]

    allTargets {
        linkerOpts '-rpath', '@executable_path/Frameworks'
        linkerOpts "-F${productsDir}"
    }

    dependencies {
        cinterop('AFNetworking'){
            packageName 'com.afnetworking'
            compilerOpts "-F${productsDir}"
            linkerOpts "-F${productsDir}"
            includeDirs{
                allHeaders "${productsDir}/AFNetworking.framework/Headers"
            }
        }
    }
}

task copyExecutable() {
    doLast {
        def srcFile = tasks['compileDebugIos_x64KotlinNative'].outputFile
        def targetDir = getProperty("konan.configuration.build.dir")
        copy {
            from srcFile.parent
            into targetDir
        }
    }
}                                                                                                            

I have tried to translate this into the build.gradle.kts file of my own :

plugins {                                                                                                          
    kotlin("multiplatform") version "1.3.61"                                                                       
    kotlin("xcode-compat") version "0.2.5"
    }
repositories {                                                                                                     
    mavenCentral()                                                                                                 
}                                                                                                                  

kotlin {                                                                                                           
    val productsDir = "/Users/trond/Desktop/native_meteor/native_meteor/"                                          
    iosX64("ios")                                                                                                  
    {                                                                                                              
        compilations.getByName("main")                                                                             
        {                                                                                                          
            val myInterop by cinterops.creating {                                                                  
                defFile(project.file("src/nativeInterop/cinterop/afnetworking.def"))                               
                packageName ("com.afnetworking")                                                                   
                compilerOpts ("-F${productsDir}")                                                                  
             //   linkerOpts ("-F${productsDir}")                                                                  
                // 3                                                                                               
                includeDirs{                                                                                       
                    allHeaders ("${productsDir}/AFNetworking.framework/Headers")                                   
                }                                                                                                  
            }                                                                                                      
        }                                                                                                          

    }                                                                                                              
    xcode {                                                                                                        
        setupApplication("native_meteor")                                                                          
    }                                                                                                              
}

From what I can see the cinterops tool is doing it's job and is creating a klib file and a afnetworing.kt ( build/classes/kotlin/ios/main/..... )

But the I'm not able to use the library AFNetworking! I try to add the import directive in the ViewController.kt file :

import com.afnetworking.*

But this is not recognized. This results in that the project is not building :

> Task :native_meteor:compileKotlinNative_meteor FAILED
e: /Users/trond/Desktop/native_meteor/native_meteor/src/native_meteorMain/kotlin/ViewController.kt: (15, 8): Unresolved reference: com
e: /Users/trond/Desktop/native_meteor/native_meteor/src/native_meteorMain/kotlin/ViewController.kt: (37, 23): Unresolved reference: AFHTTPSessionManager
e: /Users/trond/Desktop/native_meteor/native_meteor/src/native_meteorMain/kotlin/ViewController.kt: (40, 38): Unresolved reference: AFJSONResponseSerializer                                                                                                                  

Anyone that can shed some light on this ?

Trond Tunheim
  • 115
  • 10

2 Answers2

2

Maybe it will make sense to check the contents of your result .klib to make sure you use the correct package name. It can be done using ~/.konan/kotlin-native-macos-1.3.61/bin/klib CLI tool.
Also, I would recommend you to take a look at this sample from the Kotlin/Native Github, it also utilizes AFNetworking and uses the latest compiler version.

Artyom Degtyarev
  • 2,704
  • 8
  • 23
2

Thank you for pointing me in that direction. I investigated the .klib file, and I discovered that it contained the package that I expected. I then took a look at the Gradle sourcsets that I had in the Gradle pane in Appcode. Here I discovered that the .klib library was defined under "iosMain" and not "native_metorMain". I then found out that the line iosX64("ios") should be iosX64("meteor_main").

After that I got an error : "ld: framework not found AFNetworking" when I tried to compile in XCode. I then added -F{full path to framework} for the linkerOpts ( linkerOpts= -F{full path to framwork} -framework AFNetworking ) in the afnetworking.def file.

Now the project builds successfully.

Trond Tunheim
  • 115
  • 10