4

I recently encountered a project need to use cmake compiled .so library.When compiled with ndk-build, the size of .so library is 415k.When compiled with cmake , the size of .so library is 4.5M.

plugin for Gradle versio:gradle:3.0.0', gradle version: gradle-4.1-all.zip

build.gradle cmake configuration:

ndk {
        abiFilters 'armeabi','armeabi-v7a','x86'
    }

    externalNativeBuild {
        cmake {
            arguments '-DANDROID_PLATFORM=android-21',
                    '-DANDROID_TOOLCHAIN=clang',
                    '-DCMAKE_BUILD_TYPE=Release'
            targets 'device'
        }
    }

CMakeLists.txt file is as follows:

cmake_minimum_required(VERSION 3.4.1)
set (CMAKE_CXX_STANDARD 14)

set(CMAKE_VERBOSE_MAKEFILE ON)

add_library(so SHARED  IMPORTED)

set_target_properties( 
                  so 
                  PROPERTIES IMPORTED_LOCATION
                  ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/so.so)


add_library( 
         myso
         SHARED
         XXX.cpp
         .......
         xxx.c
         ...
         )

target_include_directories(myso  PRIVATE
                       src
                       .....
                       )

find_library( 
          log-lib
          log )
target_link_libraries( 
                   myso
                   so 
                   android
                   log
                   )
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../dis)
set_target_properties(myso  
                  PROPERTIES
                  LIBRARY_OUTPUT_DIRECTORY
                  "${distribution_DIR}/lib/${ANDROID_ABI}")

I compare the symbols in two .so library files and found that the small .so library has 1000 lines, The same as the first 1000 lines of the big .so library, the larger ones have more symbols than the smaller ones. Is my cmake configuration wrong?

Guang Yang
  • 41
  • 5
  • targets in build.gradle is 'myso' – Guang Yang Nov 17 '17 at 08:44
  • Which .so file are you looking at? CMake won't strip .so files even in release mode. The stripping will be performed by gradle when packaging the APK. – Michael Nov 17 '17 at 10:13
  • THX, The same JNI project using ndk-build and cmake build .so library, cmake build a very large library. I can not find the reason. – Guang Yang Nov 17 '17 at 10:32
  • Yeah, but _which_ exact copy of the .so file are you looking at? (there are various intermediate copies). You should be looking at the one inside the APK, because that's the one that has been stripped. – Michael Nov 17 '17 at 10:50
  • Sorry, I do not understand what you mean. After compilation I can get only one myso.so library for each ABI. Why there are various intermediate copies? – Guang Yang Nov 20 '17 at 08:15
  • I understand what you mean, thank you very much!!! – Guang Yang Nov 21 '17 at 10:37
  • see https://stackoverflow.com/questions/48987208/how-to-enable-stripping-for-cmake-on-android, work for me. – Jaeyo Keh May 27 '21 at 03:23

1 Answers1

0

Add -DANDROID_STL=c++_shared to build.gradle

It is possible static c++ lib just included to your .so.

Nikolay
  • 21
  • 4
  • Wherabouts should this setting be added in the build.gradle file? Can you provide an example ? – Joman68 Jan 21 '21 at 02:41
  • externalNativeBuild { cmake { arguments '-DANDROID_PLATFORM=android-21', '-DANDROID_TOOLCHAIN=clang', '-DCMAKE_BUILD_TYPE=Release' -DANDROID_STL=c++_share targets 'device' } – Nikolay Jan 21 '21 at 07:44