Compile and use ABI-dependent executable binaries in Android with Android Studio 2.2 and CMake

后端 未结 2 1990
梦如初夏
梦如初夏 2020-12-13 15:28

I\'m testing out the new Android Studio C/C++ building via CMake through stable gradle (http://tools.android.com/tech-docs/external-c-builds).

In my app, an already

2条回答
  •  悲&欢浪女
    2020-12-13 16:00

    1. Make the executable files output to where the Android Gradle plugin expects libraries:

      set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

    2. Fool the plugin into thinking your executable is a shared object:

      add_executable(i_am_an_executable.so main.c)

    3. Check the APK:

      $ 7z l build/outputs/apk/app-debug.apk lib/                                                                                                                                    [2:08:56]
         Date      Time    Attr         Size   Compressed  Name
      ------------------- ----- ------------ ------------  ------------------------
                          .....         9684         4889  lib/armeabi/i_am_an_executable.so
                          .....         6048         1710  lib/arm64-v8a/i_am_an_executable.so
                          .....         9688         4692  lib/armeabi-v7a/i_am_an_executable.so
                          .....         5484         1715  lib/x86/i_am_an_executable.so
                          .....         6160         1694  lib/x86_64/i_am_an_executable.so
      
    4. Access and run your executable; it is located in context.getApplicationInfo().nativeLibraryDir.

    The downside to this is that you cannot set android:extractNativeLibs to false — I don't know of any way to access lib/ in an APK from within the app.

提交回复
热议问题