iosgradlekotlinkotlin-nativecinterop

Kotlin/native cinterop build problem with gradle


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 ?


Solution

  • 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.