I am here to have some clarifications on how j2ObjC works. I've developed my own app on android and now I'm trying to get into the world of IOS with less problems as possible. I heard of j2objc and I would know it's mechanism before I could use it. I know that j2objc leaves the UI creation to the developer but how It works if I should try to convert a java file with declarations of Text views , Recycler views and their adapters, etc? It makes impossible to convert the entire file, deletes the problematic parts or maybe tries to find an equivalent of them? And finally, should i use it to convert all the Java files of the activities or maybe only the simplest java files (Like the ones with constructor's, getters and setters, etc). Thanks to everyone who will take some time to help me :)
2 Answers
+1 to Woodrow's answer as to how J2ObjC works. We chose to do source-to-source because when the project started, Apple seemed very intent on not allowing Java or Java-like languages on iOS: no GC or dynamic code execution, for example. To avoid any AppStore blocking, we generated source code that could be visually inspected for compliance, if necessary. It also helped that developers have an easier time finding and reporting compilation bugs when the output is another language, rather than object code.
I think the real question you're asking, though, is how useful would J2ObjC be in translating an existing Android app. The answer to that is "rarely", unfortunately. J2ObjC's first customer was Inbox by Gmail, which was already designed so that Java code could be shared by its server and Android client, then by its web client using GWT. None of its UI code was shared, but the app was still ~70% shared Java code when it released, since it was designed to share code from its beginning. Smaller apps written specifically for Android are likely to have very little shareable code, as the data models and business logic is embedded in classes that control the UI.
So if you have a smaller app, especially one where the business logic and data models are all kept on the server (or your app doesn't have a server component), then the best approach is a complete rewrite in iOS. That sounds difficult, but the hard part of designing the app is already done in your Android app.

- 1,984
- 11
- 21
-
1it's rare to get an answer straight from the horse's mouth! – Woodrow Barlow Sep 05 '19 at 17:25
-
Ok I will mark this as the correct answer because the answer is less far from my practical problems. Yes I was asking for a question who let me understand how much I would have to do for passing my app to ios. Unfortunately, It will be an hard and challenging experience because i built my android code without knowing that it would have been necessary to develop the same app also on iOs. So thanks for the answer – Fabrizio Cacciapuoti Sep 05 '19 at 17:32
J2ObjC is a transpiler, or "source-to-source compiler".
A normal compiler accepts source code as input and generates an executable binary as output. A transpiler, on the other hand, outputs source code that is in a different form than the input. In this case, J2ObjC inputs Java source code and outputs Objective C source code.
Compilers typically build an internal, intermediate structural representation of the source code called an "abstract syntax tree". The process of building this tree is called "parsing" and part of this includes looking for errors that would prevent the code from compiling -- such as referencing a variable that was never declared. Each language's AST looks a little different based on what features that language has, and each AST is a direct one-to-one reflection of the original source file (meaning it can be reversed).
The way J2ObjC works is by parsing your Java source code, building a Java abstract syntax tree from that source, then making a series of transformations on that tree to turn it into an Objective-C abstract syntax tree. Then it reverse-parses that tree into Objective-C source code files.
The most complex part of a transpiler is usually the rules needed to translate one language's AST into another language. Usually this is done by looking for equivalent syntax features in each language and just substituting them, but sometimes it involves rewriting larges swaths of code or even omitting support certain features of the input language.
As a simple mental exercise, imagine a transpiler that converts C code into Python code. Consider that C offers a switch-case
statement, but Python has no such language construct. In this scenario, a C-to-Python transpiler might replace the switch-case
with a series of if
...elif
statements.

- 8,477
- 3
- 48
- 86
-
Thank you for the answer. It makes me understand the background behind j2objc. Unfortunately, i will not flag this as the correct answer because I wanted to know how it works in specific cases that I've mentioned. But I upvoted your question because it has been useful – Fabrizio Cacciapuoti Sep 05 '19 at 17:29
-
@FabrizioCacciapuoti yes, i see how tball's answer more directly addresses your question. :) good luck in your project! – Woodrow Barlow Sep 05 '19 at 17:30