Converting Java to ObjC: J2ObjC - Digital Solutions, IT Services & Consulting - Payoda

Converting Java to ObjC: J2ObjC

J2ObjC is an open-source tool developed by Google, which allows sharing the of code between iOS and Android. The Java extension file is reused in iOS where J2ObjC converts java classes to objective-c classes. For example, when you return an array in a method the j2objc will translate the Java code to return a subclass of iOSArray where the objective-c gets compiled by the Xcode

Requirements

  • OS X 10.11 or higher
  • Xcode 10 or higher
  • JDK 1.8 or higher

JDK 1.8 is recommended than the latest versions

Installation

  • Download graalvm-ce from https://github.com/graalvm/graalvm-ce-builds/releases/ and paste it in a specific directory
  • Open terminal and enter the command ‘nano .bash_profile’ and paste the below commands
  • export GRAALVM_HOME=<path to graalvm folder>/graalvm-ce-java11-<version>
  • export JAVA_HOME=$GRAALVM_HOME
  • export PATH=$JAVA_HOME/bin:$PATH
  • Download j2objc from https://github.com/google/j2objc/releases. And paste it inside the project directory so that the file needs to accessed inside Xcode

Usage

Open or create a project in Xcode and follow the below steps

  1. Go to theBuild Settings tab and search for Other Linker Flags and add -ljre_emul -ljre_util -liconv

This will connect to the JRE emulation library.

2. In the Build Settings hit the + (near the search bar) and select Add User-Defined Setting.

Name the setting as J2ObjC_HOME and set the value to the J2objC folder

3. Set Path Globally

Open Xcode preferences and navigate to the Locations tab and search for a subtab named Custom Paths

Hit + button and add path along with path field of j2objc

4. Update the Search path in build settings as per the below image

5. Create a folder called ‘shared’ in the project directory and paste the java code in the shared folder (example: sharededcode.java)

6. Navigate to the Build Rules tab.

Hit the + icon on top and change the following below

  • Select Process: Java source files
  • Select Using: Custom script:

Paste the following as below

if [ ! -f “${J2ObjC_HOME}/j2objc” ]; then echo “J2ObjC_HOME not correctly defined in Settings.xcconfig, currently set to ‘${J2ObjC_HOME}’”; exit 1; fi;”${J2ObjC_HOME}/j2objc” — swift-friendly -v -g -d ${DERIVED_FILE_DIR} -sourcepath “${PROJECT_DIR}/shared” — no-package-directories ${INPUT_FILE_PATH};

Modify the option -sourcepath to based on the shared java code repository

Add to the Output Files below the script part: ${DERIVED_FILE_DIR}/${INPUT_FILE_BASE}.h and ${DERIVED_FILE_DIR}/${INPUT_FILE_BASE}.m

Add to the -fno-objc-arc next to the .m entry

7. Create an object and access the functions in the sharedjavacode.java

Benefits

  • Rather than writing code separately for each platform, developers can reuse the code for both Android and iOS
  • J2Objc is easy to implement in the iOS applications
  • This achieves faster application development and cost reduction

Leave a Reply

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

16 − fourteen =