8

I am developing a project that requires business logic calculations based on a relative complex rule set. The calucations are to be executed on the following platforms:

  • Android app
  • iOS app
  • Java EE application container

The business logic does not depend on any platform specific details but is strictly about "number crunching".

To avoid double implementation for Java and Objective C, which would require elaborate testing and so forth, I would like to create a Java library cotaining the implementation and use it within the iOS app.

What would be the best way to do this? Would it be better to do a native port for Objective C? Would it be better to do it the other way around and write the library in Objective C and use it on Android/Java?

5 Answers 5

8

There is no way how you can use a Java library in an iOS application. Java requires runtime to interpret (JRE) and you don't have that on iOS.

Technically, you could compile a Java library into native code (there are tools for that) but I don't recommend it.

For sharing bussiness code between Android and iOS the best solution is to use pure C libraries. You can call C libraries from Java using JNI (NDK for Android).

Since Obj-C is only a small object layer over pure C, you can use C libraries easily from Obj-C.

You can compile Obj-C for NDK, too, but it is much more complicated than just using C.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your detailed response. We will probably give j2objc a shot and see if it works. If not, pure C or native implementation in Objective- C will be the next step.
4

You could try parsing the java library to Objective C using j2objc https://code.google.com/p/j2objc/

Update: This Swift Java bridge is in development https://github.com/SwiftJava/SwiftJava/blob/master/README.md

1 Comment

for pure business logic code j2objc works good. There is no support for any UI logic. I have no experience with xmlvm.org I do have experience with using a c library in iOS and Android (using NDK) That works very well.
4

Kotlin Native now has a Swift integration. So with some build process overhead you could share business logic written in Kotlin with iOS and Android.

https://blog.jetbrains.com/kotlin/2017/12/kotlinnative-v0-5-released-calling-kotlin-from-swift-and-c-llvm-5-and-more/

Comments

2

In this case you should write C implementation of your logic and use JNI for android application.

So you wouldn't repeat yourself

http://en.wikipedia.org/wiki/Java_Native_Interface

Comments

1

There is no way you can use any java-related code or application in iOS. You have to work on a native port. iOS doesn't have a jvm.

5 Comments

Would it be better to do it the other way around and write the library in Objective C and use it on Android/Java?
You cannot port objective C to java directly either. Anyway, there is a project in early stage called code.google.com/p/j2objc. It traslates objective C to java. You may give it a try. Anyway, why don't you try some platform-independent development tool like phonegap?
@MohammedHabib How would he share phonegap code with J2EE? Also, phonegap and other similar platform independent frameworks have only limited capabilities.
You cannot share phonegap code with J2EE. I only suggested it to avoid duplicating your code and yes, phonegap has limited capabilities.
Generally, I do re-write everything when creating iOS and Android application.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.