Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Android Studio #5003

Closed
opencv-pushbot opened this issue Jul 27, 2015 · 29 comments
Closed

Support for Android Studio #5003

opencv-pushbot opened this issue Jul 27, 2015 · 29 comments

Comments

@opencv-pushbot
Copy link
Contributor

Transferred from http://code.opencv.org/issues/4366

|| Philipp Hasper on 2015-05-28 11:31
|| Priority: Normal
|| Affected: None
|| Category: android
|| Tracker: Feature
|| Difficulty: 
|| PR: 
|| Platform: None / None

Support for Android Studio

Android Studio is now the official supported way for developing Android apps - the Eclipse integration is deprecated.

We need to migrate the Android tutorials / Android build chain to Android Studio.

History

@PhilLab
Copy link
Contributor

PhilLab commented Sep 29, 2015

Is this planned for OpenCV 3.0.1?

@hauptmech
Copy link

I have been having a very unpleasant experience getting opencv and android development to work. I'm new to android and the java toolchain. I'd be really happy with a commandline walkthrough of getting any opencv native sample running with gradle+ndk-build. An android studio example would be ok too but that seems to be a moving target right now.

@PhilLab
Copy link
Contributor

PhilLab commented Nov 11, 2015

Yeah, the "these tutorials are for Eclipse but are easily reproducable with Android Studio"-sentence at the beginning of many new Android OpenCV tutorials seems rather cynical 😉 It does work but it is by no means easy if you are new to Gradle.

@hauptmech
Copy link

12 hours to get tutorial-2-mixedprocessing to build and run. Having OpenCV 2.4 and an Arm64 device did not help things, but most of my time was figuring out a working combination of android studio, android_ndk, gradle-experimental vs gradle plugins, and viable imports of the samples (or gradle layout remappings).

The most useful thing you guys could do is add gradle files to the samples. At least one could build them and start learning from them then.

@PhilLab
Copy link
Contributor

PhilLab commented Nov 11, 2015

@hauptmech: is it possible for you to create a pull request containing that?

@hauptmech
Copy link

Don't have time. Also anything I came up with would be nothing close to best practice which is what you want for a sample.

You will need to decide whether to support ndk-build or android-studio with gradle-experimental plugin.

@hauptmech
Copy link

I revisited the OpenCV samples as prep to tackle the ROS android stuff. Using gradle-experimental and an in-situ build.gradle file for the library worked well for me. Possibly neither are best practice but after a couple days of working with this as a beginner, I can say that this approach is much easier to wrap my head around than the Import-to-android-studio way which has generally unreliable documentation and seems to change every few months.

I jotted down what I did here: http://hauptmech.github.io/OpenCV1/

@PhilLab
Copy link
Contributor

PhilLab commented Jul 14, 2016

@mshabunin Would this also be an issue for milestone 3.2 ? Maybe the priority could be increased as well - Android Studio is quite mature now.

@mshabunin
Copy link
Contributor

@PhilLab , unfortunately the core team does not have enough Android experts to help with this feature at this moment. Milestone 3.2 is intended to collect several reproducible and/or simple bugs to be fixed during summer hackathon, it does not mean that other issues will not be fixed before 3.2.
So we are still waiting for community developers willing to add gradle support to OpenCV.

@PhilLab
Copy link
Contributor

PhilLab commented Jul 14, 2016

Okay, what a pity. Thanks for the response, though!

@tudor-pop
Copy link

Hello !
I ported face detection sample project to the latest Android Studio 2.2 Beta 3 with CMake (now supported by AS). I included the header files in CMakeLists and copied the compiled libs from OpenCV-android-sdk\sdk\native\libs to my project. Also Android Studio automatically imported OpenCV-android-sdk\sdk\java and everything should work out of the box. Should I do a pull request ?

@apavlenko
Copy link
Contributor

Hi @PopTudor , please make a PR.
Even if I decide to prepare my own one - it will be helpful.

@tudor-pop
Copy link

@apavlenko I'm not sure how to integrate the branch because I downloaded the samples from original site and sourceforge instead of forking from git :( . I pushed the sample here: https://github.com/PopTudor/face-detection-Android-Studio . I'll leave them here temporarily or try to integrate them if you have any idea how. To run the sample you need at least Android Studio 2.2 Beta 2 and then 'Open an existing Android Studio project'. I think it would be a good idea to wait at least a stable version of AS 2.2 before porting the samples

@truedat101
Copy link

I can help with Android / Gradle and development issues, so I guess someone from the project core let me know what is needed. To earlier comments by folks like @PhilLab and @hauptmech , Gradle support for NDK + Android Plugin has been a long time coming, like 4+ years (ridiculously long), so it's taken a long time for the Android community to switch over to using Gradle for JNI development, despite there being claims that Gradle/Android Plugin/NDK were all working well together.

@PopTudor I'll look at your branch.

@tudor-pop
Copy link

@truedat101 awesome! Android Studio 2.2 is also stable now and future updates should not break the samples. They also updated their tutorials/documentation on NDK and CMake integration with Gradle. Hope it helps.

@lxdatgithub
Copy link

lxdatgithub commented Feb 9, 2017

Add this code to the openCVSamplefacedetection build.gradle, in my case ,it works.

    externalNativeBuild {
        ndkBuild {
             path 'src/main/jni/Android.mk'
        }
    }

In this case , the environment is android studio 2.2.3 , opencv 3.2.0.

In the new verison of android studio, the C++ support is so sweet. You right click the any directory in your project,like "cpp" or so-called "jni" directory in your project, that will a option "Link C++ Projects with Gradle" ,click it and choose ndk-build, then point to your Android.mk which is in "cpp" directory(/home/user/AndroidStudioProjects/face-detection/openCVSamplefacedetection/src/main/jni). That will add the code above to the build.gradle. Lukily , you can also choose cmake if you have CMakeList.txt file.

By the way , you would better export the OPENCV_ANDROID_SDK to the sys variable before compiling the opencv sample project, like below:

export OPENCV_ANDROID_SDK=/home/user/AndroidStudioProjects/OpenCV-android-sdk

Enjoy the new android studio and opencv!

@victorv
Copy link
Sponsor

victorv commented Apr 19, 2017

We have a working android studio build in our fork here https://github.com/10imaging/opencv

It currently uses gradle-experimental for the enhanced native debug support but it can also integrate into standard gradle.

Let me know if there is any interest and I can provide a sample AS app to show how to easily integrate it into any app.

@steveccable
Copy link

Is there any word on when/if this support should be integrated into OpenCV proper? It looks like people are getting it working in their own projects (see above comment), but the last activity I see on here of someone trying to get it into the main project was a volunteer last September with no responses to his comment.

@victorv
Copy link
Sponsor

victorv commented Jun 19, 2017

@steveccable - our tree is up to date with 3.2.0 and it's easy to build -

./build.sh - makes arm7 and arm8 targets by default
install.android.debug and install.android.release contain studio compatible installs - just move this into your project under an opencv directory and include into your project using the template at -

opencv/platforms/android/template/opencv-lib/

We migrated the template to the latest stable gradle but it's not pushed to GitHub.

@bendaf
Copy link

bendaf commented Jul 26, 2017

please create a how to page or a tutorial or example or anything. It is not so obvious for me - an android developer new to openCV - how should I integrate it to my project. Nobody uses eclipse for android development any more and I can't find an opncv example working with android studio right after import. Where do I find ./build.sh? what is install.android.debug/release, a directory?

@floe
Copy link
Contributor

floe commented Nov 1, 2017

FWIW, I've just ported tutorial-2-mixedprocessing to native Gradle 3 & CMake, which was pretty straightforward (half an afternoon). I'd be interested to work on this and submit a PR, but I don't have a very good overview how the Android SDK package is actually built. I'm assuming this is mostly done by https://github.com/opencv/opencv/blob/master/platforms/android/build_sdk.py ?

@floe
Copy link
Contributor

floe commented Dec 22, 2017

Addendum: I've built a "proper" Android Studio package from the official Android SDK. Package is available at https://github.com/floe/opencv-android, just wanted to suggest that this link may be useful for people reading here. I've also enabled JitPack for this repo, so now the OpenCV SDK can be pulled into any Android Studio project by adding two lines to the Gradle build files.

@hannesa2
Copy link
Contributor

hannesa2 commented Dec 29, 2017

@victorv your fork looks very promising. Please can you add some lines in README.md to see what's better and how to use it.

And what's about to make a PR to have it in main upstream repo, I guess I've not to explain what's the benefit, when there is not that much fragmentation out there.
Eg when I make a improvement PR to use 3.4.0 I will make a fork of a fork.... this should happen in upstream repo !

@victorv
Copy link
Sponsor

victorv commented Dec 29, 2017

@hannesa2 it will need some work to make it useful for a pull request. We are currently testing 3.4.0 with our fork and we will push that change soon which will also have an alternate way to deal with creating an Android compatible library.

@alalek
Copy link
Member

alalek commented Jan 1, 2018

I prepared build.gradle script to use OpenCV in your apps with prebuilt OpenCV Android SDKs (should work with OpenCV 3.4, just put this file into "sdk" subdirectory and follow notes).

Based on behavior / project templates of Android Studio 3.0.1 (171: Nov 9, 2017).
Successfully assembled this sample with "armeabi-v7a" ABI (runs well on Android 4.3 device).

Any feedback / suggestions / fixes based on your Android's development experience are welcome (here or via Gist comments).

Note: Building OpenCV for Android itself is out of scope of the proposed script.

@victorv
Copy link
Sponsor

victorv commented Jan 1, 2018

We have a working build system for OpenCV Android and a template working with the latest Android Studio and Gradle versions here - https://github.com/10imaging/opencv/tree/master/platforms/android/template/opencv-lib

We decided to bypass the android tool for making a compatible android library template and just keeping our template up to date against the latest tools. Fewer dependencies on Android tool set is more desirable as they seem to drop features on a whim.

@vladimir-dudnik
Copy link
Contributor

is that still actual?

@floe
Copy link
Contributor

floe commented Sep 26, 2019

No, I think this can be closed, 4.0+ works fine with Gradle/AS.

@vladimir-dudnik
Copy link
Contributor

@floe thanks for update. I'll close this issue.

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

No branches or pull requests