-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] How can i build something for Android now? #4220
Comments
You should use the new two-profile (build/host) approach https://docs.conan.io/en/latest/systems_cross_building/cross_building.html#conan-v1-24-and-newer conan install libxml2/2.9.10@ --build missing --profile:host ./conan/profiles/android --profile:build ./conan/profiles/mac dev workflow profile android [settings] arch=armv8 compiler=clang build_type=Release [options] profile mac [settings] compiler=apple-clang build_type=Release [build_requires] |
Hi. Nice. I will try. In my case i need pass some settings to override arch, build type, api level etc: It will change or i can keep in this way? |
I need this too but have not tried it yet |
Now you can use -s:h and/or -s:b to override the settings. https://docs.conan.io/en/latest/reference/commands/consumer/install.html?highlight=settings
|
Hi, I have updated the ezored project and now i has a ndk-stl cmake file to find the correct .so and copy it. It is working nice without problems. |
The other problem is that im trying build the library using gradle default method, that is adding the CMakeLists.txt to it and leave it compile the project:
Problems found:
The main problem is number 2. The problem of number 1 is "acceptable", but will be better if we can use only one NDK. Anyone have a solution? |
Another approach, but without success:
The ndkPath i put manually only to know if it will build, but the same error happen. More one think to understand: There is any way to pass "ndkPath" from conan "android-ndk" recipe to gradle? |
Hi, I found the bug, but not understand how to solve. When i run search i get the ID for package armv8, but im building for armv7:
The arch armv7 use other folder, see:
And the armv7 packages are not used, only armv8. Something is pointing wrong. |
All conan files are generated with wrong arch:
|
I found the problem. The "gradle" don't pass variables like "PROJECT_CONFIG_ARCH" (and others) to CMakeLists.txt and it keep the default values. And you cannot pass it as fixed argument in build.gradle, because each ABI need be one ARCH. I think that we will need something more complex in build.gradle when it be copied to build folder. We can make something predefined from config files (like name, version and arch), replace on final build.gradle and after build. I don't know if we can change a gradle task/options to pass different arguments by ABI. Anyone knows how to do it? |
Gradle pass ANDROID_ABI to your CMake. But I do not understand what you want to achieve. :-) I for example build all third party c++ deps with Conan outside of AS and my C++ SDK with AS. PS: update AS ndkVersion "21.3.6528147" |
build the required packages outside of android studio and have it then on your machine, or upload the to a remote you can use https://github.com/conan-io/cmake-conan, the problem with automatic building is, if it takes long , and it does, users do not know what is going on |
I have a question about the usage of the new two-profile (build/host) approach - as @dmn-star suggests - in existing projects: is it guaranteed that everything works as before with older recipes when I change? Because if one recipe like the |
2 recipes are the future, and I hope and think conan will not change/break them (at least for the version 1) I did not have any problems when I switched to 2 profiles, about 6 month ago, everything just worked way better and more logical. |
Thanks @a4z, so I will try this first, change my whole project to the new approach 😃 |
Hm, I have trouble with conan install android_ndk_installer/r20@bincrafters/stable --profile:build linux_profile --profile:host android_profile
When I add this values (using conan install android_ndk_installer/r20@bincrafters/stable --profile:build linux_profile --profile:host android_profile
-s:h arch_build=x86_64 -s:h os_build=Linux |
The bincrafter ndk is not made to work with 2 profiles, the new on in the cci is PS: you will have to update your profile, either to compiler verion 9, with r21d or 11 with r22 |
Thanks for pointing this out, i have to change this. When I try the new one I get a different error: conan install android-ndk/r21d@_/_ --profile:build linux_profile --profile:host android_profile
I think the more I try to understand cross-compiling, the less I understand 😕. I have another issue open with a more general problem, no answer yet, if someone has some idea for me, very appreciated! |
@andioz you don't need to install android-ndk/r21d@/ it happens automatically. see your profile Build the lib that you need ( here libxml2 see #4220 (comment))
Now Conan builds libxml2/2.9.10 for Android. PS: I see you are on Linux to build libxml2 for Android
|
You can install android-ndk manually but keep in mind you need it on your Mac not on Android device. On your terminal run only
|
The android profile will always run in the context of the build profile, as all build requirements, that is the beauty you need to run |
yes. My /conan/profiles/Mac profile = the default profile, so I can run only >conan install android-ndk/r21d@ |
I'll try this! I don't dare to explain why I need this command, it's so dirty... But: I use it with |
Thanks, I will check later! |
Yes this is it, looks good! |
Do you need to build your own lib, which can already be built with CMake, for Android? Can you describe your problem in more detail, please? |
Hi, There is any problem in keep android-ank requirements in android profile?
It is working here. There is any problem? |
@dmn-star If you want to spend some minutes to see my very simple problem example, please have a look here: https://github.com/andioz/conan-cmake-multiplatform, I try to explain in this issue: #4224. Many thanks! |
I'll have a look |
Hi @andioz, What your specific problem? Why are you defining the toolchain for android? You only need simple things:
|
I have to explain more. I cannot use My simple example is a first step for to understand different alternatives without |
Or, lets say I want to use https://github.com/conan-io/cmake-conan for all build types (Linux, MacOS, Android, iOS). |
Currently I'm using a script to prepare the build directories, after that I can use CLion, yes. I can add -D options too, yes. But I have to know e.g. toolchain file location in advance, which may vary on changing android-ndk versions. |
Hi, Humm...i want this feature too 👍 In my case i need it for Maybe we will need a python script for this that capture it from .conan/data folder. I make an example: get-ndk-path.py
and now you can append to your shell script:
Link to download: This will solve for you and maybe for me :) |
The alternative without
I wouldn't recommend that, you should build your C++ code separate from the dependencies.
conan install ./dependcies/conanfile.txt --build --profile:build linux_profile --profile:host android_profile
https://docs.conan.io/en/latest/uploading_packages/artifactory_ce.html
|
I have now tried the following conanfile.txt [generators]
Now you could theoretically access all env vars defined in the android-ndk recipe. This works fine for ninja and CMake but not yet for android-ndk . As a workaround
|
the package folder is exportet so ndk-buid is in the path, but there are many ways to build with the ndk, I have a tool that simple detects the folder in the cache and gives me the path to the toolchain , and this is passed to standard cmake, works nicely . Building from Android dStudio , you should get the toolchain settings for free |
because of :-)
is only a workaround to get the NDK root (cut ndk-build) ANDROID_NDK_HOME = /Users/dmn/.conan/data/androidndk/r22///package/46f53f156846659bf39ad6675fa0ee8156e859fe/ And now add /cmake/android.toolchain.cmake to locate the cmake toolchain. |
but the the folder is added to PATH before the return .... do you think it would help to put this |
Wow, many ideas (including using VCPKG 😃; but thanks I want to use My resumee for today: no easy, obvious solution... |
Yes, I think so. I will change the ndk recipe locally tomorrow and try to build the example only with virt. dev and CMake. |
-- The C compiler identification is Clang 11.0.5
[2/2] Linking C shared library libhello-jni.so |
there is one-liner to get NDK path:
|
I would prefer to use virt env gen #4253 |
I'm just playing with this, looks good so far: using cmake_minimum_required(VERSION 3.18)
# Set conan environment variables
if(EXISTS "${CMAKE_BINARY_DIR}/environment.sh.env")
FILE(READ "${CMAKE_BINARY_DIR}/environment.sh.env" _contents)
STRING(REGEX REPLACE "\n" ";" _contents "${_contents}")
foreach(_line ${_contents})
string(REGEX MATCH "([^=]+)=\"(.*)\"" _match ${_line})
if(NOT "${_match}" STREQUAL "")
set(ENV{${CMAKE_MATCH_1}} "${CMAKE_MATCH_2}")
endif()
endforeach()
unset(_contents)
unset(_line)
unset(_match)
endif()
project(hello-world-2 CXX)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} CONAN_PKG::gtest) Maybe some kind of "workaround" or "hack", but should work, what's your opinion? |
There is one problem to solve, I have to simulate the "alternative value" feature with this variable settings:
|
I just used CMake in my terminal to build the ndk example on CLI.
|
I just think, if you need additional libraries then the build requirements, the conanfile.txt approach will not work The problem is pretty much related to that issue conan-io/conan#7892, that is , info about build requirements are not available out side of conan, and conan should export them so they are. I made my own tools for that , utilising |
I have a working example now, which works with both "old" single profile/android_ndk_installer and new "new" dual profile/android-ndk recipes. I continued the idea above, to put everything into
After that, normal CMake stuff works as expected. I tested with CLion too, for both Linux and Android builds. I have now to read and try all your other suggestions, maybe I can clean up my idea. But is is now as I like: have everything running from cmake, from command line or CLion. No extra steps required. https://github.com/andioz/conan-cmake-multiplatform/tree/main/hello-world-2 Here is the CMakeLists.txt: cmake_minimum_required(VERSION 3.16)
# Find conan executable
find_program (
CONAN_EXECUTABLE conan
REQUIRED
PATHS "${CMAKE_SOURCE_DIR}/venv/bin"
PATHS "${CMAKE_SOURCE_DIR}/../venv/bin"
PATHS "${CMAKE_SOURCE_DIR}/../../venv/bin"
)
if("${CONAN_EXECUTABLE}" STREQUAL "CONAN_EXECUTABLE-NOTFOUND") # TODO shorter
message(FATAL_ERROR "Cannot find conan executable")
endif()
# Run "conan install"
set(_options "${CMAKE_SOURCE_DIR}" --install-folder "${CMAKE_BINARY_DIR}" --build outdated)
if(NOT "${CONAN_PROFILE}" STREQUAL "")
list(APPEND _options --profile ${CONAN_PROFILE})
endif()
if(NOT "${CONAN_BUILD_PROFILE}" STREQUAL "")
list(APPEND _options --profile:build ${CONAN_BUILD_PROFILE})
endif()
if(NOT "${CONAN_HOST_PROFILE}" STREQUAL "")
list(APPEND _options --profile:host ${CONAN_HOST_PROFILE})
endif()
execute_process(COMMAND ${CONAN_EXECUTABLE} install ${_options})
unset(_options)
# Set conan environment variables
if(EXISTS "${CMAKE_BINARY_DIR}/environment.sh.env")
FILE(READ "${CMAKE_BINARY_DIR}/environment.sh.env" _contents)
STRING(REGEX REPLACE "\n" ";" _contents "${_contents}")
foreach(_line ${_contents})
string(REGEX MATCH "([^=]+)=(.*)" _match ${_line})
if(NOT "${_match}" STREQUAL "")
set(_key "${CMAKE_MATCH_1}")
set(_value "${CMAKE_MATCH_2}")
# TODO need improvement to be more general
string(REGEX MATCHALL "^(.*)\\$\\{([^+]+)\\+([^$]*)\\$([^}]+)\\}$" _match ${_value})
if(NOT "${_match}" STREQUAL "")
set(_prefix "${CMAKE_MATCH_1}")
set(_left "${CMAKE_MATCH_2}")
set(_separator "${CMAKE_MATCH_3}")
set(_right "${CMAKE_MATCH_4}")
if("${_left}" STREQUAL "${_right}")
if("$ENV{${_left}}" STREQUAL "")
set(ENV{${_key}} "${_prefix}")
else()
set(ENV{${_key}} "${_prefix}${_separator}$ENV{${_right}}")
endif()
else()
message(FATAL_ERROR "Unhandled advanced setting for [${_match}]")
endif()
else()
set(ENV{${_key}} "${_value}")
endif()
# message(STATUS "*** ENV{${_key}} = [$ENV{${_key}}]")
endif()
endforeach()
unset(_contents)
unset(_line)
unset(_match)
unset(_key)
unset(_value)
unset(_prefix)
unset(_left)
unset(_separator)
unset(_right)
endif()
project(hello-world-2 CXX)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} CONAN_PKG::gtest) |
actually needed is something like that (details like path may vary)
and conan should be able to generate exactly that for the user |
Hi,
Im breaking my head now about how can i build something for Android since package https://github.com/bincrafters/conan-android_ndk_installer is obsolete.
Now i see a android-ndk recipe.
But how can i use it to build my packages?
There isn't any tutorial or something like this.
Can anyone help me?
Today im using profile:
https://github.com/ezored/ezored/blob/master/files/targets/android_aar/conan/profile/ezored_android_aar_profile
And on each target i pass arch, api_level and others:
https://github.com/ezored/ezored/blob/master/files/targets/android_aar/verbs/prepare.py#L53-L60
How i can pass it to recipe now?
Thanks.
The text was updated successfully, but these errors were encountered: