4

We're developing an Android library with native part. We're mostly Java developers, not C++. Our tiny C++ experience was enough to write the native code we need but we've encountered unexpected troubles with stripping (which doesn't related to programming directly). Due to some reasons we do need the output native library (xxx.so file) stripped. The problem is it's hard to find info how to do this in our particular case. There are a lot of results in Google but we don't' understand how to apply them. The most popular answer is "just check the related checkbox in your IDE". Hmm, what checkbox? We tried to set Jni Debuggable checkbox to true in config (it looks relevant) - no effect. We added -s flag (as suggested in many answers) as follows:

defaultConfig {
    ..
    externalNativeBuild {
        cmake {
            cppFlags "-fexceptions -s"
            abiFilters 'armeabi', 'x86'
        }
    }
}

No effect. We added the same flag according to this answer like the following:

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -s")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -s")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")

No effect again! Finally we tried the straightforward variant to apply strip utility to our output library manually (as many guys suggested). The problem is the utility says Unable to recognize the format of the input file 'xxx.so'. Weird... Our C++ developers (not for Android) couldn't help us, they "just check the related checkbox in IDE". We're absolutely sure it shouldn't be difficult but spent a lot of time for this task and read tons of pages without result.

@anton-malyshev is right! I used elfedit and objdump from the same package before and they worked well with the same file that confused me. I applied appropriate strip from NDK, now it works without errors but do nothing though. I used

strip -s -v xxx.so

syntax, this shows message copy from 'xxx.so' [elf32-littlearm] to 'sta10988' [elf32-littlearm], but xxx.so file stays unchanged. What is sta10988? A temp file? If my understanding is correct then the utility should reduce library size since it contains many symbols (according to elfedit and objdump). I tried to apply other options for strip (like -I -O) but didn't manage to get any working combination. Sorry, I'm not a fan of console apps. How to use this utility correctly?

2 Answers2

1

In Android Studio 3.2+ (that is, if you use externalNativeBuild in build.gradle), the stripped libraries are accumulated under app/build/intermediates/transforms/stripDebugSymbol.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • is there a way to put stripped cmake built libraries in separate folder(jni/libs) like NDK_LIBS_OUT in ndk-build like this : https://stackoverflow.com/questions/32894571/why-is-ndk-build-producing-two-different-libraries-one-very-large-and-one-small – Vivek Mangal Apr 20 '22 at 15:11
  • 1
    @VivekMangal look at *[CMake: Platform independent binary stripping for release builds](https://stackoverflow.com/a/71236302/192373)*. – Alex Cohn Apr 23 '22 at 16:53
0

The problem Unable to recognize the format of the input file 'xxx.so' appeared because you most probably tried to use system strip utility instead of the one included in Android NDK.

For example, for armeabi-v7a you should use the following one (correct the path for your NDK installation and version):

android-ndk-r15c/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-strip

or

android-ndk-r15c/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/arm-linux-androideabi/bin/strip

For other popular architectures - change the arm-linux-androideabi-4.9 part in path to aarch64-linux-android-4.9 for arm64-v8a, and to x86-4.9 for x86.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45