gpt4 book ai didi

android-ndk - 构建共享静态库依赖

转载 作者:行者123 更新时间:2023-12-02 03:21:07 24 4
gpt4 key购买 nike

我正在尝试使用 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错误,即使这些方法是由核心模块实现的。我错过了什么?

最佳答案

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

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

关于android-ndk - 构建共享静态库依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com