# 在 Gradle 项目中设置 JNI 开发

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/idea/llms.txt).

IntelliJ IDEA 支持 [在 Gradle 中进行 JNI 开发](https://docs.gradle.org/current/userguide/native_software.html) 项目。

本教程使用 Java 1.8 和 Gradle 7.4 版本。 教程使用了 [软件模型](https://blog.gradle.org/state-and-future-of-the-gradle-software-model)。

如果您想使用 [replacement plugins](https://docs.gradle.org/current/userguide/building_cpp_projects.html#cpp_plugin)，您可以查看 [JNI Sample](https://github.com/vladsoroka/GradleJniSample)项目。

> **Note:**
> 如果您在项目中使用 Kotlin，请查看 [Kotlin 教程](https://kotlinlang.org/docs/get-started-with-jvm-gradle-project.html)。

Procedure: 添加 JNI 支持

1. 创建 [新的](gradle.html#project_create_gradle)或打开 [现有的](gradle.html#gradle_import) Gradle 项目。

2. 打开  `build.gradle`  文件。

3. 请将以下代码添加到  `build.gradle` ：

在 Windows 上运行:

```GROOVY
import org.gradle.internal.jvm.Jvm

plugins {
    id 'java'
    id 'application'
    id 'c'
}

mainClassName = 'HelloWorld'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.12'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

application {
    applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath]
}

model {
    platforms {
        x64 {
            architecture "x86_64"
        }
    }

    components {
        hello(NativeLibrarySpec) {
            targetPlatform "x64"
            binaries.all {
                def jvmHome = Jvm.current().javaHome
                if (targetPlatform.operatingSystem.macOsX) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/darwin"
                    cCompiler.args '-mmacosx-version-min=10.4'
                    linker.args '-mmacosx-version-min=10.4'
                } else if (targetPlatform.operatingSystem.linux) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/linux"
                    cCompiler.args '-D_FILE_OFFSET_BITS=64'
                } else if (targetPlatform.operatingSystem.windows) {
                    cCompiler.args "-I${jvmHome}/include"
                    cCompiler.args "-I${jvmHome}/include/win32"
                } else if (targetPlatform.operatingSystem.freeBSD) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/freebsd"
                }
            }
        }
    }
}

classes.dependsOn 'helloSharedLibrary'

```

在 macOS 上运行:

```GROOVY
import org.gradle.internal.jvm.Jvm

plugins {
    id 'java'
    id 'application'
    id 'c'
}

mainClassName = 'HelloWorld'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.12'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

application {
    applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath]
}

model {
    platforms {
        x64 {
            architecture "x86_64"
        }
    }

    components {
        hello(NativeLibrarySpec) {
            targetPlatform "x64"
            binaries.all {
                def jvmHome = Jvm.current().javaHome
                if (targetPlatform.operatingSystem.macOsX) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/darwin"
                    cCompiler.args '-mmacosx-version-min=10.9'
                    linker.args '-mmacosx-version-min=10.9'
                    linker.args '-stdlib=libc++'
                } else if (targetPlatform.operatingSystem.linux) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/linux"
                    cCompiler.args '-D_FILE_OFFSET_BITS=64'
                } else if (targetPlatform.operatingSystem.windows) {
                    cCompiler.args "-I${jvmHome}/include"
                    cCompiler.args "-I${jvmHome}/include/win32"
                } else if (targetPlatform.operatingSystem.freeBSD) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/freebsd"
                }
            }
        }
    }
}

classes.dependsOn 'helloSharedLibrary'

```

为了使您的设置顺利进行，请注意以下细节：

* IntelliJ IDEA 使用位于 [Gradle 设置](gradle-settings.html#gradle_jvm)中的 Gradle JVM 版本。

* 根据您的操作系统，编译器参数可能会有所不同，注意在更改 `cCompiler.args` 和 `linker.args` 时的 `build.gradle` 。

* 当应用程序在具有以下现有系统属性的情况下运行时： ```GROOVY application { applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath] } ``` 此应用程序依赖 `classes.dependsOn 'helloSharedLibrary'` ，IntelliJ IDEA 为 项目 工具窗口中的共享库生成二进制 `libhello.dylib` 文件。 ![共享库的二进制文件](https://resources.jetbrains.com/help/img/idea/2026.3/binary_for_shared_library.png) > **Note:** > 请注意， `libhello.dylib` 取决于您使用的操作系统。 例如，对于 Windows，该二进制文件的名称将是 `hello.dll` 。

4. 在   项目  工具窗口中，选择  `src | java` 目录。

5. 右键点击  `java`  目录并选择  `新建 | Java 类`  以创建一个将使用  `C`  代码的 Java 类  `HelloWorld` 。

6. 在编辑器中打开创建的  `HelloWorld` 类，然后输入以下代码：

```JAVA
class HelloWorld {
    public native void print();

    static {
        System.loadLibrary("hello");
    }

    public static void main(String[] args) {
        new HelloWorld().print();
    }
}

```

文件中指定的代码将加载生成的 [system library](#library_binary)。

7. 在   项目   工具窗口，在  `src`  目录中，创建  `hello/c/hello.c`  文件，这是用于您的  `C`  程序的文件。

8. 在编辑器中打开  `hello.c`  文件，并指定以下代码：

```CSHARP
#include <jni.h>
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello From C++ World!\n");
return;
}

```

此时，您可以根据需要开始使用原生代码进一步开发您的应用程序。

Procedure: 运行您的应用程序

1. 打开您的运行配置对话框。

2. 如果您使用 macOS 作为您的操作系统，请将以下代码添加到   VM 选项   字段。

![运行配置](https://resources.jetbrains.com/help/img/idea/2026.3/run_config_vm_options.png)

可以从   修改选项  列表中添加 VM options 字段。

请点击   OK   以保存更改。

3. 在主工具栏上点击 ![运行按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.run.svg)。

