You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Conan管理器交叉编译Android时出现NDK无效错误

问题描述

我正尝试在Windows主机上使用Conan安装Android项目,为此创建了如下配置文件:

[settings]
os=Android
os.api_level=23
os_build=Windows
arch_build=x86_64
arch=armv8
compiler=clang
compiler.libcxx=libc++
compiler.version=9

build_type=Release

[options]
[build_requires]
*: android-ndk/r23, cmake/3.19.0, ninja/1.10.1
[env]

执行命令 conan install .. --build=missing --profile android-arm64-v8a 时出现错误:

Cross-build from 'Windows:x86_64' to 'Android:armv8'
Installing (downloading, building) binaries...
ERROR: There are invalid packages (packages that cannot exist for this configuration):
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
android-ndk/r23: Invalid ID: os=Android is not supported by android-ndk (no binaries are available)
原因与修复方案

错误根源在于android-ndk是构建依赖,它需要运行在构建主机(Windows)上,而非目标平台(Android)。你当前的配置把android-ndk/r23放在全局[build_requires]下,Conan会尝试为目标平台Android寻找对应的android-ndk包,但实际上android-ndk没有Android平台的预编译包,仅提供构建主机(Windows/macOS/Linux)的版本。

修复方法

方法1:修正构建依赖的作用范围

修改[build_requires]部分,明确指定这些工具是构建主机的依赖,而非全局所有包的依赖:

[build_requires]
# 仅为构建主机指定构建工具
build_requirements: android-ndk/r23, cmake/3.19.0, ninja/1.10.1

或者更精准地指定android-ndk的构建主机配置:

[build_requires]
*: cmake/3.19.0, ninja/1.10.1
# 单独指定android-ndk适配构建主机
android-ndk/r23: os_build=Windows, arch_build=x86_64

方法2:添加NDK环境变量关联目标平台

在[env]部分补充NDK工具链的环境变量,确保Conan能正确关联Android目标平台:

[env]
CONAN_CMAKE_TOOLCHAIN_FILE = ${android-ndk:path}/build/cmake/android.toolchain.cmake

方法3:分离主机与目标配置文件(推荐)

  • 创建host-windows配置文件,包含Windows主机的设置和构建依赖:
    [settings]
    os_build=Windows
    arch_build=x86_64
    [build_requires]
    android-ndk/r23, cmake/3.19.0, ninja/1.10.1
    
  • 创建target-android-arm64配置文件,包含Android目标平台的设置:
    [settings]
    os=Android
    os.api_level=23
    arch=armv8
    compiler=clang
    compiler.libcxx=libc++
    compiler.version=9
    build_type=Release
    
  • 执行命令时同时指定两个配置:
    conan install .. --build=missing --profile:build host-windows --profile:host target-android-arm64
    

这种方式彻底分离构建环境与目标环境,避免配置冲突。

内容的提问来源于stack exchange,提问作者montjet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:46:08