如何使用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
这就是您收到错误的原因。 cCompiler
是 GccPlatformToolchain
的一个属性。 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/