一、AndroidStudio 混合编程流程
1.1 设置支持的arm的类型
在build.gradle中设置arm的类型,如果不设置也就是忽略此步骤,默认支持所有的类型,运行项目后,系统根据自身的arm类型,自动选择加载
......
android {
defaultConfig {
....
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi-v7a','arm64-v8a'
}
......
}
......
1.2 编译自定义库和第三方库
1.2.1 编译第三方库
CMakeList.txt:
#1.设置第三方库头文件所在位置
#此目录为CMakeList.txt父目录下的thirdLibrary目录
include_directories(${PROJECT_SOURCE_DIR}/thirdLibrary/include)
add_library(thirdLibrary
SHARED
IMPORTED)
# 设置第三方动态库属性(存储位置)
#此目录为CMakeList.txt父目录下的opus/libs目录
set_target_properties(thirdLibrary
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/thirdLibrary/libs/${ANDROID_ABI}/libthirdLibrary.so)`
1.2.2 在自定义库中加载第三方库
add_library( # Sets the name of the library.
selfLibrary
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
selfLibraryFunction.cpp
}
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# 链接第三方库
target_link_libraries( # Specifies the target library.
selfLibrary
thirdLibrary
# Links the target library to the log library
# included in the NDK.
${log-lib} )
二、Eclipse 混合编程流程
2.1 编译自定义库和第三方库
2.1.1 编译第三方库
Android.mk
#include $(CLEAR_VARS)
#LOCAL_MODULE := thirdLibrary
#LOCAL_SRC_FILES := ./kwPrivateData/libs/armeabi-v7a/libthirdLibrary.so
#include $(PREBUILT_SHARED_LIBRARY)
2.1.2 在自定义库中加载第三方库
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := thirdLibrary
LOCAL_MODULE := selfLibrary
LOCAL_C_INCLUDES := \
$(LOCAL_PATH) \
LOCAL_SRC_FILES := \
selfLibraryFunction.cpp
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)