Skip to content Skip to sidebar Skip to footer

Android Studio Cmake - Shared Library Missing Libc++_shared.so? Can Cmake Bundle This?

Now that Android Studio 2.2 is released officially, I'm migrating from my old ndk-build process to try and use CMake within AS. As I'm incorporating several codebases from within m

Solution 1:

I just add this script to moudle's build.gradle:

externalNativeBuild {
        cmake {
            cppFlags ""arguments"-DANDROID_STL=c++_shared"
        }
    }

it will package 'libc++_shared.so' in the apk file

Solution 2:

I wrote a CMake config that should package the STL files: https://github.com/jomof/ndk-stl/blob/master/ndk-stl-config.cmake

Copy this file next to your CMakeLists.txt and inside CMakeLists.txt do

include(ndk-stl-config.cmake)

Let me know if you have problems

Solution 3:

add this in your build.gradle (Module: app)

externalNativeBuild {
            cmake {
                cppFlags "-std=c++14 -fexceptions -frtti"arguments"-DANDROID_ARM_NEON=TRUE",'-DANDROID_STL=c++_shared'
            }
        }

Solution 4:

As Gerry pointed out, the latest changes to the audio-echo sample project (https://github.com/googlesamples/android-ndk/pull/298) include changes that worked for me. I added this to the bottom of my CMakeLists.txt file.

# Android Studio CMake does not pack stl shared libraries, so app needs to pack# the right shared lib into APK. The following code find right stl type and copy# the needed shared lib into app's app/src/main/jniLibs, android studio assembles# it into the final APK# Helper function to retrieve shared stl path and name in NDK# stl_path: the path to the NDK's shared lib path; empty if not using shared stlfunction(get_stl_info stl_path stl_name)
   # assume app not uses shared stl libset(${stl_path}"" PARENT_SCOPE)
   if(NOT ${ANDROID_STL} MATCHES "_shared")
       return()
   endif()

   # using shared lib, config lib name and pathif("${ANDROID_STL}" MATCHES "c\\\+\\\+_")
       # app uses c++_shared for stl typeset(stlPath "llvm-libc++/libs/${ANDROID_ABI}")
       set(stlName "libc++_shared.so")
   elseif(${ANDROID_STL} MATCHES "gnustl_")
       set(stlPath "gnu-libstdc++/4.9/libs/${ANDROID_ABI}")
       set(stlName "libgnustl_shared.so")
   else()
       # this sample not supporting other stl types
       message(FATAL_ERROR "Not Suppored STL type: ${ANDROID_STL}")
       return()
   endif()

   set(${stl_path}${ANDROID_NDK}/sources/cxx-stl/${stlPath} PARENT_SCOPE)
   set(${stl_name}${stlName} PARENT_SCOPE)
endfunction()

# force copying needed shared stl lib into ${project}/app/src/main/jniLibs# so it will be packed into APK
get_stl_info(ndk_stl_path  ndk_stl_name)
if(NOT ${ndk_stl_path} STREQUAL "")
    set(jniLibs_dir "${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs")
    add_custom_command(TARGET mylibrary PRE_BUILD
                   COMMAND "${CMAKE_COMMAND}" -E
                   copy ${ndk_stl_path}/${ndk_stl_name}"${jniLibs_dir}/${ANDROID_ABI}/${ndk_stl_name}"
                   COMMENT "Copying Shared library to the packing directory")
endif()

I guess it's a workaround that we'll be able to do without some day... Note you have to change the line add_custom_command(TARGET mylibrary PRE_BUILD and replace mylibrary with your target name.

Solution 5:

Add the following line in Application.mk

APP_STL := c++_shared

Post a Comment for "Android Studio Cmake - Shared Library Missing Libc++_shared.so? Can Cmake Bundle This?"