java 从Kotlin文件生成本机头文件

kpbwa7wx  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(163)

我试图写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'
ylamdve6

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

package com.demo.lab;

public class Hello {
    static {
         System.loadLibrary("hello");
    }

    public native void SayHello(String strName);
}

生成.h

IntelliJ IDEA Settings-〉Build,Execution,Deployment-〉Compiler-〉Java Compiler-〉Java Options-〉其他命令行参数:

-h $PROJECT_DIR$/src

创建.h

选择Hello.java,功能表Build-〉Recompile Hello.java
选择项目,弹出菜单-〉Reload From Disk
现在您可以发现com_demo_lab_Hello.h位于src目录下

com演示实验室您好. h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_demo_lab_Hello */

    #ifndef _Included_com_demo_lab_Hello
    #define _Included_com_demo_lab_Hello
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     com_demo_lab_Hello
     * Method:    SayHello
     * Signature: (Ljava/lang/String;)V
     */
    JNIEXPORT void JNICALL Java_com_demo_lab_Hello_SayHello
      (JNIEnv *, jobject, jstring);
    
    #ifdef __cplusplus
    }
    #endif
    #endif

然后创建并编译C或C++库

创建Kotlin文件- Hello.kt等同Hello.java

package com.demo.lab

class Hello {
    external fun SayHello(strName: String?)

    companion object {
        init {
            System.loadLibrary("hello")
        }
    }
}

相关问题