Gradle plugin and libraries for J2objc development
Made by TOUCHLAB
See other tutorials in menu under “Basic Quickstart / Tutorials”
We’ll be cloning and building a very basic Android project, which shares just a little bit of standard Java code.
The iOS Xcode project that consumes the generated code is ready to go, so all we’ll be doing is making sure the necessary tools are installed and running everything. Not counting download time, assuming no install issues, this whole thing should take 5-10 minutes.
Clone the BasicAndroidSample
git clone https://github.com/j2objcgradle/BasicAndroidSample.git
cd BasicAndroidSample
We can walk through more details later, but to see the plugin function, we’ll run J2objc on the shared Java code, then open and run the project in Xcode.
./gradlew j2objcBuild
Assuming this the first run, the plugin will download a J2objc runtime. It will take some time. You should see download status updates output to terminal.
If everything comes out OK, the next step is to run Cocoapods. The gradle plugin generates a definition file which points at the generated code. Running Cocoapods will wire the generated code to the Xcode project.
cd ios
pod install
Navigate in Finder to the project directory, open the ‘ios’ folder, and double-click ‘ios.xcworkspace’.
Select a simulator to run, and click ‘Run’.
If everything is set up correctly, you should see the app launch.
Watch the setup process here
The plugin and available libraries are currently kept in a bintray maven repository. The repo is at ‘https://dl.bintray.com/doppllib/j2objc’. In general you’ll want to add the repo to both the buildscript and project repository collections. Also add ‘org.j2objcgradle:gradle:0.12.1’ to the buildscript dependencies.
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/doppllib/j2objc' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.j2objcgradle:gradle:0.12.2"
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/doppllib/j2objc' }
}
}
In the app folder’s build.gradle, apply the Gradle plugin
apply plugin: 'org.j2objcgradle.gradle'
At the bottom of the file, add J2objc specific configuration
j2objcConfig {
translatedPathPrefix 'com.kgalligan.basicandroid.shared', 'SH'
translatePattern {
include '**/shared/**'
}
}
In the projects gradle.properties file add:
j2objc_runtime=2.0.6b
The J2objc Gradle plugin can operate in two basic contexts: a separate Java module, and inside an Android project module. Inside a Java module, generally all of the Java code is shared to Objective-C. In an Android module, the shared code needs to be identified in the plugin config.