我创建了一个支持C ++的Android Studio项目,其中包含一个库模块。名为native-lib.cpp
的C ++文件自动添加到我的项目中。当我现在构建项目时,* .so文件的输出名称为libnative-lib.so
。
如果我将native-lib.cpp
重命名为其他任何内容(使用重构/重命名功能),C ++文件将从我的项目中消失,直到我更改名称为止。
那么如何重命名我的本机库,或者设置输出文件的名称?
答案 0 :(得分:3)
我很高兴你能搞清楚。我会在这里发布答案以供参考。
CMakeLists.txt<< =应该在您的应用模块的根目录(my_project/app/CMakeLists.txt
)中
# https://developer.android.com/studio/projects/add-native-code.html#create-cmake-script
# Minimum version of CMake
cmake_minimum_required(VERSION 3.4.1)
# adding CEC library
# add_library structure: add_library(lib_name lib_type_STATIC_or_SHARED source_file_path)
add_library(my_lib_name SHARED src/main/jni/my_cpp_file.cpp)
# include_directories is to provide the path to you native lib code
# include_directories structure: include_directories(native_lib_folder_path)
include_directories(src/main/jni/)
# adding Android log library
# find_library is used to find NDK API libraries (built in NDK libs)
# find_library structure: find_library(name_you_want_to_call_the_lib lib_name_in_ndk_api)
find_library(log-lib log)
# linking log lib to our native lib
# once you find the library, you have to link that library with your native library
# target_link_libraries structure: target_link_libraries(you_native_lib lib_found_using_find_library)
target_link_libraries(my_lib_name ${log-lib})
然后在build.gradle中,执行以下操作
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
然后,执行完全清理,使用“生成”菜单中提供的选项刷新C ++项目。
和@bitwise一样,如果必须,删除build
和.externalNativeBuild
文件夹的内容并重建项目。