Sharing code between iOS and Android using j2objc

Uncategorized

From the start the concept seems interesting. Write some code in plain Java, run it though a translator, which will generate some objective-c equivalents calling into the framework’s JRE emulation library. It all seems legit.

So I go and compile the “Hello world” example and yes it works, the excitement is still there,  so let me try something more useful than a “Hello world”. Since I’ve been into “pull to refresh” apps lately, and I need to keep going with those I will try to pull down some JSONs down! Yeah!

So I try a few things in plain Java to download a file. None of them work. The JRE emulation library that implements Java calls as objective-c compatible equivalents has no implementation of HTTPS. The openssl wrapper has not yet been implemented. Neither are the slew of common java libraries, such as apache commons, that make the Java projects happening.

I’ve kept persisting. This time trying to save some data into sqlite3 database. This was another dead end… The POJO’s way of dealing with data persistence is though jdbc and there are no chances of any of the jdbc drivers getting translated – and how can they? They are loaded dynamically! And you can’t do that on iOS.

Conclusion

j2objc is a valid technology to use if you are hitting these points

  • You have amassed tons of Java code that is a pure logic around Java primitives and basic data structures. For example, a computation library of some sort. That is, look at the Java package in question, look at all the all the imports it is making, if there are many things outside  import java.xxx then it’s not likely to translate well.
  • You are comfortable with researching supported libraries before coding. If you are to venture outside basic JRE support you would need to first check if there is support of the libraries in question. At the moment of writing this post it’s not clear how to determine if a specific jar is supported by the project.
  • You have no intentions of learning languages other than objectve-c or Java

In short, I would reuse this technology to avoid re-coding a Java library that does nothing but numbers, strings and hash tables manipulation. And there are many of use cases like this and yours may be one of them.

But this is not what I needed for my simple “pull to refresh” world so I kept looking.

Next: Xamarin for iOS and Android

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*