I am experimenting with kotlin multiplatform in this chess app. I have c++ code for the chess engines in the app. For each engine, I want to create a kmm module. Since kotlin native does not yet interop with c++, I have to create wrappers around the c++ code. In each androidMain, I have the JNI class that interface with c++, and this works. ios is where I am not clear. I believe I have to create an obj-c++ wrapper (.mm file) around the c++ code, then do cinterop for the wrapper.
iosTarget("ios") {
compilations.getByName("main") {
val jwtc by cinterops.creating {
defFile(project.file("src/iosMain/cpp/jwtc.def"))
packageName("com.cinterop.jwtc")
}
}
}
Is this right? When I try to build it, gradle sync fails with this error:
Execution failed for task ':jwtc:cinteropJwtcIos'.
> Process 'command '/Applications/Android Studio Preview.app/Contents/jre/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':jwtc:cinteropJwtcIos'. <36 internal calls>
Caused by: org.gradle.process.internal.ExecException: Process 'command '/Applications/Android Studio Preview.app/Contents/jre/Contents/Home/bin/java'' finished with non-zero exit value 1
at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:414)
at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:52)
at org.gradle.process.internal.DefaultExecActionFactory.javaexec(DefaultExecActionFactory.java:198)
at org.gradle.api.internal.project.DefaultProject.javaexec(DefaultProject.java:1145)
at org.jetbrains.kotlin.compilerRunner.KotlinToolRunner.runViaExec(KotlinToolRunner.kt:98)
at org.jetbrains.kotlin.compilerRunner.KotlinToolRunner.run(KotlinToolRunner.kt:73)
at org.jetbrains.kotlin.gradle.tasks.CInteropProcess.processInterop(KotlinNativeTasks.kt:1035) <122 internal calls>
I have cleaned and restarted android studio severally, and also tried java 8 instead of 11.
I would also appreciate links to more info about the def file. There is not much about it in the kotlin docs.
My jwtc.def:
headers = GameWrapper.h
headerFilter = GameWrapper.h
GameWrapper.h:
#interface GameWrapper
- (void) getBestMove;
#end
GameWrapper.mm:
#include "GameWrapper.h"
#include "Game.h"
#implementation GameWrapper
- (void) getBestMove {
Game game;
game.getBestMove();
}
#end
I finally found that the real error message:
Exception in thread "main" java.lang.Error: /var/folders/17/14lmpq652zn5h_cy_h51z7yr0000gr/T/8492320185156128475.c:1:10: fatal error: 'GameWrapper.h' file not found
and fixed it by adding compilerOpts to point to the location of my src files:
iosTarget("ios") {
compilations.getByName("main") {
val jwtc by cinterops.creating {
defFile(project.file("src/iosMain/cpp/jwtc.def"))
packageName("com.cinterop.jwtc")
compilerOpts ("-Isrc/iosMain/cpp/")
}
}
}
Related
I've just created a new KMM project through out KMM Plugin, but I can't run or even debug in Xcode iosApp part of the project. When I try to run iosApp from Android Studio, the build process fails (Command PhaseScriptExecution failed with nonzero exit code)
The final lines of building was:
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':shared:compileKotlinIosX64'.
Compilation finished with errors
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 8s
1 actionable task: 1 executed
Command PhaseScriptExecution failed with a nonzero exit code
** BUILD FAILED **
The following build commands failed:
PhaseScriptExecution Run\ Script /Users/tamegajr/AndroidStudioProjects/TesteKMM5/build/ios/iosApp.build/Release-iphonesimulator/iosApp.build/Script-7555FFB5242A651A00829871.sh
(1 failure)
Can anyone help to solve this problem?
I had the same issue but this solution helped me:
From KMM pluging you will obtain (on dependencies):
Change this:
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
To this:
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
What i found to be the solution was to uncomment the "iosSimulatorArm64()" in the "build.gradle.kts(:shared)".
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64() //sure all ios dependencies support this target
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
podfile = project.file("../iosLink/Podfile")
framework {
baseName = "shared"
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
//val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
//iosSimulatorArm64Test.dependsOn(this)
}
}
}
After some code review from 2-3 months old KMM examples project and comparing them with new ones, I found out a solution for this build failure when trying to run iosApp on Ios Simulators, just apply this change to build.gradle.kts on root project:
From KMM pluging you will obtain (on dependencies):
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
Change it to:
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
And that's it, problem solve. I hope someone on Jetbrains can solve this problem in future updates of KMM plugin.
08/30/2020:
It seems that Jetbrains has correct some issues and now you can build and run a KMM project with version 1.7.10, the last stable version at this time.
By the way, if you have any trouble is worthy to check this stack overflow post about JDK version used by Android Studio : How to set or change the default Java (JDK) version on macOS?
Multiplatform error when building iosApp: Command PhaseScriptExecution failed with a nonzero exit code
I have desktop app written with compose, I am workinkg on Mac. Everything works fine for mac builds, but I am unable to produce one for linux.
dependencies {
implementation(compose.desktop.linux_x64)
implementation(compose.desktop.macos_x64)
[...]
}
compose.desktop {
application {
mainClass = "pl.rtsm.myapp.ApplicationKt"
jvmArgs += listOf("-Xmx12G")
nativeDistributions {
targetFormats(TargetFormat.Deb, TargetFormat.Dmg)
outputBaseDir.set(project.buildDir.resolve("installers"))
packageName = "MyApp"
}
}
}
Whatever I specify in targetFormats it only produces Mac app. Only thing in debug logs I've found is that this task is skipped (even though I am running task from clean state):
2022-01-09T18:11:57.948+0100 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :packageDeb SKIPPED
2022-01-09T18:11:57.948+0100 [INFO] [org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter] Skipping task ':packageDeb' as task onlyIf is false.
Is it possible to create linux builds on Mac? Something is missing?
I have a gradle project which uses the Kotlin DSL
build.gradle.kts
plugins {
kotlin("jvm") version "1.4.21"
id("com.google.protobuf") version "0.8.10"
}
group = "schema"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.0.0"
}
}
schema.proto
syntax = "proto3";
package schema;
message Message {
string content = 1;
string date_time = 2;
}
and project structure
schema
-src/main/proto/schema.proto
-build.gradle.kts
Whenever I run gradle build I get error:
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/x/schema/build.gradle.kts' line: 13
* What went wrong:
Script compilation errors:
Line 15: protoc {
^ Unresolved reference: protoc
Line 16: artifact = "com.google.protobuf:protoc:3.0.0"
^ Unresolved reference: artifact
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
The only thing I am doing different to various tutorials is using the Gradle Kotlin DSL. What am I doing wrong?
I think this is happening because you are referencing a single task within the protobuf gradle plugin. Try importing the whole bundle to make use of type-safe accessors as you desired.
import com.google.protobuf.gradle.*
For whatever reason this works
import com.google.protobuf.gradle.protoc
protobuf {
protobuf.protoc {
artifact = "com.google.protobuf:protoc:3.0.0"
}
}
It seems a little ugly having to specify protobuf twice mind
I tried including secext.h winapi header in Kotlin/Native cinterop using Gradle:
build.gradle:
// ...
kotlin {
mingwX64("windows") {
compilations.main {
cinterops {
secext {
packageName 'me.[...].cinterop.secext'
includeDirs.allHeaders("src/nativeInterop/cinterop")
}
}
}
// ...
}
// ...
}
// ...
src/nativeInterop/cinterop/secext.def:
headers = secext_wrapper.h
headerFilter = secext.h
package = me.[...].cinterop.secext
src/nativeInterop/cinterop/secext_wrapper.h:
#pragma once
#include <windows.h>
#define SECURITY_WIN32
#include <security.h>
And IDE I use (IntelliJ Idea with the Kotlin plugin) does see the functions from secext.h and it does compile if I don't use any of them. When I try to use GetUserNameExA, though, this errors show up in :linkDebugSharedWindc:
> Task :linkDebugSharedWindows
Produced library API in gpio4k_api.h
e: C:\Users\Chilli\.konan\dependencies\msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1/bin/clang++ invocation reported errors
The C:\Users\Chilli\.konan\dependencies\msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1/bin/clang++ command returned non-zero exit code: 1.
output:
C:\Users\Chilli\.konan\dependencies\msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1\bin\ld: C:\Users\Chilli\.konan\dependencies\msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1\bin\ld: DWARF error: mangled line number section (bad file number)
[many identical lines here]
C:\Users\Chilli\.konan\dependencies\msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1\bin\ld: DWARF error: mangled line number section (bad file number)
C:\Users\Chilli\AppData\Local\Temp\konan_temp8580678556601557020\result.o:out:(.text+0x7e4d1): undefined reference to `GetUserNameExA'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
> Task :linkDebugSharedWindows FAILED
Execution failed for task ':linkDebugSharedWindows'.
> Compilation finished with errors
And I have literally no idea how to fix it.
I try to run "gradle test", and get error
My test is
class HelperTest extends ro.gd.Test {
Plugin o;
void setUp() {
o = new Plugin();
}
void testGetIdeaDeps() {
def r = o.ideaDeps
asrHaveVal r
}
}
when i run gradle test, it raise:
junit.framework.AssertionFailedError: Exception in constructor: testGetIdeaDeps (java.lang.NoClassDefFoundError: Could not initialize class groovy.lang.GroovySystem
at org.codehaus.groovy.reflection.ClassInfo.isValidWeakMetaClass(ClassInfo.java:221)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClassForClass(ClassInfo.java:191)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:236)
at ro.gd.idea.HelperTest.$getStaticMetaClass(HelperTest.groovy)
at ro.Test.<init>(Test.groovy)
at ro.gd.Test.<init>(Test.groovy)
at ro.gd.idea.HelperTest.<init>(HelperTest.groovy)
...
Here is my full code
I fix this question. the reason is "groovy.lang.GroovyRuntimeException: Conflicting module versions", for detail, following is my build.gradle
compile 'org.codehaus.groovy:groovy-all:+'
compile gradleApi()
I guess gradleApi() will auto "compile localGroovy()", and this groovy version is 2.3.6, but latest version is 2.4.3
I find this error message in one test report
the solution is to specify groovy version like following
compile 'org.codehaus.groovy:groovy-all:2.3.6'
and you check your groovy version with 'gradle dependencies|grep groovy'