Skip to content

Make it possible to build with Android NDK r16#10288

Closed
erikman wants to merge 4 commits intoopencv:masterfrom
edumentab:eman/androidBuild
Closed

Make it possible to build with Android NDK r16#10288
erikman wants to merge 4 commits intoopencv:masterfrom
edumentab:eman/androidBuild

Conversation

@erikman
Copy link
Copy Markdown
Contributor

@erikman erikman commented Dec 12, 2017

This pull request makes it possible to build OpenCV with Android NDK r16. Android have switched to unified include directories, and deprecated the android executable.

This patch makes it possible to compile the native libraries. On NDK r16 the android executable is not able to create build.xml files for use with ant so it will not build any .apk files.

Newer versions of Android ndk have deprecated the "android" executable
for creating and updating projects. This patch allows cmake to use
"avdmanager" instead for detecting SDK's and if found will not use
"android" executable for creating projects.

Without a working android executable apks will not be built but we will
get lib files for linking with.

Older toolchain versions (using gcc-4.8) will produce binaries for
x86 and x86_64 that don't link with newer toolchain versions
(using gcc-4.9), hence the need for building those binaries with a newer
version of the toolchain.
Before ANDROID_SDK had to be specified as an environment variable.
This makes it possible to compile OpenCV with for instance ndk-r16
@alalek
Copy link
Copy Markdown
Member

alalek commented Dec 12, 2017

Consider using Android CMake toolchain file instead (it shipped with NDK 15+, so it is supported natively and should be well tested):

android-ndk-r15c/build/cmake/android.toolchain.cmake

Options are similar, but not exact. Something like this:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=android-23 -DBUILD_SHARED_LIBS=OFF -DENABLE_CXX11=ON -DANDROID_STL=c++_static -DANDROID_ABI="armeabi-v7a" -DANDROID_TOOLCHAIN=clang -DANDROID_ARM_NEON=ON <opencv_src>

Lets leave OpenCV's Android toolchain "as is" for legacy NDKs (and old devices).

@erikman
Copy link
Copy Markdown
Contributor Author

erikman commented Dec 13, 2017

Thanks @alalek, that sounds like a good idea, I'll have a look

@hannesa2
Copy link
Copy Markdown
Contributor

In my personal Android point of view I want to see it merged !

With this spirit "let things untouched although a improvement/modernization is there" let OpenCV midterm (current) unusable.
And why ? Because of 3% of all users !
Please tell them "Sorry 3.4.0 is your last version support api 9, we have to got to the future. Urgent !"

Look at current Android situation, it's not usable out of the box:

  • outdated Eclipse sample project, support ended one year ago. since 2015 we know Android Studio is the future
  • you can only build artifacts with archeological build tools, it's not specified which one
  • nowhere any adaption to recent version is done, new user needs days to use it
  • ... and a lot of more reasons

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 5, 2018

@hannesa2 We can't merge any incoming patches, because who would maintain this a few months later?

The first two commits just hides "android" tool problem, without resolving them (creates .apk files with zero size, including Android tests - do we really need this?). Only complete migration to gradle builds can help here (see #8460). Also ANDROID_SDK variable is not in recommended set of environment variables: https://developer.android.com/studio/command-line/variables.html

The third 90cc3f6 commit is useful, but it is not complete: CMake is not able to properly call "find_file()" on headers.

I'm not able to reproduce problem resolved by 3135af8 commit. It should not break anything, but I have no idea that problem related to NDK16 is resolved here. @erikman Is it a build problem or runtime issue (.apk crash on launch)?

@erikman
Copy link
Copy Markdown
Contributor Author

erikman commented Jan 10, 2018

@alalek The problem is a build problem, I ran into this when I needed to recompile due to #10229 so my only goal was to be able to build libraries with r16 with my own build flags. I did not need the .apk at all so I was fine with not building it. So yes this hides the android tool problem, but will allow the build to produce libraries that can be used.

I was not aware of ANDROID_SDK_ROOT being available, I'll update to using that if available.

3135af8 and 90cc3f6 are not needed when using android.toolchain.cmake from ndk.

With the two first patches I'm able to build with the toolchain from ndk. Still doing some final testing.

@erikman
Copy link
Copy Markdown
Contributor Author

erikman commented Jan 12, 2018

I looked into ANDROID_SDK_ROOT and it is not set as an environment variable when gradle runs cmake (I find it strange that we only get the ndk location and not the sdk location, see https://developer.android.com/ndk/guides/cmake.html ).

Locally I get around this by passing it from my gradle.build:

android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_SDK=${project.android.sdkDirectory}"
            }
       }
   }
}

my point is that I would like to pass the path as a cmake variable and not just an environment variable (which is harder to pass into cmake from gradle). So what I was doing in my patch was only to expose the ANDROID_SDK that already was used by OpenCV to also be exposed as a cmake variable.

Now I'm a bit hesitant to rename it since it is used by many of the support scripts: test_cmake_build.py, samples/android/hello-android/run.cmd, test_ant_build.py, android/build_sdk.py and modules/ts/misc/run_android.py and modules/ts/misc/run.py. @alalek: What do you think?

@vpisarev
Copy link
Copy Markdown
Contributor

@erikman, thank you! Some parts of your patch have been already re-used in #10515. Let's close this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants