Skip to content

[question] How to properly cross-compile for Android with android-ndk package? #11096

Description

@nmgwddj

In the official documentation, I found the following link: https://docs.conan.io/en/latest/integrations/cross_platform/android.html#using-android-ndk-package-tool-require, through which I learned that android-ndk can be referenced through tool_requires. The documentation states that when we use this package, the toolchain file and the variables required by CMake are generated. But when I execute conan install and then pass CMake include conanbuildinfo.cmake, I get the following error when I execute CMake initialization:

[cmake] CMake Error at /opt/homebrew/Cellar/cmake/3.23.0/share/cmake/Modules/Platform/Android-Determine.cmake:209 (message):
[cmake]   Android: Neither the NDK or a standalone toolchain was found.
[cmake] Call Stack (most recent call first):
[cmake]   /opt/homebrew/Cellar/cmake/3.23.0/share/cmake/Modules/CMakeDetermineSystem.cmake:160 (include)
[cmake]   CMakeLists.txt:24 (project)

Obviously, Android NDK related environment variables are not injected into CMake. Then I saw in the description of the android-ndk package that CMakeDeps and CMakeToolchain need to be specified in generators, but when I After setting up both generators and doing conan install I get the following error:

[cmake] conanfile.py (conan-cross-platform-build/None): Generator 'CMakeToolchain' calling 'generate()'
[cmake] conanfile.py (conan-cross-platform-build/None): ERROR: Traceback (most recent call last):
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conans/client/generators/__init__.py", line 177, in write_generators
[cmake]     generator.generate()
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conan/tools/cmake/toolchain/toolchain.py", line 164, in generate
[cmake]     save(self.filename, self.content)
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conan/tools/cmake/toolchain/toolchain.py", line 157, in content
[cmake]     context = self._context()
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conan/tools/cmake/toolchain/toolchain.py", line 144, in _context
[cmake]     blocks = self.blocks.process_blocks()
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conan/tools/cmake/toolchain/blocks.py", line 43, in process_blocks
[cmake]     content = b.get_rendered_content()
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conan/tools/cmake/toolchain/blocks.py", line 66, in get_rendered_content
[cmake]     context = self.values
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conan/tools/cmake/toolchain/blocks.py", line 58, in values
[cmake]     self._context_values = self.context()
[cmake]   File "/usr/local/Cellar/conan/1.47.0/libexec/lib/python3.10/site-packages/conan/tools/cmake/toolchain/blocks.py", line 308, in context
[cmake]     raise ConanException('CMakeToolchain needs tools.android:ndk_path configuration defined')
[cmake] conans.errors.ConanException: CMakeToolchain needs tools.android:ndk_path configuration defined

I've been tossing around for days, and I haven't been able to find a solution by constantly searching for issues, google, conan index example, etc.

The requirements are very simple. I hope to manage NDK through conan tool requires, and correctly inject various environment variables and toolchain information of NDK into my existing CMakeLists.txt and then perform cross-compilation. Because I have implemented various cross-compilation scenarios for Windows, macOS, Linux, and iOS through the existing Conan capabilities, and now it is only Android, I am very distressed.

Can someone give a complete example and tell me how to use it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions