构建与静态库依赖关系共享

时间:2015-10-26 19:15:40

标签: android-ndk shared-libraries static-linking dynamic-linking

我正在尝试使用NDK构建共享库。我的文件夹结构有两个文件夹,一个用C ++编写(core),另一个用Java编写,名为project,这是一个Android Studio项目。编译好C ++库并生成.a文件,但它不会与共享库链接。这是我的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'
}

这是我的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)

当像这样编译时,我得到了一堆undefined reference错误,即使这些方法是由核心模块实现的。我错过了什么?

1 个答案:

答案 0 :(得分:1)

https://stackoverflow.com/a/22401410/755804读到:

仅将LOCAL_LDLIBS用于系统库依赖项。如果你想指向另一个库,最好将它们列在LOCAL_STATIC_LIBRARIES和LOCAL_SHARED_LIBRARIES中(即使这意味着为它们定义一个PREBUILT_XXX模块),因为这可以让构建系统解决依赖关系并自动排序你。