我试图写JNI应用程序,并希望在其中使用kotlin。
据我所知,kotlin相当于:
private static native void hi();
是:
private external fun hi()
但问题是我无法在项目中使用gradle生成. h文件:
compileJava{
options.headerOutputDirectory.set(file('src/main/java/idk/bluecross/JMM/natives'))
}
我该怎么做呢?
这是我的建设。gradle顺便说一句:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.8.0-RC2'
id 'application'
id 'cpp'
}
group 'idk.bluecross'
version '1.0-SNAPSHOT'
application.mainClass = 'idk.bluecross.example.Main'
application {
applicationDefaultJvmArgs +=
["-Djava.library.path=" + file("${buildDir}/libs/jMM/shared").absolutePath]
}
compileJava{
options.headerOutputDirectory.set(file('src/main/java/idk/bluecross/JMM/natives'))
}
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
model {
platforms {
x64 {
architecture "x86_64"
}
}
components {
JMM(NativeLibrarySpec) {
sources {
cpp {
source {
srcDir "src/main/java/idk/bluecross/JMM/natives"
}
}
}
targetPlatform "x64"
binaries.all {
def jvmHome = System.properties['java.home']
if (targetPlatform.operatingSystem.macOsX) {
cppCompiler.args '-I', "${jvmHome}/include"
cppCompiler.args '-I', "${jvmHome}/include/darwin"
cppCompiler.args '-mmacosx-version-min=10.4'
linker.args '-mmacosx-version-min=10.4'
} else if (targetPlatform.operatingSystem.linux) {
cppCompiler.args '-I', "${jvmHome}/include"
cppCompiler.args '-I', "${jvmHome}/include/linux"
cppCompiler.args '-D_FILE_OFFSET_BITS=64'
} else if (targetPlatform.operatingSystem.windows) {
cppCompiler.args "-I${jvmHome}/include"
cppCompiler.args "-I${jvmHome}/include/win32"
} else if (targetPlatform.operatingSystem.freeBSD) {
cppCompiler.args '-I', "${jvmHome}/include"
cppCompiler.args '-I', "${jvmHome}/include/freebsd"
}
}
}
}
}
classes.dependsOn 'JMMSharedLibrary'
1条答案
按热度按时间ylamdve61#
方法1 - Gradle配置
查找
build.gradle.kts
https://eritque-arcus.tech/2021/05/14/kotlin-%E7%94%9F%E6%88%90jni%E5%A4%B4%E6%96%87%E4%BB%B6/#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95
方法2 -手动方法(无Maven或Gradle配置,仅使用IDEA)-脏路
创建Java类-Hello.java
生成.h
IntelliJ IDEA
Settings
-〉Build,Execution,Deployment
-〉Compiler
-〉Java Compiler
-〉Java Options
-〉其他命令行参数:创建.h
选择Hello.java,功能表
Build
-〉Recompile Hello.java
选择项目,弹出菜单-〉
Reload From Disk
现在您可以发现
com_demo_lab_Hello.h
位于src
目录下com演示实验室您好. h
然后创建并编译C或C++库
创建Kotlin文件- Hello.kt等同Hello.java