三元组 x64-android、arm-neon-android 和 arm64-android 通过 vcpkg 精心策划的注册表持续集成进行了测试。
Android 版本要求
下载 Android NDK
将环境变量“
ANDROID_NDK_HOME
”设置为 Android NDK 安装以设置 Android 工具链。 例如:export ANDROID_NDK_HOME=/home/your-account/Android/Sdk/ndk-bundle
或:
export ANDROID_NDK_HOME=/home/your-account/Android/android-ndk-r26d
注意:仍然需要安装以主机作为目标的 g++ 或 C++ 编译器才能获得主机依赖项。
vcpkg 三联密码及其相应的 Android ABI
有 6 个不同的 Android ABI,每个映射到一个 vcpkg 三联密码。 下表概述了从 vcpkg 体系结构到 android 体系结构的映射:
VCPKG_TARGET_TRIPLET | ANDROID_ABI | ANDROID_ARM_NEON |
---|---|---|
arm64-android | arm64-v8a | |
arm-android | armeabi-v7a | OFF |
arm-neon-android | armeabi-v7a | ON |
x64-android | x86_64 | |
x86-android | x86 | |
armv6-android | armeabi |
在 Docker 容器中构建 Android 库
可以构建 Android 库,例如 Ubuntu Docker 容器中的 jsoncpp
。
使用以下内容创建 Dockerfile
:
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND noninteractive
RUN \
apt-get update && \
apt-get -y upgrade
RUN \
apt-get -y --no-install-recommends install git g++ wget curl zip vim pkg-config tar cmake unzip ca-certificates
# Download Android NDK
RUN \
wget https://dl.google.com/android/repository/android-ndk-r26d-linux.zip && \
unzip android-ndk-r26d-linux.zip && \
rm -rf android-ndk-r26d-linux.zip
ENV ANDROID_NDK_HOME /android-ndk-r26d
RUN git clone https://github.com/microsoft/vcpkg
WORKDIR vcpkg
RUN ./bootstrap-vcpkg.sh
ENV PATH "/vcpkg:$PATH"
ENV VCPKG_ROOT "/vcpkg"
WORKDIR /project
构建图像并发布新的容器。
docker build . -t "vcpkg-android"
docker run -it "vcpkg-android" bash
在容器中,使用以下内容创建 /project/vcpkg.json
:
{
"dependencies": [
"jsoncpp"
],
"builtin-baseline": "1e68748a7c6914642ed686b2e19c3d688bca150a"
}
最后,运行 vcpkg install --triplet x64-android
以构建面向 android jsoncpp
。
使用 Vulkan SDK
vcpkg 有一个 vulkan
包允许你 find_package(Vulkan)
。 必须提供“VULKAN_SDK
”环境变量才能使用它。
export VULKAN_SDK=/usr/local
./vcpkg install vulkan
NDK 已包含其每个体系结构的 Vulkan 标头和 libvulkan.so
库。
可以考虑为每个安装使用 export VULKAN_SDK=...
来将它们暴露给 vcpkg。 不过将 set(ENV{VULKAN_SDK} ...)
放入三联密码文件中可跳过重复的工作。
如果使用的是 NDK 21.3.6528147 或更低版本,则如下所示:
# In android triplets... (e.g. arm64-android.cmake)
set(VCPKG_CMAKE_SYSTEM_NAME Android)
# ...
# If your API level is 30, libvulkan.so is at $ENV{ANDROID_NDK_HOME}/platforms/android-30/arch-arm64/usr/lib
set(ENV{VULKAN_SDK} $ENV{ANDROID_NDK_HOME}/sysroot/usr)
请注意,自 NDK 22 起,sysroot 的位置已经改变。 (有关详细信息,请参阅[BUG] 自 r22? 起缺少 sysroot 标头)。
如果想要使用最新版本,请查看 NDK 文档的 BuildSystemMaintainers.md,然后放入系统合适的路径。
例如,MacOS 用户将使用如下例所示路径:
# In android triplets... (e.g. arm64-android.cmake)
set(VCPKG_CMAKE_SYSTEM_NAME Android)
# ...
# If your API level is 30, libvulkan.so is at $ENV{ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/aarch64-linux-android/30
set(ENV{VULKAN_SDK} $ENV{ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr)
请为所有 Android 三联密码执行此操作,可以安装 vulkan
和要求它的包。 (例如 vulkan-hpp
)
“vcpkg install vulkan-hpp:arm64-android”
user@host$ ./vcpkg install vulkan-hpp:arm64-android
Computing installation plan...
The following packages will be built and installed:
* vulkan[core]:arm64-android -> 1.1.82.1-1
vulkan-hpp[core]:arm64-android -> 2019-05-11-1
Additional packages (*) will be modified to complete this operation.
Detecting compiler hash for triplet arm64-android...
...
Starting package 1/2: vulkan:arm64-android
Building package vulkan[core]:arm64-android...
-- Using community triplet arm64-android. This triplet configuration is not guaranteed to succeed.
-- [COMMUNITY] Loading triplet configuration from: /.../vcpkg/triplets/community/arm64-android.cmake
-- Querying VULKAN_SDK Environment variable
-- Searching /.../Library/Android/sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/vulkan/ for vulkan.h
-- Found vulkan.h
-- Performing post-build validation
-- Performing post-build validation done
...
Building package vulkan[core]:arm64-android... done
Installing package vulkan[core]:arm64-android...
Installing package vulkan[core]:arm64-android... done
Elapsed time for package vulkan:arm64-android: 35.9 ms
Starting package 2/2: vulkan-hpp:arm64-android
Building package vulkan-hpp[core]:arm64-android...
-- Using community triplet arm64-android. This triplet configuration is not guaranteed to succeed.
-- [COMMUNITY] Loading triplet configuration from: /.../vcpkg/triplets/community/arm64-android.cmake
-- Using cached /.../vcpkg/downloads/KhronosGroup-Vulkan-Hpp-5ce8ae7fd0d9c0543d02f33cfa8a66e6a43e2150.tar.gz
-- Cleaning sources at /.../vcpkg/buildtrees/vulkan-hpp/src/e6a43e2150-4f344cd911.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source /.../vcpkg/downloads/KhronosGroup-Vulkan-Hpp-5ce8ae7fd0d9c0543d02f33cfa8a66e6a43e2150.tar.gz
-- Using source at /.../vcpkg/buildtrees/vulkan-hpp/src/e6a43e2150-4f344cd911.clean
-- Performing post-build validation
-- Performing post-build validation done
...
Building package vulkan-hpp[core]:arm64-android... done
Installing package vulkan-hpp[core]:arm64-android...
Installing package vulkan-hpp[core]:arm64-android... done
Elapsed time for package vulkan-hpp:arm64-android: 144.5 ms
Total elapsed time: 1.013 s
The package vulkan-hpp:arm64-android is header only and can be used from CMake via:
find_path(VULKAN_HPP_INCLUDE_DIRS "vulkan/vulkan.hpp")
target_include_directories(main PRIVATE ${VULKAN_HPP_INCLUDE_DIRS})
Android 项目示例
文件夹 docs/examples/vcpkg_android_example_cmake 提供了一个工作示例,其中包含使用 jsoncpp 库的 Android 库:
详细信息
CMakeLists.txt 只是使用
find_package
和target_link_library
可以使用 compile.sh 脚本来选择“android abi”/“vcpkg triplet”的任何匹配对,并测试编译。
虚拟 my_lib.cpp 文件使用 jsoncpp 库
注意
此示例仅编译 Android 库,因为编译完全成熟的 Android 应用不在本文档的介绍范围内。
测试示例:vcpkg_android.cmake
使用 vcpkg_android.cmake 进行测试。
文件夹“vcpkg_android_example_cmake_script”提供相同的示例,并使用 cmake 脚本来简化用法。
详细信息
如果设置了标志“
VCPKG_TARGET_ANDROID
”,则主 CMakeLists.txt 会加载 vcpkg_android.cmake:if (VCPKG_TARGET_ANDROID) include("cmake/vcpkg_android.cmake") endif()
重要
在调用
project()
之前放置这些行。显示compile.sh 脚本,然后可以使用简单的 cmake 调用为 android 进行编译,例如:
cmake .. -DVCPKG_TARGET_ANDROID=ON -DANDROID_ABI=armeabi-v7a
使用 vcpkg 和 Android prefab 存档(AAR 文件)使用库
注意
本部分介绍 vcpkg 的实验性功能,此类功能随时可能会更改或删除。
vcpkg 可以导出 Android 存档(AAR 文件)。 创建存档后,可以作为本机依赖项导入 Android Studio。 使用 Android Studio 的 prefab 工具自动使用存档。
有关 Prefab 的详细信息,请参阅:
- Prefab 官方文档。
- Android 开发人员博客的博客文章:Android Studio 4.0 中的本机依赖项
注意
Android Studio 用户:Android Studio 4+ 支持的 prefab 包。
要求
ndk <required>
将环境变量“ANDROID_NDK_HOME
”设置为 android ndk 安装。 例如:
export ANDROID_NDK_HOME=/home/your-account/Android/Sdk/ndk-bundle
7zip <required on windows>
或zip <required on linux>
maven <optional>
Android 三联密码
注意
需要全部四种体系结构才能使用“prefab”(请见下文)。 如果缺少任何一种体系结构,则导出将会失败。
导出示例 [jsoncpp]
首先,为所有受支持的 Android 体系结构运行“vcpkg install”(必须导出所有体系结构):
./vcpkg install jsoncpp:arm-android jsoncpp:arm64-android jsoncpp:x64-android jsoncpp:x86-android
然后,导出 prefab:
注意:
--prefab-maven
标志是可选的。 如果安装了 maven,请调用。- 标志“
--prefab-debug
”将输出有关如何通过 gradle 使用 prefab 存档的说明。
./vcpkg export --triplet x64-android jsoncpp --prefab --prefab-maven --prefab-debug
应会看到如下所示的输出:
The following packages are already built and will be exported:
jsoncpp:arm64-android
Exporting package jsoncpp...
[DEBUG] Found 4 triplets
arm64-android
x64-android
x86-android
arm-android
...
... Lots of output...
...
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
...
... Lots of output...
...
[INFO] BUILD SUCCESS
[INFO] Total time: 2.207 s
[INFO] Finished at: 2020-05-10T14:42:28+02:00
...
... Lots of output...
...
[DEBUG] Configuration properties in Android Studio
In app/build.gradle
com.vcpkg.ndk.support:jsoncpp:1.9.2
And cmake flags
externalNativeBuild {
cmake {
arguments '-DANDROID_STL=c++_shared'
cppFlags "-std=c++17"
}
}
In gradle.properties
android.enablePrefab=true
android.enableParallelJsonGen=false
android.prefabVersion=${prefab.version}
Successfully exported jsoncpp. Checkout .../vcpkg/prefab
导出后的输出目录
prefab
└── jsoncpp/
├── aar/
│ ├── AndroidManifest.xml
│ ├── META-INF/
│ │ └── LICENSE
│ └── prefab/
│ ├── modules/
│ │ └── jsoncpp/
│ │ ├── libs/
│ │ │ ├── android.arm64-v8a/
│ │ │ │ ├── abi.json
│ │ │ │ ├── include/
│ │ │ │ │ └── json/
│ │ │ │ │ ├── json.h
│ │ │ │ │ └── ....
│ │ │ │ └── libjsoncpp.so
│ │ │ ├── android.armeabi-v7a/
│ │ │ │ ├── abi.json
│ │ │ │ ├── include/
│ │ │ │ │ └── json/
│ │ │ │ │ ├── json.h
│ │ │ │ │ └── ....
│ │ │ │ └── libjsoncpp.so
│ │ │ ├── android.x86/
│ │ │ │ ├── abi.json
│ │ │ │ ├── include/
│ │ │ │ │ └── json/
│ │ │ │ │ ├── json.h
│ │ │ │ │ └── ....
│ │ │ │ └── libjsoncpp.so
│ │ │ └── android.x86_64/
│ │ │ ├── abi.json
│ │ │ ├── include/
│ │ │ │ └── json/
│ │ │ │ ├── json.h
│ │ │ │ └── ....
│ │ │ └── libjsoncpp.so
│ │ └── module.json
│ └── prefab.json
├── jsoncpp-1.9.2.aar
└── pom.xml
通过 vcpkg 和 prefab 使用 [jsoncpp] 的示例
请参阅此处的示例 GitHub 存储库: