bitwise bitwise - 3 months ago 69
C++ Question

Android Studio rename native library

I've created an Android Studio project with C++ support, with one library module. A C++ file named

was automatically added to my project. When I build the project now, the *.so file's output name is

If I rename
to anything else(using the refactor/rename feature), the C++ file just disappears from my project until I change the name back.

So how do I rename my native library, or set the name of the output file?


I'm glad you were able to figure it out. I'll post the answer here for reference anyway.

CMakeLists.txt <<= should in your app module's root dir (my_project/app/CMakeLists.txt)


# 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)

# 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})

Then in build.gradle, do the following

externalNativeBuild {
    cmake {
        path 'CMakeLists.txt'

Then, do a full clean, refresh the C++ project using the option provided in the Build menu.

And like @bitwise posted, if you have to, delete the contents of build and .externalNativeBuild folder and rebuild the project.