使用gradle将libFLAC编译成android项目

时间:2014-12-19 22:31:42

标签: android android-ndk android-gradle

我已经为我制作了一个关于android的libFLAC汇编。它是.so形式。

当我从我的jni文件夹运行“ndk-build”时,我得到以下内容:

[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

当我然后回到我的app目录并运行gradle build时,我收到以下错误:

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

我不知道如何处理这部分事情。似乎构建工作正常,我使用ndk构建得到了SO,但如果我想通过gradle运行失败。

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"
        }

    }

如果我删除

# 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)
从Android.mk

然后运行ndk-build我收到此错误,这与gradle错误非常相似,这让我觉得gradle没有利用我的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

1 个答案:

答案 0 :(得分:2)

确实,gradle没有使用你的Android.mk。这是AS和Android插件1.0的当前行为。

他们即时生成一个Makefile,其中包含来自jni文件夹的源代码和来自buld.gradle的配置。截至目前,您无法配置gradle,因此需要考虑您的预制。

您可以做的是停用此ndk-build集成以从Android Studio外部使用ndk-build,或者从构建脚本自动调用它:

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
    }
}

有关此问题的更多背景信息,请参阅我在using the NDK with Android Studio

上的文章