Can j2objc be used to allow a java wrapper for a native library be used as java library in an iOS project?

288 views
Skip to first unread message

pim...@gmail.com

unread,
Dec 2, 2014, 10:50:34 AM12/2/14
to j2objc-...@googlegroups.com
Current situation:

I have a set of binaries and header files for a library written in C.
An iOS version is available, as is an android version.

I have written a wrapper for this library in Java.
This wrapper is usable for windows, unix and android right now.
It loads a different native library depending on the platform.

The java wrapper uses the native library to setup sockets and receive information.
Socket management is handled by the wrapper.

This allows the android and desktop developers to focus on the UI.

Now, I imagine I could have a bit of iOS code poll the java code whether it needs anything from time to time. Like this, the Java code would be completely detached from it's native library, and only be responsible for processing the values received from the sockets (as well as "managing" the sockets via delegation by setting a flag that it wants to do something, which iOS would read and act upon).

This, however, is a troublesome process and a dirty hack.

It's my fallback in case I can't do what I'm asking for:

Is it possible to use a iOS library in Java with j2objc? Can I just use `System.loadLibrary(<ios library filename>)`, and all my native calls will work?

Tom Ball

unread,
Dec 2, 2014, 12:07:09 PM12/2/14
to j2objc-...@googlegroups.com
You had me until System.loadLibrary() -- much as we'd like to support dynamic code loading, iOS app restrictions explicitly forbid it. Even if you figured out a way to do so, any app using your library wouldn't be accepted by the App Store.

But hope is not lost, as there are other ways to do this. If the API isn't too big, the easiest is probably to add OCNI comments. It's a technique borrowed from GWT that allows developers to embed Objective-C code into Java files such that it is ignored by the Java compiler. For example, here is how System.exit() is implemented:

  public native static void exit(int status) /*-[
    exit(status);
  ]-*/;

The exit(status) call is the Posix system function available in most systems. If the above is compiled with a Java compiler, everything between the /* and */ is ignored, creating a normal native declaration.

For larger libraries, a wrapper generator may be worth the effort. SWIG is a popular choice; although it doesn't have a j2objc-specific generator, it looks easy to extend especially by cut & pasting from its Java module.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bigr...@gmail.com

unread,
Dec 8, 2014, 7:18:47 PM12/8/14
to j2objc-...@googlegroups.com
Couldn't the System.loadLibray implementation for j2objc just be a nonop or or log to the console a comment to verify the library is linked? I think the interesting thing here is the question, how would j2objc handle a real native library call from java code, ie one without the special comment context?

I have a some media code that uses ffmpeg native lib to do some conversions. Would j2objc be able to handle the jni wrapper?

Tom Ball

unread,
Dec 8, 2014, 7:35:04 PM12/8/14
to j2objc-...@googlegroups.com
Yes, the System.loadLibrary() can be no-opped. J2ObjC doesn't support jni wrappers, but can generally compile the native function code without problems. In many cases, a good editor with search-and-replace can adapt a source file easily enough. It helps that we're moving our source base to use primitive types defined in jni.h, such as jint instead of int (also helps with 32/64 bit differences).

For example, here are the Android and J2ObjC sources for the java.util.regex.Pattern native code:

On Mon Dec 08 2014 at 4:18:49 PM <bigr...@gmail.com> wrote:
Couldn't the System.loadLibray implementation for j2objc just be a nonop or or log to the console a comment to verify the library is linked?  I think the interesting thing here is the question, how would j2objc handle a real native library call from java code, ie one without the special comment context?

I have a some media code that uses ffmpeg native lib to do some conversions.  Would j2objc be able to handle the jni wrapper?

Reply all
Reply to author
Forward
0 new messages