Make package for Android - java

Problem Description
I'm writing module for Android (JAVA) that use native code (C). In JAVA I have created Class "MyClass.java" and implement all functions and classes there, also I call loadLibrary function from that file.
Now I want to build something like dll in C only for Android JAVA in order to give it to other users and they can use my class and functions.

You probably want to create a library project that contains your Java and C stuff.
http://developer.android.com/guide/developing/projects/index.html#LibraryProjects
Do you want it to be used at runtime or compile time?

Related

See result of System.loadLibrary/ the defined methods and classes?

I have an .so file that I successfully integrated into android studio and I can load the file by calling System.loadLibrary.
Is there a way I can see what methods and classes I can use now with this library loaded?
Depends on how the library maps Java methods to native function pointers: it can do one or both of the following:
Dynamic symbols that start with Java_ are mangled names for the class and method name.
If the code uses RegisterNatives system call you will have to put a breakpoint on env->RegisterNatives and look at the surrounding code/arguments.

Java call of .NET

I try to use a .NET Library in Java. Therefore, I use JNI calls of a C++ project which refers / uses a DLL of the .NET.
To use the .NET Library I have several steps in-between. First I use C# project as a ‘wrapper’ instance between the Library and a C++ project. The C# project creates a DLL.
The main wrapper C# class (CSHARPWRAPPERSession) is [ComVisible(true)] and has a fixed GUID [Guid("XXX")]
Out of this ‘C#’-DLL I create a TLB file.
This tlb file is imported by my C++ project.
#import ".\CSHARPWRAPPER.NET.tlb" no_namespace, named_guids
When I create an exe out of the C++ project I can use the wrapper perfectly and call the needed .NET library.
Now I try to make a connection between the C++ project an JAVA. Therefore, I use JNI and created DLL out of the C++.
This leads to the error 0x80040154 – Class not registered.
This error happens, when I try to call CoCreateInstance(CLSID_CSHARPWRAPPERSession)
I have registered all used Libraries with regasm and done a gacutil on it.
What am I doing wrong? Any ideas or suggestions?

Missing class error when trying to call Android JAR-based JAVA class from Delphi

I am writing an Android App using XE7. I have a java source file that contains a class that I wish to use. I am struggling with the exact way of creating and working with this class. I have done the following:
The supplied file (a commercial JAVA library) is called FT311I2CInterface.java. (It gives access to the FT311 USB device). The class inside this file is called "FT311I2CInterface".
This file is part of an Android demo project that I was able to compile and run using Eclipse. Further, I was able to generate "FT311I2CInterface.jar" from "FT311I2CInterface.java".
I have added "FT311I2CInterface.jar" under the library node of my Delphi Android project.
From "FT311I2CInterface.jar" I tried to extract a Delphi interface for this class using the recommended Java2op.exe tool but was not successful, but using the commercial tool Java2Pas I did obtain a Delphi interface "JFT311I2CInterfaceClass" and "TJFT311I2CInterface".
Using this interface I have constructed and run a simple Android project that calls this import unit, creates an instance of this class ready for me to call its methods.
My creation line is:
procedure Test;
var
F311 : JFT311I2CInterface;
begin
F311 := TJFT311I2CInterface.JavaClass.init( MainActivity );
// ...
end;
when this line is called, I get the error:
Exception class EJNI with message 'Java class JFT311I2CInterface could not be found'. Process Project1.apk (6287)
My question is, do I have to register or load this class in some way before I can call it? I see that there are lots of examples of calling Java classes from Delphi but I am confused when it comes to using a class that is not already in the Android SDK.
Look for the video and website's from Brian Long. He has an example of creating a Delphi Android wrapper (for a Java class) that isn't supplied by Embarcadero. His example is for calling Toast. There are lots of gotcha's here, but if you can follow how he "translated" the Android Api documentation into a Delphi wrapper, then you'll be able to do the same for your own Java class - eventually. There are 1 or 2 of my own here on SO. Compare the Android documentation with the Delphi wrapper implementation until you see the pattern emerge.

Android How do I use the same native library (.so) for multiple projects and classes?

I am making a android project with some native code . running javah results in header files which contain function prototypes with fully qualified java class name in their names. I assume this means that those functions and headers are generated to be called only by the java class which was used to create them using javah.
How do I go on making a native lib that I can use with other classes as well as other projects. Like say I made a library that has a function that I want to use in all my projects then how would I build it so that I can use just the .so file each time without recompiling it for the project. I think we call this dynamic library?
Is it possible? or do I have to create seperate jni headers for each class and then reuse the remaining c/c++ code?
if you want to reuse your native library from various Java projects, you should directly bundle your native library with a Java class that is independent from your initial project.
This way you'll be able to distribute it as an Android Library Project, and use it from your various classes and projects.

How can I create a simple JNI library that I can simply call methods from

I have an existing .c and .h file from a library. I would like to be able to call the functions contained within the c files from Java. I understand that this requires the JNI, and I have a pretty good understanding of how I will need to modify the .c and .h files in order to make them work with the JNI. What I don't fully understand how to do, or if it's even the purpose of the JNI, is to make a simple JNI library that I can then wrap into another java application. I want to create the interface; extend the C API into Java so I can use it there. The final result would be something like: someCmethodInSomeJNILibrary() located in some jnilib sort of file.
Thanks in advance
First, you may wish to consider using JNA. JNA would allow you to interact without writing additional native code. This is particularly convenient if you already have a library and don't want to build another.
With JNI, the steps required include:
Design a Java API of native methods for interacting with the C code.
Use javah to generate C stubs for the Java native methods.
Glue the C stubs to your C code.
Generate a dynamic/shared library containing the native method functions and your original C code.
Load that shared library in Java.
More detail can be found in "Compiling, Loading, and Linking Native Methods" in the JNI documentation.

Categories