I have a project where we want to access to a finger print scanner (external scanner). The drivers are written in c++ (NDK).
does Native script support access to Native Components?
There are a several issues about that on NativeScript's repo on GitHub, all lead to this issue where the solution is to create an AAR file from the CPP file and load it from NativeScript.
I found also a detailed guide about how to do it, here are the main steps summarized by the author:
Create an Android library using Android Studio
Import the native C++ library and wrap the code we want to export into a Java class, using JNI
Export the library to a aar file, and import it into a new NativeScript plugin
Import the plugin into a new NativeScript application
Related
I was wondering if writing apps in C++ in Android Studio is possible. I have some knowledge in creating apps in Java but I am more comfortable with C++, and is this a complicated process?
You can use Qt and create Android apps with C++ (http://doc.qt.io/qt-5/androidgs.html).
To setup Android Studio with Qt https://wiki.qt.io/Android
You can add C and C++ code to your Android project by placing the code into a cpp directory in your project module.
When you build your project, this code is compiled into a native library that Gradle can package with your APK.
Your Java or Kotlin code can then call functions in your native library through the Java Native Interface (JNI). If you want to learn more about using the JNI framework, read JNI tips for Android.
You can also use JNI to run c++ code (as a matter of fact c++ shared library or so) from Java.
Here you can find a usefull article + sample for how to do it.
I'm developing (with Android Studio) an Android app that uses OpenCV 3.1.0. I'm using async OpenCV initialization. My questions is: do I need to copy OpenCV native libs (OpenCV-android-sdk/sdk/native/libs) to my jniLibs directory (app/src/main/jniLibs)?
In all the tutorials that I've read about how to setup OpenCV in Android Studio they copy the libraries. But, as I'm using OpenCV manager to access OpenCV libraries externally installed in the device (not static initialization), I think I don't need to copy the libraries. Am I right?
Thanks for your help.
After some time working with OpenCV for Android, I can give an answer to my question:
Async initialization
If you are using async initialization with OpenCV Manager you don't need to copy the native OpenCV libs to your project. Because you are using OpenCV Manager for that:
OpenCV Manager is an Android service targeted to manage OpenCV library
binaries on end users devices. It allows sharing the OpenCV dynamic
libraries between applications on the same device.
More info: OpenCV Manager docs.
Static initialization
According to this approach all OpenCV binaries are included into your application package (this approach is deprecated for the production code).
App without JNI part:
You have to copy the contents of sdk/native/libs into your project directory to folder app/src/main/jniLibs.
App with JNI part:
You need to modify your Android.mk file. After that the OpenCV libraries will be copied to your application jniLibs folder during the JNI build.
More info: Static Initialization docs.
Yes you will be needing to copy the contents of sdk/native/libs for OpenCV into the apps jniLibs directory if you usinf NDK, the java based classes are just a `wrapper to the OpenCV native libraries to make it easier for you to work with OpenCV.
See further official documentation at link provided below: http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/O4A_SDK.html
Target Application: Development of an Unity application for android device.The Unity application will use OpenCV for various image processing functions.
Approach Planned:
Develop an OpenCV for android application in Android Studio. This will have a bridge class for sharing data with Unity.
Export the android studio project into jar
Use the jar and other assets in Unity as a Plugin
Access the required functions for image processing using the bridge class in android using the Android Java support classes in Unity.
Build the unity application for Android Platform
Experiments Done:
Already did a set up for OpenCV for android in Android Studio.
Tested sample applications on device by generating apk.
Tested the plugin concepts in unity using DLLs for a windows based application. Dll concepts working fine for Unity.
Final Experiment--
4. Used the classes.jar generated in the build\intermediates\exploded-aar... as a plugin in Unity.
It generates an apk from the unity build. The apk gets installed on a device but fails to open.(Can't see it in the apps menu)
Used this .jar approach based on an answer in the following link
How to export library to Jar in Android Studio?
Problems:
Most of the android plugin tutorials use Eclispe and have the export to jar function. This jar file can be used as a plugin in unity.
Queries:
I found about the jar task which could be included in the gradle script.
But I am not sure how to use it ? There seems to be a lot of version for the jar task creation in Android studio. How to achieve for a project using opencv in android?
I am also confused whether .jar or .aar will solve my purpose?
Should I try any other approach like the android library for achieving the android, Opencv and unity integration.
My take on Solutions:
Generating the appropriate jar in Android Studio will solve the issues.
Else go back developing the opencv for android in eclipse
Useful Links
Can Android Studio be used to create Unity-plugin compatible JARs out of Library projects?
The following project approach is almost the same which I want to acheive
https://github.com/thorikawa/unity-opencv-android
Any help on this will be appreciated.
Update 24th Feb 2015
With the help of this link
Can Android Studio be used to create Unity-plugin compatible JARs out of Library projects?
I am able to integrate Unity and Android.
The android studio project now contains two classes an activity class and a normal java class containing various getter functions.
I am able to call any functions of the normal java class from unity.
But unable to start the activity class from unity.
My Thoughts
I guess unity generates its own manifest file while doing a build for android in unity.
Changing the android studio manifest file accordingly might solve the issues.
Queries
How to start an activity inside the android library from Unity?
How is the android studio generated manifest file provided in unity so that unity merges it with its own generated manifest without any conflicts?
Things Tried
While building in unity, if no android studio generated manifest file is provided, the app functions properly and communicates with the simple java class with getter functions.The values can be obtained in Unity.
If the default manifest file is provided along with the classes.jar the app gets installed but crashes on opening. Even the app name is changed to the one provided by the android studio manifest file.
If any one could help me with this I ll be grateful. Besides I think if I solve this step integrating opencv for android won't be very tough.
Best regards,
Swaroop
First of all you can’t use .aar files unless you have Unity5 (I don’t have but I read that aar support is added). For creating jar I use the same approach that use posted here and it worked for me.
If you have Unity5, If your project is set up as an Android library, it will output an .aar when it's built. It will show up in the build/outputs/aar/ directory in your module's directory.You need to copy those to Assets/Plugins/Android folder
If you want to open an activity from Unity since you added it inside your .jar need to declare it inside your manifest. If you are already using a custom manifest for your project just add it inside your application tag.
Otherwise you need to create a custom manifest for your project. First you need copy the Android Manifest that Unity generates when you compile your game, it’s on folder:
YourProjectname/Temp/StagingArea/AndroidManifest.xml
copy it from there to Assets/Plugins/Android folder inside Unity and add your activity of your library:
<activity android:name="yourLibraryPackagename.YourActivity"</activity>
into that manifest inside your application tag and you should be able to open your activity.
I have read the overview for Android Library project. I have in mind to develop an Android project library containing native libraries and JNI wrapper which would be included into the Android project. However, the doc does not explicitly mention if Android library project can include native libraries.
Can anyone confirm/infirm support for native libraries in Android library projects ?
As Android Document said:
An Android library project is a development project that holds shared
Android source code and resources.
An Android Library Project, in fact, isn't so different from normal Android project. You can make a normal android project as library project, except asset folder. They're just different when you declare in Eclipse Buid Path or something similar in other IDEs.
Native support simply, just a call to native layer (as in your post, Native Library) such as C/C++ library file (*.so file). This function is not specific to Android, but in normal Java project also support this feature: Calling functions from another language inside Java.
So, in short answer, yes :)
I want to work on an Android project (under Eclipse) using Opencv, I've imported the Opencv library and it's associated samples to Eclipse, as explained here:
http://opencv.itseez.com/doc/tutorials/introduction/android_binary_package/android_binary_package.html.
I tested the samples and they work well. But when I create my new own Android project in Eclipse, it can't link Opencv which is placed in the same workspace as the project (i.e. error when I write import org.opencv.core.*).
Is there something that has to be done for a new project to be linked with Opencv (src and lib)?
I recomend you to download the OpenCV prebuild package and follow these instructions.
I don't know if you are going to use C++ for openCV and crosscompile with NDK and JNI library, but that way is quite easy to set up everything. If you need more help about how including C++ OpenCV codes in android projects I can check my codes. Good luck.
It sounds like you haven’t included the Open CV library’s in your new project.
I can’t remember the exact step to include libraries in projects but below are a couple of link that might help.
http://wiki.eclipse.org/FAQ_How_do_I_add_an_extra_library_to_my_project's_classpath%3F
Import Libraries in Eclipse?