NoonanRosenblum
NoonanRosenblum

Reputation: 533

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 :

Upvotes: 1

Views: 621

Answers (2)

Dan Albert
Dan Albert

Reputation: 10509

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",
      }
    }
  }
}

Upvotes: 1

NoonanRosenblum
NoonanRosenblum

Reputation: 533

After further research thanks to Tsyvarev comments, I found the solution: I modified manually all CMakeCache.txt files in the .ExternalNativeBuild folder.

Before :

CMAKE_CXX_STANDARD_LIBRARIES:STRING=-latomic -lm "/Users/homefolder/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a"

Manual fix :

CMAKE_CXX_STANDARD_LIBRARIES:STRING=-latomic -lm

Then, the auto-generated build.ninja files do not contain the link to libgnustl_static.a anymore. And the apk compiles.

This fix does not make send to me. I though that all files in .ExternalNativeBuild are auto-generated and erased at each new build. The name itself has cache in it, so it does not sounds like a file a user should modify. Yet it was the working solution to my problem.

I believe it is these CCMakeCaches.txt files are generated at the first build, and then re-used for the following build requests. This is very not logical to me. I do not understand why set(CMAKE_ANDROID_STL_TYPE none) did not work.

Upvotes: 0

Related Questions