我正在通过稳定的gradle (http://tools.android.com/tech-docs/external-c-builds)通过CMake测试新的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)二进制文件是在build文件夹中生成的:
app/build/intermediates/cmake/debug/lib/armeabi/mybinary
app/build/intermediates/cmake/debug/lib/x86_64/mybinary
...但它们似乎不会被复制到apk中的任何地方。
哪种方法才是处理这种需求的正确方法?gradle-task是要走的路吗?
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");发布于 2016-08-13 16:02:07
好吧,我已经找到了一个看起来很舒服的解决方案,但可能还有更合适的方法;
默认情况下,CMakeLists.txt被放在myAppProject/app中,所以我在CMakeLists.txt中添加了下面这一行:
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${ANDROID_ABI}")完整的app/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})这样一来,任何可执行的二进制文件都会被直接编译到assets文件夹中,该文件夹位于名为目标ABI的子文件夹中,例如:
assets/armeabi/mybinary
assets/x86_64/mybinary
... 为了在应用程序内部使用正确的二进制文件,应该选择正确的二进制文件:
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");然后,应使用正确的执行权限将二进制文件从assets文件夹中复制出来:
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'
}https://stackoverflow.com/questions/38917297
复制相似问题