Android NDK: How to build for ARM64-v8a with minimumSdkVersion = 19 - android

I have an Android Library project which has a part in C/C++ via Android NDK. I want to build my native code for all architectures. If i set minSdkVersionto 21 it all works fine. but i need to have lower minSdkVersion(SDK 19) if i set minSdkVersion to 19 the ARM64-v8a version is not built. I know that there are no ARM64-v8a devices running SDK 19. How can i achieve to have all platforms built in one APK and have minSdkVersion 19?

Actually, there is typo in "minimumSdkVersion". The right attribute is "minSdkVersion".
However, arm64-v8a library should be successfully built for API 19, because NDK build scripts will automatically take right set of platform headers and libraries.
If you are using Gradle build scripts try to set it as one of target ABIs:
ndk {
abiFilters 'arm64-v8a', 'armeabi-v7a'
}
Then, explicitly set target platform for native build. If you use CMake, then in gradle script set:
externalNativeBuild {
cmake {
arguments '-DANDROID_PLATFORM=android-19'
}
}
If you use ndk-build, then in your Application.mk set:
APP_PLATFORM := android-19
I hope that helps. Please, write details if it will not build.

Related

Android Studio CMake/Ninja Not Used for Building an NDK project

I have the following CMAKE & Ninja installed through Android Studio's SDK Tools:
~/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja --version
1.8.2
I run into "Error Configuring" while trying to build my project. Here is the build output:
Executable : /Users/ssk/Library/Android/sdk/cmake/3.10.2.4988404/bin/cmake
arguments :
-H/Users/ssk/MyProject
-B/Users/ssk/MyProject/.externalNativeBuild/cmake/debug/armeabi-v7a
-DANDROID_ABI=armeabi-v7a
-DANDROID_PLATFORM=android-16
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/ssk/MyProject/build/intermediates/cmake/debug/obj/armeabi-v7a
-DCMAKE_BUILD_TYPE=Debug
-DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle
-DCMAKE_CXX_FLAGS=-std=c++11
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a
-DCMAKE_SYSTEM_VERSION=16
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_ANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle
-DCMAKE_TOOLCHAIN_FILE=/Users/ssk/Library/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake
-G Ninja
-DANDROID_STL=gnustl_statics
-DANDROID_CPP_FEATURES=rtti exception
-DANDROID_TOOLCHAIN=gcc
-DANDROID_NDK=/Users/ssk/android-ndk-r17c/
jvmArgs :
It's missing:
-DCMAKE_MAKE_PROGRAM=/Users/ssk/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja
Error:
CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool
Only if I switch to CMake version say 3.6.3155560 it works. Otherwise, I have to install ninja from brew or macports.
Here is the snippet from my build.gradle:
externalNativeBuild {
cmake {
// Linker flags and Visibility options keeps the size of the library small
cppFlags "-std=c++11"
arguments "-DANDROID_STL=gnustl_static",
"-DANDROID_CPP_FEATURES=rtti exceptions",
"-DANDROID_TOOLCHAIN=gcc"
}
}
How to fix it?
Install/Update CMake From Android Studio SDK Manager
Check your CMake from sdk root directory if ninja exists.
Below is not good.
cmake {
cppFlags "-std=c++11"
arguments "-DANDROID_ABI=armeabi-v7a",
"-DANDROID_PLATFORM=android-16",
"-DANDROID_STL=gnustl_static",
"-DANDROID_CPP_FEATURES=rtti exceptions",
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=libs"
}
Because, ANDROID_PLATFORM should be automatically decided by Android external native build system according to minSdkVersion, see below official document from how ANDROID_PLATFORM works:
Instead of changing this flag directly, you should set the minSdkVersion property in the defaultConfig or productFlavors blocks of your module-level build.gradle file. This makes sure your library is used only by apps installed on devices running an adequate version of Android. The CMake toolchain then chooses the best platform version for the ABI you're building using the following logic:
If there exists a platform version for the ABI equal to minSdkVersion, CMake uses that version.
Otherwise,
if there exists platform versions lower than minSdkVersion for the ABI, CMake uses the highest of those platform versions. This is a reasonable choice because a missing platform version typically means that there were no changes to the native platform APIs since the previous available version.
Otherwise, CMake uses the next available platform version higher than minSdkVersion.
And, -DANDROID_ABI=armeabi-v7a is not good as well. You should not define this parameter here. CMake will iterate all your ABIs according to your abiFilters automatically. If you just want to build armeabi-v7a, you can specify this using abiFilter, e.g.
externalNativeBuild {
cmake {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
Also, rtti and exceptions are cppFlags, below should be the proper way to set these two flags.
cppFlags "-std=c++11 -frtti -fexceptions"
Ensure that your have properly configured ANDROID_NDK path, because according to your question, you have TWO version of NDK set, one is -DANDROID_NDK=/Users/ssk/android-ndk-r17c/, the other one is -DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle. Config NDK path from local.properties:
ndk.dir=/Users/ssk/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/ssk/Library/Android/sdk
what is the fix for -GAndroid Gradle - Ninja?
Add below arguments to the cmake config:
externalNativeBuild {
cmake {
...
version "3.10.2"
arguments "-GAndroid Gradle - Ninja"
}
}

Expected caller to ensure valid ABI: MIPS

Getting an error when trying to fix error -
No toolchains found in the NDK toolchains folder for ABI with prefix mips64el-linux-android
Do not want to upgrade as afraid it might break my application and need to use NDK to work with C++.
After following steps for workaround to fix the error -
Error: No toolchains found in the NDK toolchains folder for ABI with prefix: llvm
New error:
Expected caller to ensure valid ABI: MIPS
Any help on how to fix the issue.
You should specify an ABI filter.
You haven't mentioned how you are building. If you're using Gradle, then you put something like this in the defaultConfig block in your build.gradle:
ndk {
abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
}
If you're invoking ndk-build directly, then you put this on the ndk-build command line:
APP_ABI=armeabi-v7a arm64-v8a x86 x86_64
Or inside your Application.mk:
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
The ABI filter I showed is just an example. It's up to you to decide which ones you want to build for. arm64-v8a and armeabi-v7a are by far the most common ones among Android devices.mips, mips64 and armeabi are no longer supported by the NDK.
From your TOP-LEVEL build.gradle, change your classpath for android gradle plugin to 3.2.1 or higher.
classpath 'com.android.tools.build:gradle:3.2.1'
Or for other options, please check here: Three options for solving this kind of issue

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

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 do I tell Android Studio to use GCC 4.9 with CMake?

I'm using NDK r12 with Android Studio 2.2. I need CMake to use GCC 4.9 instead of Clang to build our code base, however even if I provide the following it still uses clang:
android {
compileSdkVersion 17
buildToolsVersion "25.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 17
externalNativeBuild {
cmake {
arguments '-DBUILD_TESTING=OFF -DANDROID_TOOLCHAIN=gcc-4.9'
cppFlags "-std=c++14 -fexceptions -frtti"
}
}
}
}
I've tried -DANDROID_TOOLCHAIN=gcc as well but this doesn't work either. How can I get CMake to use GCC ARM toolchain?
Split your arguments string into one string per argument:
arguments '-DBUILD_TESTING=OFF','-DANDROID_TOOLCHAIN=gcc'
I don't know if it's possible to explicitly specify version 4.9 of GCC ("gcc-4.9" didn't work). However, that's redundant anyway since GCC 4.8 was removed in NDK r11, so GCC 4.9 is now the only version of GCC included in the NDK, and ANDROID_TOOLCHAIN=gcc therefore implicitly means GCC 4.9.
It looks that it's impossible now and GCC toolchaing is dropped.
I get this error when trying to set ANDROID_TOOLCHAIN=gcc:
CMake Error at D:/Android/ndk-bundle/build/cmake/android.toolchain.cmake:169 (message):
GCC is no longer supported. See
https://android.googlesource.com/platform/ndk/+/master/docs/ClangMigration.md

Categories

Resources