2

我想使用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錯誤的,即使這些方法是由核心模塊來實現。我錯過了什麼?

+0

嘗試將定義jni.srcDirs的行替換爲'jni.srcDirs = []' –

回答

0

https://stackoverflow.com/a/22401410/755804寫着:

僅使用LOCAL_LDLIBS系統庫的依賴。如果你想指向另一個庫,最好將它們列在LOCAL_STATIC_LIBRARIES和LOCAL_SHARED_LIBRARIES中(即使這意味着爲它們定義一個PREBUILT_XXX模塊),因爲這可以讓構建系統解決依賴性併爲你自動排序。