Build Error with Android+JNI+OpenCV - android

Iam New to Android OpenCV+NDK devlopment. I want to find out the largest contour in the image in Android for that I am trying to build the JNI Integrated OpenCV Android project in eclipse. I've included
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/include
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/include
${ProjDirPath}/../../sdk/native/jni/include
${NDKROOT}/platforms/android-9/arch-arm/usr/include
/usr/include/c++/4.6
/usr/include
${NDKROOT}/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.8/include
to the project -> Properties -> C/C++ General -> Paths and Symbols -> Includes under GNU C++
If I tried to build the project I am still getting the build error.
showing error as 'class std::vector > >' has no member named 'toArray' at the line Point points = contours.toArray();
What is this error. How can I resolve this.
and my Console Output is:
* Incremental Build of configuration Default for project OpenCV-Square-Tracking-Android-JNI-master ****
/home/dharaniinfotech/Downloads/android-ndk-r10d/ndk-build all
Android NDK: WARNING:jni/Android.mk:native_sample: non-system libraries in linker flags: -lboost_system-gcc-mt-1_53 -lboost_regex-gcc-mt-1_53 -lboost_filesystem-gcc-mt-1_53 -lboost_date_time-gcc-mt-1_53
Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES
Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the
Android NDK: current module
[armeabi-v7a] Compile++ thumb: native_sample <= jni_part.cpp
jni/jni_part.cpp: In function 'jint Java_org_opencv_samples_tutorial3_Sample3Native_FindSquares(JNIEnv*, jobject, jlong, jint)':
jni/jni_part.cpp:77:30: error: 'class std::vector > >' has no member named 'toArray'
jni/jni_part.cpp:82:25: error: no match for 'operator[]' in 'points[i]'
make: * [obj/local/armeabi-v7a/objs-debug/native_sample/jni_part.o] Error 1
Please Help me to know what I am missing.

First thing I've wrongly included
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/include
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/include
They should be like
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.8/include
And go to Code Analysis(right click the project-> C/C++ General) and select use project settings and uncheck all the check boxes.

Related

Problems attempting to build oboe c++ library using cmake on windows

I am attempting to build 'oboe' on a windows machine using cmake (version 3.25.0) and a visual studio generator. The end goal is to create a c++ audio library that uses oboe as the back end for android, but as a first step simply geting oboe to build is the plan.
I have managed to succesfully generate build and use a static library with a few simple test functions using the method described below, but I run into errors when attempting to build oboe.
I have also managed to get oboe to build by using the method described in the documentation and
doing add_subdirectory etc. in the CMakeLists file of android studio. However I am trying to generate/ build using cmake outside of android studio.
The oboe repo is found here: https://github.com/google/oboe
The toolchain file I am using is: 'android.toolchain.cmake' included in the NDK at the location: ndk version/build/cmake/android.toolchain.cmake
I use a batch script to automate generating/ building to several different android ABI's, inspired by a similar shell script included with the oboe repo 'build_all_android.sh'. The .bat script I made is shown below:
#echo OFF
set BUILD_DIR=build
set ANDROID_NDK=C:\Microsoft\AndroidNDK\android-ndk-r23c
set GENERATOR="Visual Studio 17 2022"
set CMAKE_GENERATOR=-G %GENERATOR%
set CMAKE_TOOLCHAIN_FILE=-DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake
set CMAKE_SYSTEM_NAME=-DCMAKE_SYSTEM_NAME=Android
set EXTRA_CMAKE_ARGS=-DBUILD_SHARED_LIBS=true -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_static
CALL :build_android armeabi-v7a ARM 16
CALL :build_android arm64-v8a ARM64 21
CALL :build_android x86_64 x64 21
CALL :build_android x86 x86 16
EXIT /B %ERRORLEVEL%
:build_android
set ABI_VERSION=%~1
set GENERATOR_PLATFORM=%~2
set MINIMUM_API_LEVEL=%~3
set CMAKE_ANDROID_ARCH_ABI=-DANDROID_ABI=%ABI_VERSION%
set ABI_BUILD_DIR=%BUILD_DIR%\%ABI_VERSION%
set CMAKE_GENERATOR_PLATFORM=-A %GENERATOR_PLATFORM%
set CMAKE_BUILD_DIR=-B %ABI_BUILD_DIR%
set CMAKE_MIN_API=-DANDROID_PLATFORM=android-%MINIMUM_API_LEVEL%
set CMAKE_ARGS=%CMAKE_BUILD_DIR% %CMAKE_ANDROID_ARCH_ABI% %CMAKE_GENERATOR% %CMAKE_GENERATOR_PLATFORM% %CMAKE_SYSTEM_NAME% %CMAKE_TOOLCHAIN_FILE% %CMAKE_MIN_API%
echo building for android ABI: %ABI_VERSION%
echo cmake arguments = %CMAKE_ARGS%
echo:
cmake %CMAKE_ARGS% %EXTRA_CMAKE_ARGS%
echo:
cmake --build %ABI_BUILD_DIR% --target ALL_BUILD
echo:
echo:
EXIT /B 0
When I run this using the ANDROID_NDK variable (line 3) pointing to the root of the up to date ndk that comes with the visual studio 2022 android tools, I get an error:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message):
The C compiler
"C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/user/Documents/Code Projects/oboe/build/x86/CMakeFiles/CMakeScratch/TryCompile-pp2ibg
Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_c21fb.vcxproj /p:Configuration=Debug /p:Platform=x86 /p:VisualStudioVersion=17.0 /v:m && MSBuild version 17.4.1+9a89d02ff for .NET Framework
ANDROID_HOME=C:\\Microsoft\AndroidSDK\25
ANDROID_SDK_ROOT=C:\\Microsoft\AndroidSDK\25
ANT_HOME=
JAVA_HOME=C:\Program Files\Android\jdk\jdk-8.0.302.8-hotspot\jdk8u302-b08
NDK_ROOT=C:\Microsoft\AndroidNDK\android-ndk-r23c
testCCompiler.c
In file included from <built-in>:349:
<command line>(1,9): warning : '__ANDROID_API__' macro redefined [-Wmacro-redefined] [C:\Users\user\Documents\Code Projects\oboe\build\x86\CMakeFiles\CMakeScratch\TryCompile-pp2ibg\cmTC_c21fb.vcxproj]
#define __ANDROID_API__ 1
^
<built-in>(342,9): note: previous definition is here
#define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
^
1 warning generated.
ld: error: cannot open crtbegin_so.o: No such file or directory
ld: error: unable to find library -llog
ld: error: unable to find library -landroid
ld: error: cannot open crtend_so.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Application Type\Android\3.0\Android.Common.targets(125,5): error MSB6006: "clang.exe" exited with code 1. [C:\Users\user\Documents\Code Projects\oboe\build\x86\CMakeFiles\CMakeScratch\TryCompile-pp2ibg\cmTC_c21fb.vcxproj]
CMake will not be able to correctly generate this project.
and cmake fails to generate the project. I can actually get this to generate correctly, but only if I set the android NDK version within the cross-compilation options of visual studio to use the NDK bundled with my installation of Android studio, and also set the ANDROID_NDK variable of the batch script to point to the same root.
In this case, though cmake generates the project the build fails with a similar reason to the warning given above:
Building Custom Rule C:/Users/user/Documents/Code Projects/oboe/CMakeLists.txt
AAudioLoader.cpp
In file included from <built-in>:404:
<command line>(1,9): error : '__ANDROID_API__' macro redefined [-Werror,-Wmacro-redefined] [C:\Users\user\Documents\Co
de Projects\oboe\build\x86\oboe.vcxproj]
#define __ANDROID_API__ 16
^
<built-in>(394,9): note: previous definition is here
#define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
^
1 error generated.
Thanks in advance for any help, it is much appreciated!
Your build fails because you instruct the compiler to view the warning '__ANDROID_API__' macro redefined as an error. And this warning seems to occur because Visual Studio still sets the macro __ANDROID_API__ instead of setting __ANDROID_MIN_SDK_VERSION__. I think that is a Visual Studio bug that was introduced when they started shipping the NDK 23 instead of 21.
EDIT:
This issue is being tracked here.

Having trouble building a very simple OpenCV C++ .so library using CMake for use on Android

I am trying to use CMake to create a .so library that uses OpenCV for use on Android.
I have no prior experience with CMake before this. I got a couple things working with CMake so far. I successfully built and ran a simple OpenCV program for windows and I've also created a .so file that doesn't use OpenCV. I was able to successfully called that .so from a Unity program on Android. My goal is to create an OpenCV plugin for Unity. I turned to CMake because I tried making a simple .so in Visual Studios, but my Unity program couldn't find it (though I was able to create a .dll that used OpenCV for a windows Unity program using Visual Studios that worked).
I have both r19c Android NDK and OpenCV 4.5.2 Android SDK on my C drive.
My code file is test.cpp, and it very simply uses some OpenCV elements. It's as follows:
#include "opencv2/opencv.hpp"
extern "C" int get_number()
{
cv::Mat img(2,2, CV_8UC3, cv::Scalar(126,0,255));
cv::Size size = img.size();
int width = size.width;
return width;
}
I am thinking that my error may be in the CMakeLists.txt file, that I am probably pointing to the wrong OpenCV folders.
My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.20)
set(OpenCV_DIR C:/OpenCVAndroid/sdk/native/jni/abi-arm64-v8a)
set(OpenCV_INCLUDE_DIRS C:/OpenCVAndroid/sdk/native/jni/include)
project( test )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_library( test SHARED test.cpp )
and I'm using a .bat file that has the following lines:
rmdir /s /q build
mkdir build
cd build
"C:/Program Files/CMake/bin/cmake.exe" ^
-S=C:\Users\m_knu\Desktop\test ^
-G"MinGW Makefiles" ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_TOOLCHAIN_FILE=C:\r19c\android-ndk-r19c\build\cmake\android.toolchain.cmake ^
-DCMAKE_MAKE_PROGRAM=C:\r19c\android-ndk-r19c\prebuilt\windows-x86_64\bin\make.exe ^
-DANDROID_NDK=C:\r19c\android-ndk-r19c ^
-DANDROID_NATIVE_API_LEVEL=android-21 ^
-DANDROID_ABI=arm64-v8a ^
-DCMAKE_MODULE_PATH="C:/OpenCVAndroid/sdk/native/jni/abi-armeabi-v7a" ^
"C:/Program Files/CMake/bin/cmake.exe" --build .
pause
all three files are located in the same folder.
When I run the .bat file I get the following output:
C:\Users\m_knu\Desktop\test>rmdir /s /q build
C:\Users\m_knu\Desktop\test>mkdir build
C:\Users\m_knu\Desktop\test>cd build
C:\Users\m_knu\Desktop\test\build>"C:/Program Files/CMake/bin/cmake.exe" -S=C:\Users\m_knu\Desktop\test -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=C:\r19c\android-ndk-r19c\build\cmake\android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=C:\r19c\android-ndk-r19c\prebuilt\windows-x86_64\bin\make.exe -DANDROID_NDK=C:\r19c\android-ndk-r19c -DANDROID_NATIVE_API_LEVEL=android-21 -DANDROID_ABI=arm64-v8a -DCMAKE_MODULE_PATH="C:/OpenCVAndroid/sdk/native/jni/abi-armeabi-v7a"
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/r19c/android-ndk-r19c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/r19c/android-ndk-r19c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: C:/OpenCVAndroid (found version "4.5.2")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/m_knu/Desktop/test/build
C:\Users\m_knu\Desktop\test\build>"C:/Program Files/CMake/bin/cmake.exe" --build .
[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
[100%] Linking CXX shared library libtest.so
CMakeFiles/test.dir/test.cpp.o: In function `get_number':
C:\Users\m_knu\Desktop\test/test.cpp:5: undefined reference to `cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)'
C:\Users\m_knu\Desktop\test/test.cpp:9: undefined reference to `cv::Mat::~Mat()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libtest.so] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
C:\Users\m_knu\Desktop\test\build>pause
Press any key to continue . . .
As I mentioned earlier, my guess is that I've incorrectly referenced the OpenCV directories, but I wouldn't be surprised if I'm missing other things.

Compiling stressapptest on NDK for Android

I am currently working on trying to compile the stressapptest (link: https://github.com/stressapptest/stressapptest) using the NDK environment, and for some reason, I'm having a hard time. These are the steps that I have taken:
Clone the repository onto my computer
Added a jni folder and moved all the files into it
Created an Application.mk file
Opened PowerShell from the location of my jni folder and execute ndk-build. This is the error I'm getting:
PS C:\Users\...\Desktop\stressapptest-master\stressapptest-master> ndk-build
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-16.
[arm64-v8a] Compile++ : stressapptest <= main.cc
In file included from jni/src/main.cc:17:
jni/src/sattypes.h:25:10: fatal error: 'algorithm' file not found
#include <algorithm>
^~~~~~~~~~~
1 error generated.
make: *** [obj/local/arm64-v8a/objs/stressapptest/src/main.o] Error 1
Is there anything else that I need in order to compile this to run on an Android system?
Set APP_STL to c++_shared in your Application.mk. ndk-build does not use any STL by default.

Android - file not found while building NDK library

I'm trying to build the SoundTouch library for Android, by following the guide provided within the source code:
Compiling
To compile the SoundTouch library source codes into an Android native
library, open Cygwin/bash shell, go to directory
"soundtouch/source/Android-lib/jni" and invoke the NDK compiler with
following command:
$NDK/ndk-build
This will build binaries for all the supported Android platforms
(arm-v5, arm-v7, X86, MIPS etc) of SoundTouch library, plus the JNI
wrapper interface as discussed below. The target binaries will be
built into the "libs" subdirectory. As long as all these .so binary
library versions are included in the APK Application delivery package,
the targer Android device can choose the correct library version to
use.
Notice that to allow Cygwin/bash to locate the NDK compile scripts,
you need to define the location of the NDK installation defined in
environment variable "NDK". That's easiest done by adding the NDK path
definition at end of your ~/.bash_profile file, for instance as
follows:
NDK=/cygdrive/d/Android/android-ndk-r6
to do this I go into Terminal in the SoundTouch/source/android-lib/jni directory and type the following commands:
export ANDROID_NDK=~/Android/Sdk/ndk-bundle
export NDK_ROOT=$ANDROID_NDK
export PATH=${PATH}:${ANDROID_NDK}
ndk-build
Then I get the following error:
[arm64-v8a] Compile++ : soundtouch <= soundtouch-jni.cpp
jni/soundtouch-jni.cpp:23:10: fatal error: '../source/SoundStretch/WavFile.h'
file not found
#include "../source/SoundStretch/WavFile.h"
^
1 error generated.
/home/daniele/Android/Sdk/ndk-bundle/build/core/build-binary.mk:499: recipe for target 'obj/local/arm64-v8a/objs/soundtouch/soundtouch-jni.o' failed
make: *** [obj/local/arm64-v8a/objs/soundtouch/soundtouch-jni.o] Error 1
If I go to that directory the file is there though. Here is a screenshot of the file with it's direcotry:

C++ on Android missing std::map:at

I am trying to compile my cocos2d-x project for android, but it I get the error:
jni/../../Classes/Utility.cpp:359:47: error: 'class
std::map,
std::allocator >, std::basic_string, std::allocator > >' has no member named
'at' make: *
[obj/local/armeabi/objs-debug/game_shared///Classes/Utility.o]
Error 1
I have set APP_STL := stlport_static in Application.mk (not Android.mk), I even have set the eclipse include path {android-ndk-r9}/sources/cxx-stl/stlport/stlport. But still nothing, any ideas? or is that method just not included in this version of c++?
UPDATE
I am using stlport_static, which is defined as follows (as static library):
I.3. STLport runtime:
This is a port of STLport (http://www.stlport.org) that can be used on
Android. It will provide you with a complete set of C++ standard
library headers, with RTTI and exception handling support.
That's because the library embeds its own copy of GAbi++.
My cocos2d-x version is 2.1rc0-x-2.1.4

Categories

Resources