使用Android Studio 2.2和CMake在Android中编译和使用ABI相关的可执行二进制文件
我正在通过CMake通过stable gradle()测试新的Android Studio C/C++构建 在我的应用程序中,一个已经扎根的设备需要使用我在Android Studio中编译的ABI依赖的二进制文件 当我试图用使用Android Studio 2.2和CMake在Android中编译和使用ABI相关的可执行二进制文件,android,android-studio,gradle,android-ndk,cmake,Android,Android Studio,Gradle,Android Ndk,Cmake,我正在通过CMake通过stable gradle()测试新的Android Studio C/C++构建 在我的应用程序中,一个已经扎根的设备需要使用我在Android Studio中编译的ABI依赖的二进制文件 当我试图用 add_library(mylib SHARED mylib.c) 它会自动编译并复制到APK的lib/[ABI]文件夹中(例如/lib/armeabi/mylib.so),但如果我使用以下命令编译可执行二进制文件: add_executable(mybinary my
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;
}
好的,我已经找到了一个解决方案,看起来很舒服,但可能有更合适的方法 默认情况下,CMakeLists.txt放在myAppProject/app中,因此我在CMakeLists.txt中添加了这一行:
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${ANDROID_ABI}")
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)
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})
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})
完整的应用程序/CMakeLists.txt:
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${ANDROID_ABI}")
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)
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})
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})
完整的app/src/main/cpp/mybinary/CMakeLists.txt:
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${ANDROID_ABI}")
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)
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})
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})
完整的app/src/main/cpp/mylib/CMakeLists.txt:
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${ANDROID_ABI}")
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)
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})
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");
然后,应使用正确的执行权限从资产文件夹中复制二进制文件:
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'
}
设置(CMAKE_RUNTIME_output_DIRECTORY“${CMAKE_LIBRARY_output_DIRECTORY}”)
添加可执行文件(i\u an_executable.so main.c)
上下文.getApplicationInfo().nativeLibraryDir
中这样做的缺点是,您无法将
android:extractNativeLibs
设置为false
——我不知道从应用程序内部访问APK中的lib/
。这太好了。为什么要这样麻烦呢?对于共享库,机制已经存在,只需使用loadlibrary()加载它并使用。我没有在我的资产文件夹中获得任何构建文件,只是创建了空目录。如果我没记错的话,前一段时间有一个Gradle更新给了我类似的问题;我已经用应用程序的build.gradle文件更新了答案,请特别注意“defaultConfig”部分。原来的gradle.build(作为整个项目)要复杂得多,所以我删掉了不必要的部分。无论如何,我已经再次更新了答案,以更好地反映我的实际CMakeLists配置。查看3个CMakeLists.txt文件。但是,您是否在gradle构建文件中显式设置了“目标”?我发布了一个示例代码:使用getResources().getAssets().OpenNoAssetFD()打开嵌入的二进制文件,以便您自己提取它。不要强迫Android提取你的库。“添加可执行文件”不是用来创建命令行程序而不是共享库吗?为什么它被命名为“我是一个可执行文件,所以”