使用Android Studio 2.2和CMake在Android中编译和使用与ABI相关的可执行二进制文件

我正在通过稳定gradle( http://tools.android.com/tech-docs/external-c-builds )通过CMaketesting出新的Android Studio C / C ++版本。

在我的应用程序中,已经有根的设备需要使用我在Android Studio中编译的依赖于ABI的二进制文件。

当我尝试编译一个标准库

add_library(mylib SHARED mylib.c) 

它会自动编译并复制到APK的lib / [ABI]文件夹中(例如/lib/armeabi/mylib.so),但是如果我编译一个可执行的二进制文件:

 add_executable(mybinary mybinary.cpp) 

二进制文件是在生成文件夹内正确生成的:

 app/build/intermediates/cmake/debug/lib/armeabi/mybinary app/build/intermediates/cmake/debug/lib/x86_64/mybinary ... 

但他们似乎并没有被复制到apk里的任何地方。

哪个是正确的方法来处理这个需求? 这是一项艰巨的任务吗?

的build.gradle:

 apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { applicationId "com.my.app" minSdkVersion 10 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "" } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild{ cmake{ path "CMakeLists.txt" } } defaultConfig { externalNativeBuild { cmake { targets " arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_PLATFORM=android-21" cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2" cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2" abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a' } } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.1.1' compile 'com.android.support:design:24.1.1' compile 'com.android.support:recyclerview-v7:24.1.1' compile 'eu.chainfire:libsuperuser:1.0.0.201607041850' } 

的CMakeLists.txt

 cmake_minimum_required(VERSION 3.4.1) set(CMAKE_VERBOSE_MAKEFILE on) add_executable(mybinary ${CMAKE_CURRENT_SOURCE_DIR}/mybinary.cpp) target_link_libraries( mybinary libcustom) target_include_directories (mybinary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) 

mybinary.cpp

 #include <stdlib.h> #include <string> #include <iostream> using namespace std; int main(int argc, char *argv[]) { string hello = "Hello from C++"; cout << "Message from native code: " << hello << "\n"; return EXIT_SUCCESS; } 

应用程序如何与mybinary进行交互:

 import eu.chainfire.libsuperuser.Shell; ... Shell.SU.run("/path/to/mybinary"); 

Solutions Collecting From Web of "使用Android Studio 2.2和CMake在Android中编译和使用与ABI相关的可执行二进制文件"

好吧,我已经find了一个相当舒适的解决scheme,但可能有更合适的方法;

CMakeLists.txt默认放在myAppProject / app里,所以我把这行添加到CMakeLists.txt中:

 set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${ANDROID_ABI}") 

完整的应用程序/ CMakeLists.txt:

 cmake_minimum_required(VERSION 3.4.1) set(CMAKE_VERBOSE_MAKEFILE on) # set binary output folder to Android assets folder set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${ANDROID_ABI}") add_subdirectory (src/main/cpp/mylib) add_subdirectory (src/main/cpp/mybinary) 

完成app / src / main / cpp / mybinary / CMakeLists.txt:

 add_executable(mybinary ${CMAKE_CURRENT_SOURCE_DIR}/mybinary.cpp) # mybinary, in this example, has mylib as dependency target_link_libraries( mybinary mylib) target_include_directories (mybinary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) 

完成app / src / main / cpp / mylib / CMakeLists.txt:

 add_library( # Sets the name of the library. mylib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). # Associated headers in the same location as their source # file are automatically included. ${CMAKE_CURRENT_SOURCE_DIR}/mylib.cpp ) target_include_directories (mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) 

这样做,任何可执行的二进制文件被直接编译到名为目标ABI的子文件夹内的assets文件夹中,例如:

 assets/armeabi/mybinary assets/x86_64/mybinary ... 

为了在App中使用正确的二进制文件,应该select正确的二进制文件:

 String abi; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { abi = Build.SUPPORTED_ABIS[0]; } else { //noinspection deprecation abi = Build.CPU_ABI; } String folder; if (abi.contains("armeabi-v7a")) { folder = "armeabi-v7a"; } else if (abi.contains("x86_64")) { folder = "x86_64"; } else if (abi.contains("x86")) { folder = "x86"; } else if (abi.contains("armeabi")) { folder = "armeabi"; } ... AssetManager assetManager = getAssets(); InputStream in = assetManager.open(folder+"/" + "mybinary"); 

然后,应该使用正确的执行权限从资产文件夹中复制二进制文件:

 OutputStream out = context.openFileOutput("mybinary", MODE_PRIVATE); long size = 0; int nRead; while ((nRead = in.read(buff)) != -1) { out.write(buff, 0, nRead); size += nRead; } out.flush(); Log.d(TAG, "Copy success: " + " + size + " bytes"); File execFile = new File(context.getFilesDir()+"/mybinary"); execFile.setExecutable(true); 

就这样!

更新:gradle.build文件:

 apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25" defaultConfig { applicationId "com.myapp.example" minSdkVersion 10 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "" } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "CMakeLists.txt" } } defaultConfig { externalNativeBuild { cmake { targets "mylib", "mybinary" arguments "-DANDROID_TOOLCHAIN=clang" cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2" cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2" abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64' } } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:design:25.0.0' compile 'com.android.support:recyclerview-v7:25.0.0' compile 'com.android.support:cardview-v7:25.0.0' compile 'eu.chainfire:libsuperuser:1.0.0.201607041850' } 
  1. 使可执行文件输出到Android Gradle插件期望的库的位置:

    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

  2. 欺骗插件,认为你的可执行文件是一个共享对象:

    add_executable(i_am_an_executable .so main.c)

  3. 检查APK:

      $ 7z l build / outputs / apk / app-debug.apk lib / [2:08:56]
       date时间属性大小压缩名称
     ------------------- ----- ------------ ------------ ----------------------
                         ..... 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. 访问并运行可执行文件; 它位于context.getApplicationInfo().nativeLibraryDir

这样做的缺点是你不能设置android:extractNativeLibsfalse – 我不知道有任何方式来访问应用程序中的APK的lib/