Reputation: 2886
I've got a compilation of libFLAC for android that I've made. It's in .so form.
When i run "ndk-build" from my jni folder i get the following:
[armeabi] Install : libFLAC.so => libs/armeabi/libFLAC.so
[armeabi] Compile thumb : LibFlacWrapper <= LibFlacWrapper-jni.c
[armeabi] SharedLibrary : libLibFlacWrapper.so
[armeabi] Install : libLibFlacWrapper.so => libs/armeabi/libLibFlacWrapper.so
When I then cd back to my app directory and run gradle build
I get the following error:
Error Code:
2
Output:
/Users/nathanielwaggoner/Client-Android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/objs/LibFlacWrapper//Users/nathanielwaggoner/Client-Android/app/src/main/jni/LibFlacWrapper-jni.o: In function `Java_clientandroid_LibFlac_parseChunk':
/Users/nathanielwaggoner/Client-Android/app/src/main/jni/LibFlacWrapper-jni.c:25: undefined reference to `FLAC__stream_encoder_new'
collect2: error: ld returned 1 exit status
make: *** [/Users/nathanielwaggoner/Client-Android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/libLibFlacWrapper.so] Error 1
i'm not sure how to handle this part of things. It seems like the build works fine and I get an SO out of it using the ndk build, but if I want to run through gradle that fails.
Android.mk:
LOCAL_PATH := $(call my-dir)
# prepare libX
include $(CLEAR_VARS)
LOCAL_MODULE := FLAC
LOCAL_SRC_FILES := armeabi/libFLAC.so
LOCAL_EXPORT_C_INCLUDES := /Users/nathanielwaggoner/programming_utils/flac-1.3.1/src/libFLAC/include
include $(PREBUILT_SHARED_LIBRARY)
# build JNI
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libFLAC
LOCAL_MODULE := LibFlacWrapper
LOCAL_SRC_FILES := LibFlacWrapper-jni.c
include $(BUILD_SHARED_LIBRARY)
Build.gradle:
defaultConfig {
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "libLibFlacWrapper"
}
}
if I remove the
# prepare libX
include $(CLEAR_VARS)
LOCAL_MODULE := FLAC
LOCAL_SRC_FILES := armeabi/libFLAC.so
LOCAL_EXPORT_C_INCLUDES := /Users/nathanielwaggoner/programming_utils/flac-1.3.1/src/libFLAC/include
include $(PREBUILT_SHARED_LIBRARY)
from the Android.mk and then run ndk-build
i get this error, which is very similar to the gradle error, which makes me think gradle isn't leveraging my Android.mk....
[armeabi] Compile thumb : LibFlacWrapper <= LibFlacWrapper-jni.c
[armeabi] SharedLibrary : libLibFlacWrapper.so
/Users/nathanielwaggoner/Client-Android/app/src/main/jni/LibFlacWrapper-jni.c:25: error: undefined reference to 'FLAC__stream_encoder_new'
collect2: error: ld returned 1 exit status
make: *** [/Users/nathanielwaggoner/Client-Android/app/src/main/obj/local/armeabi/libLibFlacWrapper.so] Error 1
Upvotes: 0
Views: 945
Reputation: 14463
Indeed, gradle isn't using your Android.mk. That's the current behavior of AS and android plugin 1.0.
They generate a Makefile on-the-fly that includes your sources from jni folder and your configuration from buld.gradle. As of now, you can't configure gradle so it takes your prebuilts into consideration.
What you can do is to deactivate this ndk-build integration to use ndk-build from outside Android Studio, or make it called automatically from your build script:
import org.apache.tools.ant.taskdefs.condition.Os
android {
...
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
For more background on this, you can refer to my article on using the NDK with Android Studio
Upvotes: 2