Build tensorflow lite on ndk 22 > - android

I'm trying to make a project where I use TFLite in my native c++ code through NDK inside my android application. So far I've build and tested TFLite on my pure c++ linux project. Now I want to build and run it on my Android project which uses a somewhat similar code base as my pure linux project, with an android UI on top. It should be on an ARM64 architecture
I was following the official docs (the non-docker part) which asked me to run ./configure on my tensorflow file which I got from extracting the 2.11 release from it's github repo.
My first question is how do I properly set this up and compile everything? Currently I'm following this question which seems like a decent option but I would rather follow an official set of instructions (although as long as it works I'm fine).
My real question is this: Do I have to use NDK < 21? My project currently (because of other dependencies) only supports NDK > 22 which as you can tell creates a problem for me. Is there a way to build TFLite for NDK versions above 22?
When I run ./configure and say yes to configuring ./WORKSPACE I get this output:
WARNING: The NDK version in /home/user/Android/Sdk/ndk/25.1.8937393 is 25, which is not supported by Bazel (officially supported versions: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]). Please use another version. Compiling Android targets may result in confusing errors.
Traceback (most recent call last):
File "/home/user/Desktop/Android_build/tensorflow-2.11.0/./configure.py", line 1362, in <module>
main()
File "/home/user/Desktop/Android_build/tensorflow-2.11.0/./configure.py", line 1335, in main
create_android_ndk_rule(environ_cp)
File "/home/user/Desktop/Android_build/tensorflow-2.11.0/./configure.py", line 653, in create_android_ndk_rule
get_ndk_api_level(environ_cp, android_ndk_home_path))
File "/home/user/Desktop/Android_build/tensorflow-2.11.0/./configure.py", line 746, in get_ndk_api_level
api_levels = sorted(os.listdir(platforms))
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/Android/Sdk/ndk/25.1.8937393/platforms'
If possible, I just want to get the android compatible .so files for clang++ so I can just add that to my pure C projects cmake file and ndk can run it.
I've installed NDK though android studios interface and I'm using debian 11.
Note: Since I'm not inside a project, there is no local.properties file for me to edit and add the path. I'm trying to configure and run bazel inside the source directory I got from tensorflows github page.

Related

OpenCV for Android via Visual Studio to Unity

I tried to compile a .so library using Visual Studio 2019 along with OpenCV Android in order to use this library in Unity.
There are some answers on how to configure Visual Studio to use OpenCV Android (here or here) but none of these work for me. Below you can see my configurations.
Visual Studio 2019 (running on Windows 10)
android-ndk-r21e // also tried with android-ndk-r15c android-ndk-r16b and android-ndk-r17c
OpenCV Android 4.5.2 // also tried with OpenCV Android 4.0.0, 4.0.1 and 4.1.0
My settings in Visual Studio 2019 look as follows:
Configuration Properties
- General
Platform Toolset Clang 5.0 (also tried Clang 3.8 or GCC 4.9)
Configuration Type Dynamic Library (.so)
Target API Level Nougat 7.0 (android-24) (also tried different versions)
Use STL LLVM libc++ static library (c++_static) (also tried "GNU STL static library (gnustl_static)")
C/C++
- General
Additional Include Directories "Path to OpenCV_4_5_2_Android\sdk\native\jni\include"
Code Generation Enable C++ Exceptions "Yes(-fexceptions)"
Language C++17(-std=c++1z)
Precompiled Headers Not using Precompiled Headers
Linker
- General
Additional Library Directories Path to OpenCV_4_5_2_Android\sdk\native\libs\armeabi-v7a
- Input
Additional Dependencies Path to OpenCV_4_5_2_Android\sdk\native\libs\armeabi-v7a\libopencv_java4.so
My Source.cpp I try to compile is just a single function for testing purposes
#include <opencv2/core.hpp>
extern "C" float test(float a, float b)
{float c = a * b; return c;}
Which gives me the following errors:
E0035 #error directive: This constructor has not been ported to this platform
E0020 identifier "__fp16" is undefined
use of undeclared identifier 'ANDROID_LOG_INFO'
The ANDROID_LOG_INFO error can be fixed when I add #include "android/log.h" at the top of the file that throws this error. But the other two errors still remain.
I had the exact same issue as you (though I used c++ 11) with the exact same setup, and struggled for days. I believe the errors you're seeing (like me) are from arm_neon.h. Very oddly, I was able to just build (not run) the .so successfully, even with those errors (I say "errors" because if you look at arm_neon.h, others pop up), so try it. Maybe it's some kind of IntelliJ/Intellisense mistake more than anything else where it's catching false negatives from some other toolchain setup.
At the same time, I'm not 100% sure I was always able to build with that issue, so try these steps as well if you can't:
use OpenCV 4.0.1 with Android NDK 16rb. The NDK matters when it comes to OpenCV builds, and this is the only supposed match that I know of.
follow this tutorial from scratch: https://amin-ahmadi.com/2019/06/03/how-to-use-opencv-in-unity-for-android/
if the downloaded OpenCV android SDK is still giving trouble, build OpenCV from the source using his other tutorial here: https://amin-ahmadi.com/2019/02/03/how-to-build-opencv-4-x-for-native-android-development/
and then repeat step 2.
MAJOR EDIT:
OpenCV 4.5.2 needs to be treated differently because it no longer uses toolchains with gnu c++.
-When you build OpenCV from CMake, build with Android NDK 21e, and do not use the toolchain in OpenCV 4.5.2. Use the one inside the Android NDK's build folder (android-ndk-r21e\build\cmake).
-When you build your .so from Visual Studio 2019, do not use the GNU STL, use the LLVM. GNU c++ is no longer part of Android NDKs, and you need to cut it out of the process entirely.
-In the Linker Input, put the names of your library files (or file, if it's just the world one) in the Library Dependencies field, not the Additional Dependencies field.
-Everything else is the same as in those common tutorials.

An error with CMAKE and NDK on Android when opening a pre-existing github library. (Using Windows 10)

So as mentioned, I am trying to open a github zip. It uses NDK and CMAKE. Now the main issue is I am getting these two errors:
Incomplete line:
Error while executing 'C:\sdk\cmake\3.6.4111459\bin\cmake.exe' with arguments {-HE:\SpeedTest\iperf3-android-master\app -BE:\SpeedTest\iperf3-android-master\app\.externalNativeBuild\cmake\release\armeabi -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi -DANDROID_NDK=C:\sdk\ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=E:\SpeedTest\iperf3-android-master\app\build\intermediates\cmake\release\obj\armeabi -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=C:\sdk\cmake\3.6.4111459\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=C:\sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=18 -DCMAKE_C_FLAGS=-I./src/ -IE:\SpeedTest\iperf3-android-master\app -DCMAKE_CXX_FLAGS=}
I installed MinGW (C drive itself) and added packages:
mingw32-gcc (all 5 classes)
mingw-gcc-g++ (all 4 classes).
It didn't work.
Note my SDK folder is also in C drive which contains ndk & cmake (downloaded via SDK Manager).
In my CMakeLists.txt I also manually changed the path to:
set(CMAKE_C_COMPILER C:/MinGW/bin/gcc)
set(CMAKE_C++_COMPILER C:/MinGW/bin/g++)
This is the library I am trying to execute:
https://github.com/t-higuchi/iperf3-android/tree/master/app
After that I used the standard SDK Manager to add NDK, CMake and LLDB.
Though the library has another CMakeLists.txt. But its rather blank. If I am to make changes then how do I do. Which terminal window to give CMake commands (if any). I am new to NDK. Please check out the library.
Any help?

Compile c++ for android/iOS

I have to compile a library (library BPG from Bellard.org) to create a .so or a dll that I can use with android/iOS.
I'm working with Visual Studio. With some researches, I found the project "Visual C++ -> Cross Platform -> Shared Library (Android, iOS)". But I am totally lost and can't do anything.
The downloaded library is organised with some folders but Visual don't allow to make tree, all files are sorted by filters (one for header and one for sources). So I can't build, I have more than 300 errors, "can't open source file", "undefined variable"...
Secondly, the README file from project says :
The following packages need to be installed: mingw64-gcc mingw64-libpng mingw64-libjpeg-turbo mingw64-SDL mingw64-SDL_image yasm
I found installed for mingw 32 bits but no 64 bits so I don't know if build can perform. I don't know how to find the libraries.
So my question is, what is the best way to compile a C/C++ library for android/iOS ? And where can I find a tutorial for beginners ?
Thank you
I have worked as cross compiling engineer for several years. The most suitable IDE for you I think, is the CLion with CMake inside.
CMake is a tool which can cross-compile the C/C++ library into ios\android\linux\etc.. using only one config file: "CMakeList.txt".
The main task of CMake is to translate CMakeList.txt to Makefile on every platform and provide you the .a and .so files.
CLion is very powerful IDE in code editing and debugging.
Furthermore, Android needs JNI (or JNA if performance is not concerned) to wrap your c++ interfaces to java classes. Here I would recommend SWIG. SWIG is a tool to wrap C++ interfaces to other languages, that means, not only java on android you can support , other days your lib can also support python\tcl\Go\etc.
Which os are u using to build the lib? macOS or win ?
For iOS : .a file
For Android: .so file
First you should check the README file
Edit the Makefile to change the compile options (the default
compile options should be OK). Type 'make' to compile and 'make
install' to install the compiled binaries.
Use 'make -j N' where N is the number of CPU cores to compile faster.
The following packages must be installed: SDL-devel
SDL_image-devel yasm. It is recommended to use yasm version >= 1.3.0
to have a faster compilation.
Only a 64 bit target is supported because x265 needs it for bit
depths > 8.
check this to install SDL packages
https://wiki.libsdl.org/Installation

tcdrain function call with ndk toolchain for android

I'm trying to use tcdrain function call defined in termios.h by compiling my c code with the android ndk.
I'm having issues because when I build the newest android ndk, tcdrain is not defined in termios.h, however if I go inside the android source code it is defined in termios.h for bionic.
For example: https://github.com/android/platform_bionic/blob/master/libc/include/termios.h#L44
But when I build the ndk, it seems to have a different termios.h file in sysroot/usr/include/termios.h?
Why would the newest ndk not have the same include files as the newest bionic/libc files?
The source code you linked states that those functions are defined only if the following holds
#if __ANDROID_API__ >= 21
so, as nayuta said you will have tcdrain only with build environment configured with --platform=android21.
If you can not use plafform android21, you may still define yourself the functions you need.
In case of tcdrain a possible replacement would be
#define tcdrain(fd) ioctl(fd, TCSBRK, 1)
Did you configure your build environment with --platform=android21 or later?
Before android 5.0, api level 20 and older, function declarations is replaced with android/legacy_termios_inlines. h.
If you configure for android 5.0 or later, you can use tcdrain.

Creating standalone tool-chain for android NDK

I am trying to build latest FFmpeg along with halfninja's code for android since some of the options are not available in older versions.I updated FFmpeg packages and tried to run ./create_toolchain.sh.
I am getting the following error:
user#user:~/Android/android-ffmpeg-x264/Project/jni$ ./create_toolchain.sh
~/Android/android-ffmpeg-x264/Project/jni ~/Android/android-ffmpeg-x264/Project/jni
/home/user/android/tools:/home/user/android/platform-tools:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/user/android/tools:/home/user/android/platform-tools:/home/user/Android/android-ndk-r8e:/home/user/Android/android-ffmpeg-x264/Project/jni/toolchain/bin
Host system 'linux-x86' is not supported by the source NDK!
Try --system=<name> with one of: linux-x86_64
My android NDK version is android-ndk-r8e.
I am facing problems in building latest version of FFmpeg by creating standalone toolchain.
Any help is appreciated.
Your system is 64 bit. The --system=linux-x86_64 should be the parameter of make-standalone-toolchain.sh which is part of the NDK. Like this:
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --system=linux-x86_64 ...
I don't know this create-toolchain.sh but I am sure there's an invocation to make-standalone-toolchain in it. That's where you have to add the parameter.

Categories

Resources