Creating and using Java Bindings library for PocketSphinxAndroidDemo - android

I want to create a Java Bindings library for the PocketSphinx Android Demo project for Xamarin, that I can then use in my Android application. I have done the following:
downloaded the PocketSphinxAndroidDemo project, and installed and run it on my Android device using Eclipse without any issues - the native java code works nicely.
created a Java Bindings library in Visual Studio with the pocketsphinx-android-0.8-nolib.jar file marked as an EmbeddedJar and marked Copy to output directory always and got this to compile fine. I'm using the jar from the downloaded demo project.
added an android application project to my Visual Studio solution and added a reference to my bindings library.
added lib/armeabi/libpocketsphinx_jni.so to my application project (also armeabi-v7a) (using the files from the demo project) and marked the *.so files as AndroidNativeLibrary with Copy to Output directory set to Copy Always.
No matter what I do I can't run the line of C# code
var config = Decoder.DefaultConfig();
I always get a Java.Lang.UnsatisfiedLinkError exception (as described in my forum post here). I'm trying to run this line of code because the demo project has this line of java:
Config config = defaultConfig();
My question is, do I need to use DllImport here? Or perhaps Java.Lang.JavaSystem.LoadLibrary? Is this a scenario where the project should be built using ndk-build?
I have tried both DllImport and LoadLibrary with no success. Any leads or things to try much appreciated!
Thanks in advance,
David

Following on from a response that I got in this forum thread, I worked out that I needed to call
Java.Lang.JavaSystem.LoadLibrary("pocketsphinx_jni");
in the OnCreate method of my class, before calling the DefaultConfig method.

Related

create a nativescript plugin for Android with compiled C files

I'm trying to create a nativescript plugin, following the official documentation here
I created my .aar file and copied it into the platforms/android folder, which is working when I call my plugin class. The problem I'm facing is when I try to call a function that loads a native library, giving me a UnsatisfiedLinkError.
I've already seen these links 1 2 and tried all the options mentioned, but none of them seems to work. Since those links are two years old, I wonder if these methods are now deprecated.
My tns version is 4.1.2 and tns-android is 4.1.3.
I've tried adding the *.so files to the following folders:
platforms/android
platforms/android/libs
platforms/android/libs/armeabi
platforms/android/libs/jni
platforms/android/libs/jni/armeabi
platforms/android/armeabi
platforms/android/jni
platforms/android/jni/armeabi
platforms/android/jniLibs
platforms/android/jniLibs/armeabi
As you can see, I've tried all possible combinations based on the previous posts (my *.so files are for armeabi architecture)
Note that the only case that don't fail as the others is platforms/android/jniLibs/armeabi. While trying this one I got an error telling me that one of the .so files is repeated (merge conflict) when calling tns debug android, the other ones just fail on runtime when I call the function
I believe linker error is totally from your native library. Your call from plugin to native Java class is successful here, Java to C seems to be the issue.
Did you verify whether your native library (.so files) were compiled successfully, may be by importing them in a simple Android app? If it still fails, then it has nothing do with {N} runtime but may be something with NDK compilation.

Proper way to create android project with c++ support

I'm running the latest build of android studio. Trying to make a project compilable and running utilizing the power of this https://github.com/tpruvot/cpuminer-multi/ c++ stuff. What I've tried so far is create new project with c++ support - it creates the corresponding project structure. It also includes CPP file that is called from the main activity. Now let's say I want all this aforementioned miner code to be there as well - I’ve tried just copy-pasting it into the cpp folder. What are my next steps? Calling ndk-build against $ProjectFileDir$\app\src\main directory just fails with an error Android NDK: Your APP_BUILD_SCRIPT points to an unknown file: ./jni/Android.mk . Same goes if I try to import an existing project like https://github.com/mdelling/cpuminer-android - first of all I’m trying to do and ndk-build, but it just fails saying that it treats warning like errors so all the cpp/h functions remain inaccessible and are not packed into resulting apk. If I try to install the resulting apk it just crashed on the start. So for the first case – how can I like all the cpp sources to the app, and for the second – how can I make it work at all?
First of all, the crash is probably because your Java code is trying to load the c++ shared library and fails, because the library was not built due to the NDK errors. So take care of the errors first.
Second, did you configure your Android.mk properly? Have a look here, for example (it is for Eclipse and not Android Studio, but the mk file is the same): Getting started with Android NDK. Looking at an existing simple project can also help: hello-jni.

Xamarin Android app referenced to the dynamic shared library(.so) is looking for .dll extension

I have a Visual C++ dynamic shared library (.so) project created using Xamarin.
I am also creating a Xamarin.Android app in the same solution and trying to add the dynamic shared library project as a reference, so that I can debug into the c++ code.
I am doing this by selecting 'Add Reference' on the Android app project and bringing up the 'Reference Manager'.
I see the dynamic library, check the box and hit OK.
The library gets added to the list of references with a yellow triangle indicating there is some problem. When I select it, I do see that the path property is empty.
I go ahead and build the project and I see in the Output window the following:
C:\Program Files
(x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1550,2):
warning : Could not find file 'c:\users\documents\visual studio
2015\Projects\MySharedLibrary\x86\Debug\libMySharedLibrary.dll'.
No wonder the error, it is looking for the library with "dll" extension instead of the "so" extension.
What do I have to do to make the Android app reference the dynamic shared library(.so) for debugging?
-Thanks
Edit:
I get the same error even with the SanAngeles demo project and the sample project on the MSDN blog.
Edit2:
Direct reference to the .so lib works, it is the project reference to native library that throws this error.

Develop an Android Plugin (Involving OpenCV for android) for Unity in Android Studio

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.

Sharing Java library with Android Apps

I'm just getting started in Android development, and use Netbeans with NBAndroid and SDK 17.
I'd like to use the same Java source code in my Java and Android app.
http://developer.android.com/guide/developing/projects/projects-eclipse.html says how to do it in Eclipse (although it is sketchy on the .JAR connection thing), but I can't seem to make it work in NB.
Based on that link, My understanding is that the correct setup for the Android app is an Android Application project which references an Android Library project which in turn references a .JAR library produced by a Java Library project. I could then also have a Java Application project referring to the same Java Library project.
So, I've set up this project structure... I have an AndroidApp project which is a basic HelloAndroid Activity in a com.ex package. This project includes an AndroidLib library project in the Libraries folder. I also have a LibClass.java file which defines a simple LibClass class which has one function getText() that just returns a String to be displayed. The MainActivity in the AndroidApp calls this to get the String to output.
When I put LibClass.java directly into the AndroidLib project, everything is fine.
But what I want to do is to share the source code with Java.
So I want to move the LibClass.java into the JavaLib library, whose .JAR file is included in the AndroidLib project. However, when I tried that, I get an error in the MainActivity class, complaining it can't find LibClass. Looking at the Projects window, I can see LibClass.class inside the com.ex package in the JavaLib.jar in the Libraries folder of the AndroidLib project. And AndroidLib is visible in the Libraries folder of the AndroidApp project, but it doesn't show any packages or other contents there.
So I feel like I'm just one step away from making this work. Do I need to do something with one or other of the AndroidManifest files perhaps? Or do something with the build.xml files? Or am I on the wrong track altogether?
I'd be really grateful if someone could post a how-to for this.
I'm trying something similar; I've got Java EE projects, built using Eclipse, and I'm trying to utilize some of that code from my Android projects. This should give me a shared codebase rather than a bunch of confusing SVN externals which I've had to endure before.
Rather than creating JAR files I've found that working with the source and building for the platform works best (well, it has been working but I've got a problem with it at the moment). So, what I'm doing is:
c:\MySvnFolderStructure\MyJavaProjectFolder\src\ (and then all the source under that)
c:\MySvnFolderStructure\MyJavaProjectFolder\android\ (and all the Eclipse Android project gubbins)
c:\MySvnFolderStructure\MyJavaProjectFolder\jee\ (and all the Eclipse JEE project gubbins)
The Android and Java EE projects do not have their own src folders, they both link to the src folder in their parent folder. What this means is that each of the Java implementations is building its own byte code version from the source, and using its own external libraries (like the Apache HTTP ones, for example).
Naturally they can't share stuff like awt (as mentioned in another post), but there's plenty of stuff that does cross-over especially if it's core Java classes that are being used.
Also, it's proving a bit tricky writing JUnit tests as there needs to be some duplication of the test code at the moment because the Android ones need extra instrumentation, but I'm working on it.
Also, see this post about relative paths in Eclipse, which means the folders can be checked-out to different places on different machines (like we all do with our version control check-outs) and still be shared.
if I understand your situation correct, you are trying to use a custom java library for both your android and java applications.
For this scenario, you can build the java library first. Instead of adding the java library jar as android library, you can drop the jar directly inside the libs folder of android project and add it to android project's build path.
If you are using ANT scripts for building the java library jar , you can consider adding the source files also as part of jar. This will help you get code assistance when you develop the android part. But this part is purely optional.
The problem is that the Java platform in Android is different from the JDK platform.
In particular, the .JAR library CANNOT refer to anything that is not icluded in the Android platform. An example of things you can't refer to is java.awt.* (except you can have java.awt.fonts).
There is also a difference between JDK String and Android String -- Android does not implement the isEmpty() method.

Categories

Resources