Search code examples
androidc++cmakeandroid-ndkninja

How remove libgnustl_static.a at linking when compiling apk with native code using Android Studio?


I am using Android Studio (3.4.1) to build an apk that contains a C++ part. The compile phase works, and the link phase throw an error.

The error :

clang++: error: no such file or directory: '/Users/homefolder/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/libgnustl_static.a'

This comes from the last line of the full link command :

/Users/homefolder/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=i686-none-linux-android23 --gcc-toolchain=/Users/homefolder/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/homefolder/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -O2 -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libmy_lib.so -o ../../../../build/intermediates/cmake/release/obj/x86/libmy_lib.so CMakeFiles/my_lib.dir/src/main/cpp/my_lib.cpp.o -llog ../../../../libs/x86/libssl.a ../../../../libs/x86/libcrypto.a -latomic -lm "/Users/homefolder/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/libgnustl_static.a"

Indeed this library does not exist in my sdk folder. The folder gnu-lib-stdc++does not exists.

As far as I understand, the native code is compiled with CMake. CMake generates build.ninja files and executes ninja to compile. If I modify manually these generated build.ninja files to remove this library and then call ninja in the terminal (in the correct folder), then it compiles and links perfectly.

The problem :

Where should I modify the Android Studio settings so these build.ninja files do not contain this library anymore? I don't want to modify the generated files each time I produce an apk.

What I have tried so far :

The only parameter file I found related to CMake is CMakeList.txt. And from documentation (1) , it seems like this library can be set up using CMAKE_ANDROID_STL_TYPE. I have put this to 'none' or 'system' in CMakeList.txt, but I see no difference in the build.ninja files.

# CMakeList.txt
set(CMAKE_ANDROID_STL_TYPE none)

Configuration :

  • Android Studio 3.4.1
  • Android SDK Tools 26.1.1
  • CMake 3.10.2 (not sure, 3.6 also installed)
  • macOS 10.14

Solution

  • And from documentation (1) , it seems like this library can be set up using CMAKE_ANDROID_STL_TYPE. I have put this to 'none' or 'system' in CMakeList.txt, but I see no difference in the build.ninja files.

    # CMakeList.txt
    set(CMAKE_ANDROID_STL_TYPE none)
    

    Wrong documentation. You want https://developer.android.com/ndk/guides/cmake. Set the following value in your build.gradle:

    android {
      defaultConfig {
        externalNativeBuild {
          cmake {
            arguments "-DANDROID_STL=none",
          }
        }
      }
    }