Multiplatform project with native module using: "apply plugin: 'kotlin-platform-native'" possible?


#1

I am trying to create a multiplatform project where one of the platforms is native using:

apply plugin: ‘kotlin-platform-native’

I have made a very simple project but I cannot get it to compile.
Its git repo is here

The problem is it will not let me add the native module as a dependency to the main project’s gradle file. I keep getting this error when I build with gradle

What went wrong:
A problem occurred evaluating root project ‘untitled1’.
Could not find method implementation() for arguments [project ‘:domain:native’] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Basically in my example, in my common module at /domain/common/src/main/kotlin/Date.kt I declare

expect class Date()

build.gradle for common module:

buildscript {

    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.60"
    }
}

apply plugin: 'kotlin-platform-common'



repositories {
     jcenter()
}

dependencies {
     implementation 'org.jetbrains.kotlin:kotlin-stdlib-common:1.2.60'
}

Then in my native module at /domain/native/src/main/kotlin/KtDate.kt I define

actual class Date

build.gradle for native module:

buildscript {
    repositories {
        jcenter()
        maven { url 'https://plugins.gradle.org/m2/' }
        maven { url 'https://dl.bintray.com/jetbrains/kotlin-native-dependencies' }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.9-dev-2922"
    }

}

apply plugin: 'kotlin-platform-native'

 repositories {
    jcenter()
 }

 dependencies {
        expectedBy project(':domain:common')
}

and finally the main native kotlin project’s gradle:

buildscript {
    repositories {
        jcenter()
        maven {
            url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies"
        }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.9-dev-2922"
    }
}

apply plugin: 'konan'

konan.targets = ['macbook']

konanArtifacts {
     program('hello')
}

repositories {
    jcenter()
}

dependencies {
    implementation project(':domain:native')
}

In my example project, the Date class isn’t actually used anywhere… But, my goal is to be able to have the Date class be accessible inside the main kotlin native project. Is this possible to accomplish?

Edit:

When building only the native module with gradle the following error is thrown:

* What went wrong:
A problem occurred configuring project ':domain:common'.
> Failed to notify project evaluation listener.
> java.lang.AbstractMethodError (no error message)
> java.lang.AbstractMethodError (no error message)

* Exception is:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':domain:common'.
at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:94)
at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:89)
....
Caused by: org.gradle.internal.event.ListenerNotificationException: Failed to notify project evaluation listener.
at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:86)
at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:324)
....
Caused by: java.lang.AbstractMethodError
at org.jetbrains.kotlin.gradle.plugin.KotlinPluginKt.resolveSubpluginArtifacts(KotlinPlugin.kt:776)
at org.jetbrains.kotlin.gradle.plugin.KotlinPluginKt.loadSubplugins(KotlinPlugin.kt:750)
at org.jetbrains.kotlin.gradle.plugin.KotlinPluginKt.access$loadSubplugins(KotlinPlugin.kt:1)
at org.jetbrains.kotlin.gradle.plugin.KotlinCommonSourceSetProcessor$doTargetSpecificProcessing$1.execute(KotlinPlugin.kt:329)
at org.jetbrains.kotlin.gradle.plugin.KotlinCommonSourceSetProcessor$doTargetSpecificProcessing$1.execute(KotlinPlugin.kt:311)
at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:91)

#2

I have figured it out.

For some reason removing the buildscript from my common, and native modules’ build.gradle fixed the first error.

Then to work around the second error in my native module I changed from applying ‘kotlin-platorm-native’ to ‘konan’ then declared a library:

apply plugin: 'konan'

konanArtifacts {
    library('native') {
        enableMultiplatform true
    }
}


dependencies {
        expectedBy project(':domain:common')
}

Then in my main build.gradle I removed

implementation project(path: ':domain:native')

and imported the library

konanArtifacts {
     program('hello') {
             libraries {
                 allLibrariesFrom project(':domain:native')
             }
     }
}

This allowed me to import and use my native Date class in the example hello world program.

CLion does not auto import my Date class, or auto-suggest it though :confused:

My project on git has been updated. Hope this helps anyone else trying to create a native module for a common library.