How can I use CMake -C option in Android Studio (gradle)? - android-studio

I have to populate the CMake variable cache from a file.
I added an argument to defaultConfig.externalNativeBuild.cmake.arguments (-C options.cmake).
However, I get this:
17:05:13.520 [LIFECYCLE] [org.gradle.api.Project] loading initial
cache file options.cmake 17:05:13.521 [LIFECYCLE]
[org.gradle.api.Project] CMake Error: Error processing file:
options.cmake
The file is in the same directory as the build.gradle.
I tried providing the absolute path (with the help of projectDir variable), but it didn't change anything.
Simple reproduction
Create an empty Android app in Android Studio.
In MyApplication/app add a simple CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0)
project(hello)
In the same folder add a simple options.cmake file:
set(foo "bar")
Note that this is OK for CMake:
cd MyApplication/app && mkdir bld && cd bld
cmake -C ../options.cmake ..
Now, import the CMakeLists.txt with right-click on the project and 'Link C++ Project with Gradle'
Your build.gradle should now have this:
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
What I tried doing now was adding the following in defaultConfig:
externalNativeBuild {
cmake {
arguments "-C ${projectDir}/options.cmake"
}
}
But this doesn't work. Any other ideas for populating CMake cache?

It turned out to be quite easy:
Don't put space after "-C"

Related

CMake (CLion / Android Studio) Sub-builds fail with ninja

I have multiple dependencies for a piece of software. To make everything easier, we made CMake build files for these dependencies. For example, we have lo libfoo, so we make a folder called "make", and in there put the file "buildLibFoo.cmake", which looks like this:
include(ExternalProject)
externalproject_add(LIBFOO
URL https://github.com/lib/foo/archive/refs/tags/v1.6.0.tar.gz
URL_MD5 58e4e09322f2d1e417469eb0987f0531b
BUILD_IN_SOURCE 0
PREFIX ${CMAKE_BINARY_DIR}/deps/libfoo
SOURCE_DIR ${CMAKE_BINARY_DIR}/deps/libfoo/src/libfoo
CONFIGURE_COMMAND
cd <SOURCE_DIR> &&
autoreconf -i &&
./configure
BUILD_COMMAND
cd <SOURCE_DIR> &&
make -j$(nproc)
INSTALL_COMMAND ""
)
set(LIBFOO_INCLUDE_PATH ${CMAKE_BINARY_DIR}/deps/libfoo/src/libfoo/utils)
set(LIBFOO_LIB_PATH ${CMAKE_BINARY_DIR}/deps/libfoo/src/libfoo/utils/.libs)
In my primary CMakeFiles.cmake I add the following:
cmake_minimum_required(VERSION 3.18.1)
project("testproject")
set (COMPILE_FLAGS "-O2")
## Need LIBFOO
if(NOT (${LIBFOO_INCLUDE_PATH} AND ${LIBFOO_LIB_PATH}))
include(buildTSS.cmake)
include_directories(${LIBFOO_INCLUDE_PATH})
link_directories(${LIBFOO_LIB_PATH})
list(APPEND DEPENDENCY_LIST "LIBFOO")
endif()
add_library(
testproject
SHARED
testproject.cpp)
target_link_libraries(testproject libfoo)
add_dependencies(testproject ${DEPENDENCY_LIST})
While the compiles fine if I run cmake CMakeLists.txt and make, it fails to do so if I use Android Studio (with native code) or CLion. Here is an example in Android Studio.
Build command failed.
Error while executing process /home/derp/Android/Sdk/cmake/3.18.1/bin/ninja with arguments {-C /home/derp/AndroidStudioProjects/testproject/app/.cxx/Debug/565m494g/arm64-v8a testproject}
ninja: Entering directory `/home/derp/AndroidStudioProjects/testproject/app/.cxx/Debug/565m494g/arm64-v8a'
ninja: error: build.ninja:178: bad $-escape (literal $ must be written as $$)
Any ideas how to make it work through the IDE?
I ran into this same error for an existing project I was trying to load with CLion that also built fine using cmake from the command line.
In my case the solution was to override the default setting CLion uses for the generator tool (ninja) and set it to the "Let CMake decide"

Unknown host CPU architecture: arm64 , Android NDK SiliconM1 Apple MacBook Pro

I've got a project that is working fine in windows os but when I switched my laptop and opened an existing project in MacBook Pro M1. I'm unable to run an existing android project in MacBook pro M1. first I was getting
Execution failed for task ':app:kaptDevDebugKotlin'. > A failure
occurred while executing
org.jetbrains.kotlin.gradle.internal.KaptExecution >
java.lang.reflect.InvocationTargetException (no error message)
this error was due to the Room database I applied a fix that was adding below library before Room database and also changed my JDK location from file structure from JRE to JDK.
kapt "org.xerial:sqlite-jdbc:3.34.0"
//Room components
kapt "org.xerial:sqlite-jdbc:3.34.0"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
after that now I'm getting an issue which is Unknown host CPU architecture: arm64
there is an SDK in my project that is using this below line.
android {
externalNativeBuild {
ndkBuild {
path 'Android.mk'
}
}
ndkVersion '21.4.7075529'
}
App Gradle
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.18.1"
//version "3.10.2"
}
}
[CXX1405] error when building with ndkBuild using
/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/Android.mk:
Build command failed. Error while executing process
/Users/mac/Library/Android/sdk/ndk/21.4.7075529/ndk-build with
arguments {NDK_PROJECT_PATH=null
APP_BUILD_SCRIPT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/Android.mk
APP_ABI=arm64-v8a NDK_ALL_ABIS=arm64-v8a NDK_DEBUG=1
APP_PLATFORM=android-21
NDK_OUT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/build/intermediates/cxx/Debug/4k4s2lc6/obj
NDK_LIBS_OUT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/build/intermediates/cxx/Debug/4k4s2lc6/lib
APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n} ERROR:
Unknown host CPU architecture: arm64
which is causing this issue and whenever I comment on this line
path 'Android.mk'
it starts working fine, is there any way around which will help me run this project with this piece of code without getting this NDK issue?
Update - It seems that Room got fixed in the latest updates, Therefore you may consider updating Room to the latest version (2.3.0-alpha01 / 2.4.0-alpha03 or above)
use ndkVersion "24.0.8215888" update ndk to this version and no need to edit any script :)
GitHub Issue Tracker
solved this issue.
Finder -> Go To Folder(/Users/mac/Library/Android/sdk/ndk/21.4.7075529)
-> now edit ndk-build open it in text editor and paste below code script and re-run your project.
from
#!/bin/sh
DIR="$(cd "$(dirname "$0")" && pwd)"
$DIR/build/ndk-build "$#"
to
#!/bin/sh
DIR="$(cd "$(dirname "$0")" && pwd)"
arch -x86_64 /bin/bash $DIR/build/ndk-build "$#"
Reference Link
To solve this on a Apple Silicon M1 I found three options
A
Use NDK 24
android {
ndkVersion "24.0.8215888"
...
}
You can install it with
echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install 'ndk;24.0.8215888'
or
echo "y" | sudo ${ANDROID_HOME}/sdk/cmdline-tools/latest/bin/sdkmanager --install 'ndk;24.0.8215888'
Depending what where sdkmanager is located
B
Change your ndk-build to use Rosetta x86. Search for your installed ndk with
find ~ -name ndk-build 2>/dev/null
eg
vi ~/Library/Android/sdk/ndk/22.1.7171670/ndk-build
and change
DIR="$(cd "$(dirname "$0")" && pwd)"
$DIR/build/ndk-build "$#"
to
DIR="$(cd "$(dirname "$0")" && pwd)"
arch -x86_64 /bin/bash $DIR/build/ndk-build "$#"
C
convert your ndk-build into a cmake build

Pointing Android Studio to specific cmake version

I use Android Studio 4.0.1
In my project, my local.properties has the following properties:
sdk.dir=C\:\\Users\\xxx\\AppData\\Local\\Android\\Sdk
ndk.dir=c\:\\yyy\\tools\\android-ndk-r19c
cmake.dir=c\:\\zzz\\cmake-3.17.0-win64-x64
Despite this, I get a compilation error when I Build -> Rebuild project or Clean project because my CMakeLists.txt begins with cmake_minimum_required(VERSION 3.14.3) and Android Studio uses the CMake included in the Android Studio folder instead of the one I point to in cmake.dir:
CMake Error at CMakeLists.txt:5 (cmake_minimum_required):
CMake 3.14.3 or higher is required. You are running version 3.10.2
-- Configuring incomplete, errors occurred!
See also "C:/Users/xxx/AndroidStudioProjects/E2e/app/.cxx/cmake/debug/x86/CMakeFiles/CMakeOutput.log".
FAILED: build.ninja
ninja: error: rebuilding 'build.ninja': subcommand failed
C:\Users\xxx\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe -HC:\Users\xxx\AndroidStudioProjects\E2e\app\src\main\cpp -BC:\Users\xxx\AndroidStudioProjects\E2e\app\.cxx\cmake\debug\x86
What am I missing for AS to pick up the cmake 3.17 I already installed when I select Rebuild Project or Clean Project?
Notes:
The directory I point to with cmake.dir is valid and being read by AS. If I introduce a typo in the path, AS complains about it.
This problem only occurs when I do Build --> Rebuild project or Clean project. If instead I do Build --> Make project, then AS will pick up the cmake version that I point to in local.properties
Clean Project worked again after deleting .cxx folder
I'm not sure about Android Studio, but you could try editing the project's build.gradle :
android {
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
version '3.24.1'
}
}
}
.... and CMakeLists.txt :
cmake_minimum_required(VERSION 3.24.1)

React native android build fails with NDK error [duplicate]

Can someone explain why I get this errors please?
Build command failed.
Error while executing process C:\Users\Kevin\Desktop\Android\Sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\Kevin\Desktop\Mygame\proj.android-studio\app\jni\Android.mk NDK_APPLICATION_MK=C:\Users\Kevin\Desktop\Mygame\proj.android-studio\app\jni\Application.mk APP_ABI=armeabi NDK_ALL_ABIS=armeabi NDK_DEBUG=1 APP_PLATFORM=android-14 NDK_OUT=C:/Users/Kevin/Desktop/Mygame/proj.android-studio/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=C:\Users\Kevin\Desktop\Mygame\proj.android-studio\app\build\intermediates\ndkBuild\debug\lib NDK_TOOLCHAIN_VERSION=4.9 APP_PLATFORM=android-10 NDK_MODULE_PATH=C:/Users/Kevin/Desktop/Mygame/cocos2d;C:/Users/Kevin/Desktop/Mygame/cocos2d/cocos;C:/Users/Kevin/Desktop/Mygame/cocos2d/external -j4 NDK_DEBUG=1 APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
Android NDK: INTERNAL ERROR: The armeabi ABI should have exactly one `architecture definitions. Found: ''`
process_begin: CreateProcess(NULL, "", ...) failed.
*** Android NDK: Aborting... . Stop.
Build command failed.
Error while executing process C:\Users\Kevin\Desktop\Android\Sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\Kevin\Desktop\Mygame\proj.android-studio\app\jni\Android.mk NDK_APPLICATION_MK=C:\Users\Kevin\Desktop\Mygame\proj.android-studio\app\jni\Application.mk APP_ABI=armeabi NDK_ALL_ABIS=armeabi NDK_DEBUG=0 APP_PLATFORM=android-14 NDK_OUT=C:/Users/Kevin/Desktop/Mygame/proj.android-studio/app/build/intermediates/ndkBuild/release/obj NDK_LIBS_OUT=C:\Users\Kevin\Desktop\Mygame\proj.android-studio\app\build\intermediates\ndkBuild\release\lib NDK_TOOLCHAIN_VERSION=4.9 APP_PLATFORM=android-10 NDK_MODULE_PATH=C:/Users/Kevin/Desktop/Mygame/cocos2d;C:/Users/Kevin/Desktop/Mygame/cocos2d/cocos;C:/Users/Kevin/Desktop/Mygame/cocos2d/external -j4 NDK_DEBUG=0 APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
Android NDK: INTERNAL ERROR: The armeabi ABI should have exactly one architecture definitions. Found: ''
process_begin: CreateProcess(NULL, "", ...) failed.
*** Android NDK: Aborting... . Stop.
I leave here a screenshot of my android studio if can be helpful
Most likely, you have NDK r17 installed, which does not support armeabi anymore. Your gradle plugin is not aware of this recent change. You must upgrade: in build.gradle, you should have
buildscript { dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
} }
and in gradle/wrapper/gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
But even after upgrade, your build.gradle most likely lacks the abiFilters statement, and therefore your project build is slower and APK larger than necessary.
You probably only need on ABI in your APK,
android { defaultConfig { ndk {
abiFilters 'armeabi-v7a'
} } }
I got this error recently - the cause was a mystery and still is. I reinstalled everything, but I could not get my project to Clean.
In the end I manually deleted the app/build and app/.externalNativeBuild folders, and the project rebuilt fine, and I was then able to run Clean without errors again.

Android NDK, CMake with other libraries

So I am trying to build and test out a CMake with the Android NDK on Android Studio. I can get my library to compile, but it doesn't seem to want to pull any third-party dependencies over. I've been reading through the toolchain and looking for better documentations, with no luck. Can someone tell me if I am missing?
cmake_minimum_required(VERSION 3.4.1)
set(SFML_PATH ${ANDROID_NDK}/sources/sfml)
set(SFML_LIB_PATH ${SFML_PATH}/lib/${ANDROID_NDK_ABI_NAME})
set(SFML_LIB_SYSTEM ${SFML_LIB_PATH}/libsfml-system.so)
set(SFML_LIB_AUDIO ${SFML_LIB_PATH}/libsfml-audio.so)
set(SFML_LIB_GRAPHICS ${SFML_LIB_PATH}/libsfml-graphics.so)
set(SFML_LIB_NETWORK ${SFML_LIB_PATH}/libsfml-network.so)
set(SFML_LIB_WINDOW ${SFML_LIB_PATH}/libsfml-window.so)
set(SFML_LIB_ACTIVITY ${SFML_LIB_PATH}/libsfml-activity.so)
set(SFML_LIB_MAIN ${SFML_LIB_PATH}/libsfml-main.a)
set(SFML_LIBS ${SFML_LIB_SYSTEM} ${SFML_LIB_GRAPHICS} ${SFML_LIB_AUDIO} ${SFML_LIB_WINDOW} ${SFML_LIB_ACTIVITY})
include_directories(${SFML_PATH}/include)
link_directories(${SFML_LIB_PATH})
add_library(native-lib SHARED
src/main/cpp/native-lib.cpp)
target_link_libraries(native-lib log ${SFML_LIBS})
#file(COPY ${SFML_LIBS} DESTINATION ${__android_install_path})
FOREACH(SFML_LIB ${SFML_LIB})
execute_process( COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${SFML_LIB}" "${LIBRARY_OUTPUT_PATH}/${SFML_LIB}" RESULT_VARIABLE __fileCopyProcess )
MESSAGE("Lib: ${SFML_LIB}")
ENDFOREACH(SFML_LIB)
Above is my CMakeLists.txt. I have done a little hacking to get it to compile with SFML with the paths, as I have not found good documentation with CMake and Android yet.
May you add more info for:
"but it doesn't seem to want to pull any third-party dependencies over."?
this one:
https://github.com/googlesamples/android-ndk/tree/master/hello-libs
has static and shared 3rd party libs, you may try it.
For the shared dependent lib, you will need to pack them into APK, that is done inside gradle, cmake will not do it.
The above example shows that, basically they need to be copied into your app/src/main/jniLibs too so they will be packed into apk, and pushed to your android phone/tablet. At runtime they could be loaded.
I have tried to put a group of libraries into one directory, and use
link_directories(...)
then just put the lib names directly into
target_link_libraries(...)
also works. Make sure you have the right libs for the ABIs you intend to support for your app [looks like you are just building for one ABI].
The process could be little long it will depend on your android skills.
An example could be similar to this process:
Crosscompile sfml.
Create your jni bridge
Generate with cmake the project and compile
Copy your files to android studio. create java loading library code.
I guess that you have crosscompiled sfml and you know how works crosscompiling process, if I am wrong check these link below:
Tutorial:
https://github.com/SFML/SFML/wiki/Tutorial:-Building-SFML-for-Android
Source code:
https://github.com/SFML/SFML
Toolchain:
https://github.com/SFML/SFML/blob/master/cmake/toolchains/android.toolchain.cmake
Changes on your cmake:
add this file
FIND_PACKAGE(SFML required)
In cmake put your SFML build directory and cmake will fills your VARIABLES
automatically for instance this variables:
set(SFML_PATH ${ANDROID_NDK}/sources/sfml)
set(SFML_LIB_PATH ${SFML_PATH}/lib/${ANDROID_NDK_ABI_NAME})
set(SFML_LIB_SYSTEM ${SFML_LIB_PATH}/libsfml-system.so)
set(SFML_LIB_AUDIO ${SFML_LIB_PATH}/libsfml-audio.so)
set(SFML_LIB_GRAPHICS ${SFML_LIB_PATH}/libsfml-graphics.so)
set(SFML_LIB_NETWORK ${SFML_LIB_PATH}/libsfml-network.so)
set(SFML_LIB_WINDOW ${SFML_LIB_PATH}/libsfml-window.so)
set(SFML_LIB_ACTIVITY ${SFML_LIB_PATH}/libsfml-activity.so)
set(SFML_LIB_MAIN ${SFML_LIB_PATH}/libsfml-main.a)
There are two ways to make android studio native apps:
Easy way:
Create JNI bridge:
Crosscompile your cmake script and copy your lib to app/src/main/jniLibs
add library in execution time
code:
try
{
Log.v(LOG_TAG, "adding your library");
System.loadLibrary(your_library);
}
catch(UnsatisfiedLinkError e)
{
Log.e(LOG_TAG,e.getMessage());
}
More complete way (it allows to debug library)
Create your ndk module in gradle
example
android.ndk {
moduleName = "your_library"
cppFlags.add("-fexceptions")
//cppFlags.add("-std=c++11")
//cFlags.add("-fopenmp")
cppFlags.add("-I" + file("src/main/jni").absolutePath)
stl = "gnustl_shared" // Which STL library to use: gnustl or stlport
ldLibs.addAll(["android", "EGL", "GLESv2", "dl", "log", "z"])
String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi/"
ldLibs.add(libsDir + "your_native_lib.so")
}

Resources