c - JNI 与 gradle

标签 c gradle java-native-interface

如何使用gradle构建共享库?

我的项目树

core/c/{*.c, *.h}

core/c/include/{jni, lib}

我的build.gradle

apply plugin: 'c'

def JNI_INCLUDE_DIR = this.properties['jni.include.dir']

model {
        components {
                bridge(NativeLibrarySpec) {
                        sources.c.source {
                                srcDir 'core/c'
                                include '**/*.c'
                        }
                        sources.c.exportedHeaders {
                                srcDir 'core/c/include'
                        }
                        buildTypes {
                                debug
                                release
                        }
                }
        }
toolChains {
                gcc(Gcc) {
                        if(System.properties['os.name'].equals("Mac OS X")) {
                                cCompiler.withArguments {
                                        args << "-I" + JNI_INCLUDE_DIR
                                        args << "-I" + JNI_INCLUDE_DIR + "/darwin"
                                        args << "-std=gnu11"
                                        args << "-g"
                                }
                        } else {
                                cCompiler.withArguments {
                                        args << "-I" + JNI_INCLUDE_DIR
                                        args << "-I" + JNI_INCLUDE_DIR + "/linux"
                                        args << "-std=gnu11"
                                        args << "-g"
                                }
                        }
                }
        }
}

错误

Exception thrown while executing model rule: toolChains { ... } @ build.gradle line 23, column 2 Could not get unknown property 'cCompiler' for Tool chain 'gcc' (GNU GCC) of type org.gradle.nativeplatform.toolchain.internal.gcc.GccToolChain.

最佳答案

The Gcc class没有属性 cCompiler 这就是您收到错误的原因。 cCompilerGccPlatformToolchain 的一个属性。 Gcc 有一个或多个平台工具链实例。如果您想将标记应用于 Gcc 工具链中的每个平台,您可以使用 eachPlatform,例如:

toolChains {
    gcc(Gcc) {
        eachPlatform {
            if(System.properties['os.name'].equals("Mac OS X")) {
                cCompiler.withArguments {
                    args << "-I" + JNI_INCLUDE_DIR
                    args << "-I" + JNI_INCLUDE_DIR + "/darwin"
                    args << "-std=gnu11"
                    args << "-g"
                }
            } else {
                cCompiler.withArguments {
                    args << "-I" + JNI_INCLUDE_DIR
                    args << "-I" + JNI_INCLUDE_DIR + "/linux"
                    args << "-std=gnu11"
                    args << "-g"
                }
            }
        }
    }
}

我相信您还可以为给定的操作系统/体系结构定义自定义平台,然后引用 Gcc block 内的平台,而不是使用 if 语句,这可能更干净、更惯用。

关于c - JNI 与 gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38423241/

相关文章:

java - 用Java重写C代码还是使用JNI?

c - 使用 union/结构在纯 C 中创建广义列表对象

Android Gradle 同步 "Tag Mismatch!"错误

apache-spark - 如何将自定义库部署到 Apache Spark?

android - APK:与版本号相关的 native 库名称问题 (SO.X.Y)

android - 无法解决:com.android.support:appcompat-v7:23.0.3

c - C中的左旋和右旋

c - 按顺序在处理器之间传递消息

c - C中的评估&&运算符

gradle - ReactNative - Spotify SDK 依赖冲突