Reputation: 97
I'm trying to build a shared library using NDK. My folder structure has two folders, one written in C++ (the core
) and one written in Java, called project
, which is an Android Studio project. The C++ library is compiled fine and the .a
file is generated, but then it's not linking with the shared library. Here's my build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'net.sf.proguard:proguard-gradle:5.2.1'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 16
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk {
moduleName "core"
cFlags "-std=c++11 -fexceptions -I../core/includes"
stl "stlport_shared"
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
jniLibs.srcDir 'libs'
jni.srcDirs = ['src/main/jni', 'src/main/jni/', 'jni/']
}
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkDir = plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()
commandLine "$ndkDir/ndk-build",
'-C', file('jni').absolutePath,
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()
commandLine "$ndkDir/ndk-build",
'-C', file('jni').absolutePath,
'clean'
}
task cleanBinaryFolders(type: Delete, description: 'Clean binary folders') {
delete 'libs', 'obj'
}
clean.dependsOn 'cleanNative'
clean.dependsOn 'cleanBinaryFolders'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
dependencies {
compile 'com.android.support:support-v4:20.0.0'
}
Here's my Android.mk:
SHELL = /bin/bash
MY_HOMEDIR = $(realpath $(shell pwd)/../..)
MY_COREDIR = $(MY_HOMEDIR)/core
MY_ANDROIDDIR = $(MY_HOMEDIR)/project
MY_CORESOURCES = $(shell find $(MY_COREDIR)/src -type f -name "*.cpp")
MY_BRIDGESOURCES = $(shell find $(MY_ANDROIDDIR)/jni -type f -name "*.cpp")
LOCAL_PATH = $(MY_HOMEDIR)
# Generate a static library from the core implementation
include $(CLEAR_VARS)
LOCAL_MODULE = core
LOCAL_SRC_FILES = $(MY_CORESOURCES)
TARGET_PLATFORM = android-16
TARGET_ARCH_ABI = all
LOCAL_C_INCLUDES = $(MY_COREDIR)/includes
#LOCAL_LDLIBS = -llog
LOCAL_CFLAGS = -llog -I$(MY_COREDIR)/includes
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE = project
LOCAL_SRC_FILES = $(MY_BRIDGESOURCES)
TARGET_PLATFORM = android-16
TARGET_ARCH_ABI = all
LOCAL_STATIC_LIBRARIES = core
LOCAL_C_INCLUDES = $(MY_COREDIR)/includes
LOCAL_LDLIBS = -lcore # I’m not sure about this
LOCAL_CFLAGS = -llog -I$(MY_COREDIR)/includes
include $(BUILD_SHARED_LIBRARY)
When compiling like this I get a bunch of undefined reference
errors, even though the methods are implemented by the core module. What am I missing?
Upvotes: 3
Views: 1216
Reputation: 16842
https://stackoverflow.com/a/22401410/755804 reads:
Only use LOCAL_LDLIBS for system library dependencies. If you want to point to another library, it's much better to list them in either LOCAL_STATIC_LIBRARIES and LOCAL_SHARED_LIBRARIES (even if this means defining a PREBUILT_XXX module for them), because this lets the build system work out dependencies and ordering automatically for you.
Upvotes: 1