how to use a native library using JNI in Android - android

I am new in JNI and trying to use a [library][1] in android project using ndk. It seems the
It seems the library is loaded in the android project properly using the following code :
static{
System.loadLibrary("usb1.0");
}
but when I try to call a native method following the libusb API like libusb_init() I get following error :
No implementation found for void com.example.hellojni.MainActivity.libusb_init() (tried Java_com_example_hellojni_MainActivity_libusb_1init and Java_com_example_hellojni_MainActivity_libusb_1init__)
what am I missing ? Do you have any better beginner examples ?
[1]: https://libusb.sourceforge.io/api-1.0/index.html

Related

Find Xamarin class name or namespace in android java code

I have an android aar which is used in ReactNative and Xamarin android. I need to find the native api is called from which hybrid framework. For ReactNative I used Class reactClass = Class.forClass("com.facebook.react.ReactActivity") , if reactClass is not null then it was called from ReactNative. How do I find for Xamarin
Do you try to do like below ?
Java.Lang.Class.FromType(typeof(YourClass)).Name

Implementation of Compiled code is hidden Kotlin Android

I've been trying to write a library in kotlin and once the library build is generated as aar file when opening it in Android Studio it is not showing me how my class exactly looks and how the methods are implemented in that.
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package com.mypackage.dexter
public final class Test private constructor() {
public final fun testMethod(): kotlin.Unit { /* compiled code */ }
}
I've tried Java decompiler plugin in android studio, Kotlin Byte code (This also doesn't show it in the same format that we get if the same code is written in Java). The ultimate thing that I wanted to achieve by looking at the code is that I want to use debugger in the library and evaluate things while running my application where this library is integrated.

Visual Studio can't find extension methods Splat.BitmapMixins.FromNative() and Splat.BitmapMixins.ToNative() in Android app

While upgrading a Xamarin app that leverages Splat's bitmap functionality we are getting errors around the use of the methods mentioned in the title:
'IBitmap' does not contain a definition for 'ToNative' and no accessible extension method 'ToNative' accepting a first argument of type 'IBitmap' could be found (are you missing a using directive or an assembly reference?)
In an effort to troubleshoot the problem we have created a new sample Android-only Xamarin app and referenced Splat and Splat.Drawing via NuGet. We then added calls to these methods and have reproduced the error:
public void Bar(IBitmap source)
{
Bitmap native = source.ToNative();
IBitmap bitmap = native.FromNative();
}
It seems as though the Andriod app may not be referencing the proper build of the Splat libraries. When I monitor paths containing %USERPROFILE%\.nuget\packages\splat in ProcMon I don't see any attempt by Visual Studio to open/read anything other than files in the .\netstandard2.0 directories during the build process.
When I open the netstandard2.0 libraries in DotPeek I can see that the class BitmapMixins is not included, but it is included in the monoandroid90 builds.
How do I get the reference to Splat in the Android project to pull in the proper build of Splat?
Example project can be found here: https://github.com/jctlp/SplatBitmap
The error caused by that you did not have the extension methods to assist with dealing with Bitmaps of ToNative and FromNative.
Please do not add reference by installing the Splat from NuGet directly. Add reference to the whole project of Splat would be okay.
Download the whole Splat project from the link below.
https://github.com/reactiveui/splat.git
I create a Xamarin.forms app named App1. And create a class Class2.cs in App1.Android. When I add reference to Splat from the project. It works well with ToNative and FromNative extension method.
Please note: Do not use the Bitmap, use Android.Graphics.Drawables.Drawable or var instead.
public void Bar(IBitmap source)
{
Android.Graphics.Drawables.Drawable native = source.ToNative();//var
IBitmap bitmap = native.FromNative();
}

Scala reflection java.rmi dependency, can it work on Android?

I would like to use the Scala (2.11) reflection package's runtime mirror in a Scala application compiled for android which is being build using Scala on android.
I was able to fiddle with ProGuard options in order to make it include the required Scala classes. However when I try to get a mirror instance:
universe.runtimeMirror(this.getClass.getClassLoader)
(Indeed it fails during the lazy computation of universe)
The application crashes in run time:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/rmi/Remote;
at scala.reflect.internal.Definitions$DefinitionsClass.RemoteInterfaceClass$lzycompute(Definitions.scala:370)
at scala.reflect.internal.Definitions$DefinitionsClass.RemoteInterfaceClass(D efinitions.scala:370)
at scala.reflect.runtime.JavaUniverseForce$class.force(JavaUniverseForce.scal a:255)
at scala.reflect.runtime.JavaUniverse.force(JavaUniverse.scala:16)
at scala.reflect.runtime.JavaUniverse.init(JavaUniverse.scala:147)
at scala.reflect.runtime.JavaUniverse.<init>(JavaUniverse.scala:78)
at scala.reflect.runtime.package$.universe$lzycompute(package.scala:17)
at scala.reflect.runtime.package$.universe(package.scala:17)
This crash is for me as expected as it isn't:
It is expected as java.rmi is not part of the Android API and I should expect any code trying to load its classes to crash.
It is unexpected as I didn't know that Scala's reflect package used java.rmi
I have traced the code to were rmi is required, that is to JavaUniverse (a trait mixed in JavaUniverse class) force method:
...
definitions.RemoteInterfaceClass
...
Which leads to DefinitionsClass:
lazy val RemoteInterfaceClass = requiredClass[java.rmi.Remote]
Am I wrong to think that this is a no-go for Scala reflection in Android?
If I am, what could be a workaround to this problem?
To summarize your solution and a related solution, it is sufficient to add two files, and modify build.sbt to include:
dexAdditionalParams in Android += "--core-library"
Add java/rmi/Remote.java to your project with the content:
package java.rmi;
public interface Remote {}
Add java/rmi/RemoteException.java to your project with the content:
package java.rmi;
public interface RemoteException {}

tess-two can't find libpng.so

I have followed building instructions for tess-two on
Github
I build tess-two using NDK successfully and imported the library
I am trying to run the test application provided on the same repository but whenever the application starts it gives the following exception:
That error is caused once new TessBaseAPI(); is called.
dlopen("/data/app-lib/com.datumdroid.android.ocr.simple-2/liblept.so") failed: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libpng.so" needed by "liblept.so"; caused by load_library(linker.cpp:745): library "libpng.so" not found
Can anyone help with that?
I have followed suggestion of Dmitry Zaitsev & thannks to him , solved my problem also .
Please update your TessBaseAPI.java from tess-two library project as below :
static {
System.loadLibrary("png");
System.loadLibrary("lept");
System.loadLibrary("tess");
nativeClassInit();
}
Build tess-two project after updating these file . In my case I have build it using eclipse.
Hope it should solve your problem as well.
It seems like System.loadLibrary("png") call is missing in TessBaseAPI, therefore library can't be found.
Try to call System.loadLibrary("png") before calling new TessBaseAPI(). Typically this is done in static initialization block, like so:
public class MyClass {
static {
System.loadLibrary("png");
}
public void doStuff() {
new TessBaseAPI();
}
}

Categories

Resources