Setting ABI Target with Android CMake Builds - android

Using Application.mk, it looks like the ABI targets can be selected with the option APP_ABI:
APP_ABI := armeabi-v7a,arm64-v8a,x86,x86_64,mips
I can't seem to find any such options for CMake builds. The file CMakeLists.txt seems to be called with the ANDROID_ABI option passed in, but I can't find any corresponding configuration options in the gradle plugin.

You should use abiFilters:
android {
defaultConfig {
ndk {
abiFilters "x86"
}
}
}

Related

Android Studio 4.2.2 Native dependencies in AARs buildFeatures.prefab=true - Missing argument "PACKAGE_PATH"

Android Studio 4.2.2
Adding Build Feature Prefab for Prefab AAR Shared Library Android.mk/CMake Build:
https://developer.android.com/studio/build/native-dependencies?buildsystem=cmake&agpversion=4.1
Following Documentation perfectly for Android Studio 4.1+ and using Build Features
android {
buildToolsVersion '30.0.3'
ndkVersion '22.1.7171670'
buildFeatures {
prefab true
}
}
Problem:
Error while executing java process with main class com.google.prefab.cli.AppKt with arguments {--build-system ndk-build --platform android --abi armeabi-v7a --os-version 21 --stl c++_static --ndk-version 22 --output /Users/~var/Documents/project/.cxx/ndkBuild/debug/prefab/armeabi-v7a/prefab}
Usage: prefab [OPTIONS] [PACKAGE_PATH]...
Error: Missing argument "PACKAGE_PATH".
Error: Missing argument "PACKAGE_PATH".
When setting this:
buildFeatures {
prefab true
}
You must then Link to an existing prefab published module after setting that command else this error occurs
If you are not using prefabs at all Remove this line and it will compile
OR either can Link to an Active Prefab via the Android.mk or CMakeLists
Android.mk
include $(CLEAR_VARS)
$(call import-add-path, $(LOCAL_PATH)/modulePath)
$(call import-add-path, /../../anotherModulePath)
# If you don't need your project to build with NDKs older than r21, you can omit
# this block.
ifneq ($(call ndk-major-at-least,21),true)
$(call import-add-path,$(NDK_GRADLE_INJECTED_IMPORT_PATH))
Endif
# the linked prefab published module project location
$(call import-module, openFrameworksAndroid)
If you need to build your module library with type of Prefab set the gradle setting buildFeatures.prefabPublishing true and configure these extra commands
// Enable generation of Prefab packages and include them in the library's AAR.
buildFeatures {
prefabPublishing true
}
// Include the "mylibrary" module from the native build system in the AAR,
// and export the headers in src/main/cpp/include to its consumers
prefab {
openFrameworksAndroid {
//headers "src/main/cpp/include"
}
}
// Avoid packing the unnecessary libraries into final AAR. For details
// refer to https://issuetracker.google.com/issues/168777344#comment5
// Note that if your AAR also contains Java/Kotlin APIs, you should not
// exclude libraries that are used by those APIs.
packagingOptions {
exclude("**/libopenFrameworksAndroid.so")
exclude("**/classes.jar")
}
Example:
https://github.com/android/ndk-samples/tree/main/prefab
More Documentation:
https://developer.android.com/studio/build/native-dependencies?buildsystem=ndk-build&agpversion=4.1

MIPS deprecation warning

I have removed mips, but am still getting the warning
Error:(81) Android NDK: Application targets deprecated ABI(s): mips64
Error:(82) Android NDK: Support for these ABIs will be removed in a future NDK release.
Error:(82) Android NDK: Support for these ABIs will be removed in a future NDK release.
Error:(81) Android NDK: Application targets deprecated ABI(s): mips
Application.mk
APP_ABI := armeabi-v7a x86
APP_PLATFORM := android-10
UPDATES
build.gradle for ndk
sourceSets.main.jni.srcDirs = []
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
I fixed the problem by adding:
android {
.
.
versionName "1.0"
ndk {
abiFilters 'x86'
abiFilters 'armeabi-v7a'
moduleName "your-library-name"
}
to the app/build.gradle
It appears that Android Studio was not removing the previous cached NDK builds from the .externalNativeBuild directory from my project. After manually deleting them the process build correctly with no errors.
go to files and click invalidate cache/restart

adding "-O0" to cppFlags fails to disable clang compile optimization in android studio

I am building my app in debug mode, and I notice some errors saying "parent failed to evaluate: no location, value may have been optimized out". Therefore, I try to add "-O0" in my module build.gradle like this:
externalNativeBuild {
cmake {
cppFlags "-O0 -frtti -fexceptions -std=c++11 -DANDROID_ARM_NEON=TRUE -mfloat-abi=softfp "
abiFilters "armeabi-v7a"
}
}
But still, the same error shows up after adding "-O0". May I ask how to disable compiler optimization properly? My android Studio version is 2.3.3, my sdk tool version is 26.0.2 and my ndk version is 15.1.4
If you want to disable optimization for release build, you can force Debug for C/C++ only:
android {
defaultConfig {
externalNativeBuild {
cmake {
arguments '-DCMAKE_BUILD_TYPE:STRING=Debug'
You can override the build flags by adding the following to your CMakeLists.txt:
# Debug flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
# Release flags
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast")
To verify this has worked check build output in:
app/.externalNativeBuild/cmake/<buildconfig>/<architecture>/build.ninja
Look for a line starting with FLAGS. This doesn't actually replace the existing compiler flags it just appends your flags and these take precedence.
The default flags are inherited from $ANDROID_NDK/build/cmake/android.toolchain.cmake so you could edit that file directly, however, if you update your NDK these changes will be overwritten.

gradle + ndkbuild + android studio 2.2 how to set supported ABIs?

I want to use android studio integration with ndkbuild.
My "native" part of project build only for armeabi-v7a-hard and x86,
and all works fine if I just run ndk-build in jni directory.
I have proper lines in Application.mk:
APP_ABI := armeabi-v7a-hard x86
To integration project into android studio I added such lines into build.gradle:
externalNativeBuild {
ndkBuild {
path 'src/lib/jni/Android.mk'
}
}
But for some reason gradle build try build native code with APP_ABI=armeabi and failed, because of my code can only be build with armeabi-v7a-hard.
So how can I tell gradle to build my code only for armeabi-v7a-hard and x86,
or just not ignore APP_ABI line from Application.mk?
I try such variant:
defaultConfig {
ndk {
abiFilters 'x86', 'armeabi-v7a-hard'
}
}
but gradle failed with such message:
ABIs [armeabi-v7a-hard] are not available for platform and will be
excluded from building and packaging. Available ABIs are [armeabi,
armeabi-v7a, arm64-v8a, x86, x86_64, mips, mips64].
Note, that I use ndk 10, not last one (ndk 13), where there is armeabi-v7a-hard, and ndk.dir in local.properties to right value.
Link provided by #Titan is all you need to set the ABI.
The reason why it might not be working is because armeabi-v7a-hard is deprecated in 2015, so targeting it is resulting in this issue. You should target armeabi-v7a as per this this post

How to properly use NDK-Build in Android Studio 2.2 Preview 1

Android Studio 2.2 Preview 1 has a new external ndk build feature, but from app/build.gradle snippet shown in official blog post it's not clear at all how to set additional ndk build parameters which Application.mk file usually contains
I'm able to set Android.mk ndk build file via externalNativeBuild, but how could I set the required Application.mk variables?
My Application.mk contains:
NDK_TOOLCHAIN_VERSION := clang
APP_PLATFORM := android-16
APP_ABI := armeabi
APP_STL := c++_static
APP_CPPFLAGS += -std=c++11
Android Studio 2.2 Preview 3 with updated gradle plugin added support for additional arguments. You can set Application.mk and additional configuration like this:
defaultConfig {
ndkBuild {
arguments "NDK_APPLICATION_MK:=Application.mk"
cFlags "-DTEST_C_FLAG1" "-DTEST_C_FLAG2"
cppFlags "-DTEST_CPP_FLAG2" "-DTEST_CPP_FLAG2"
abiFilters "armeabi-v7a", "armeabi"
}
}
If possible I would recommend migrating to CMake build system, because of better C++ code editor and debugging integration in Android Studio. You will find more info on gradle plugin configuration here:
https://sites.google.com/a/android.com/tools/tech-docs/external-c-builds.
Edit:
From Android Studio 2.2 Preview 5 you must wrap cmake and ndkBuild groups under externalNativeBuild group:
defaultConfig {
externalNativeBuild {
ndkBuild {
targets "target1", "target2"
arguments "NDK_APPLICATION_MK:=Application.mk"
cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
abiFilters "armeabi-v7a", "armeabi"
}
}
}
Edit 2: It seems that wrapping ndkBuild under externalNativeBuild group does not work because of a bug in build tools.
add-native-code
android {
...
defaultConfig {...}
buildTypes {...}
// Encapsulates your external native build configurations.
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}
}
Note: If you want to link Gradle to an existing ndk-build project, use the ndkBuild {} block instead of cmake {}, and provide a relative path to your Android.mk file. Gradle also includes the Application.mk file if it is located in the same directory as your Android.mk file.

Categories

Resources