neděle 20. prosince 2015

How to convert Android application to iPhone and iPad app

As you may know, our GameStylus game engine started as an app for Android (in Java) and at the beginning of the development we had no idea if or how we will port it to another platform(s) in the future. But after a few months the decision has been made – we definitely had to port this Android application to iOS (and Objective-C). But how? If you are trying to find an answer to the same question, maybe this text with description of our steps could be useful for you.

How to move Android app to iOS, Java to Objective-C

You probably don't need an imagination of a successful novel writer to know, how we started: Yes, at Google.com. Many searches, many keywords and what we got? Many people masquerading themselves as experts, said this simply is not possible. At least not automatically without weeks of hard work. The operating systems (Android, iOS) are very different, you know, Java Virtual Machine, licenses, Apple special API, unique user interface of iPhones and iPads... And Java is very, very different form Objective-C... You know, all this blah, blah, blah...
Then there were other advices, mostly recommending to use Google j2objc project. But mostly theoretical, no real experiences.
If you don't know, what j2objc is, then: j2objc is an open source project from Google that can convert Java code to Objective-C (or, in short, Obj-C) code. And the resulting code should be very good – as good that you can just take it and compile it. Sounds too good to be true?
Google promises that this tool enables Java source to be part of an iOS application's build, no editing of the generated files is necessary. The idea is this: You will create a separate UI (user interface) code for both platforms, but application logic and data models will be the same – for both projects. Yes, this sounds good and we definitely gave it a try...

Is  j2objc the right solution for translating Java to Obj-C?

If you want to use  j2objc (http://j2objc.org/), you have to download it, unzip it and use it the right way – which means you have to put header files and libraries to the right place for your compiler to find them – and to use them. And set the compiler, of course. Why? Because once you translate your project from Java to Objective-C the compiler will need these header files and libraries to create your final app for iOS.
To be absolutely honest: It took a lot of time to go through the whole quite inadequate documentation  of j2objc and through advices of other people to make it work and when we got the final translated Objective-C code we were not as happy as we expected to be. Because the code is not intended to any additional changes. Methods are renamed (the names are comprehensible, but quite long), other methods added and the whole thing is a little bit confusing.
But we must be fair: This is not a problem of this tool – this is our problem. We had some (inadequate) expectations and these were not met. Our idea was to translate the code from Java to Obj-C and then to do some changes to get a nice Obj-C code where we would do as much changes as we now – or in the future – need. But this is not the purpose of this conversion tool.
We were playing with the tool and with the resulting code. Isn't it possible to make some changes and use it for our purpose? To be honest: We would prefer not to use any libraries from Google (as in every code there are bugs and why to add Google's bugs to our bugs?) and to make the code more comprehensible, but at one moment it came clear to us: No, this is not the right way to move our Android app to iOS.

We found the right conversion tool

And then we found another open source tool, four or five years old java2objc (https://code.google.com/p/java2objc/). No, the name may look similar, but it is not the same thing as j2objc. This code is published under Apache license and the purpose is different. The resulting code may be imperfect, but very well usable. This tool works on Java source files instead of class files and attempts to create “well-crafted Objective C code as if written by hand”. And it is very easy to use.
You don't have to do any complicated settings – you just need to run: run xxx.java – from command line, where xxx.java is your Java code which you want to translate to Objective-C. It is very fast - after a few seconds the translated code is in the output folder – named output. You can translate all your files one by one or more Java files at once and then use them in XCode. Without any tedious rewriting of Java to Objective-C you get your app source code in Obj-C.


Polishing of the code

In the documentation for  java2objc you can read that the resulting code probably won't work as you expect – and probably won't compile at all. That was the case for our code – but it was perfectly OK for us. Because to make necessary changes was quite easy.
Sure, you have to change all things which have anything to do with user interface. We rewrote our graphics library, which was quite easy, as the graphics methods in iOS API are simpler than those on Android, sound library (the same case), touch input and file inputs/outputs.
Then we had to correct some methods working with strings and arrays. To make things simpler we decided to use mostly standard C-style arrays, which were perfectly OK for our purposes. Only one thing to solve here – dynamic C-style arrays don't know how big they are so we had to remember this in additional variables.
The whole structure of files is very similar in XCode iOS as on Android so if you use Model-View-Controller architecture on Android it is quite easy to recreate the same with the same files on iOS. Regarding assets there was only one thing we had to convert – graphics formats are the same, but there is no mid file support and ogg file support in Apple iOS API. So we converted mid files to ogg (using great LMMS software) and then used Audacity to convert all ogg files (sounds and music) to aac files.

And that's all... Thanks to  java2objc the whole conversion from Java to Objective-C code took much less time than we expected and while right after the conversion there were many errors  shown in XCode, we could cope with them quite quickly. Let's hope the same tactic can be good for your projects as well.

GameStylus team
(GameStylus is an adventure game engine and editor which can be used by anyone. Try it! You can easily create games for Android and iOS this way.)

Updated information about Android iOS conversion can be found in the future here.

Žádné komentáře:

Okomentovat