Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-27472] Android: Add Java 8 support

GitHub Issuen/a
TypeImprovement
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2020-01-30T16:13:49.000+0000
Affected Version/sn/a
Fix Version/sRelease 9.0.0
ComponentsAndroid
Labelsandroid, d8, dexer, java, modules
ReporterJoshua Quick
AssigneeYordan Banev
Created2019-10-16T00:15:23.000+0000
Updated2020-01-30T16:13:49.000+0000

Description

*Summary:* Titanium currently support "Java 7" language features. In Titanium 9.0.0, we would like to add "Java 8" language features for the core SDK and all modules. This is needed since some 3rd party Android libraries use the Java 8 language, but we're currently unable to compile them into a module or via hyperloop. *Limitations:* Android has limited support for Java 8 language features which Google documents below. Note that older Android OS versions support less Java 8 features. https://developer.android.com/studio/write/java8-support#supported_features *Recommended Solution:* We don't support Java 8 because Titanium is currently limited to using the "dex" command line tool in the Android SDK which only supports up to Java 7. Google's "d8" command line tool supports Java 8. If we update our Titanium build system to use gradle, then we can easily add support for this. Support can be added via the following "build.gradle" settings...
android {
	compileOptions {
		sourceCompatibility JavaVersion.VERSION_1_8
		targetCompatibility JavaVersion.VERSION_1_8
	}
	// When we add Kotlin support in the future,
	// then we must do the following as well.
	kotlinOptions {
		jvmTarget = "1.8"
	}
}
*Testing:* We should add a Java 8 feature to our core Titanium code to prove that Java 8 compilation works for both the SDK and an app build. In our [TiUIView.java](https://github.com/appcelerator/titanium_mobile/blob/master/android/titanium/src/java/org/appcelerator/titanium/view/TiUIView.java) code, I recommend that we change the following code...
protected void setOnClickListener(View view)
{
	view.setOnClickListener(new OnClickListener() {
		public void onClick(View view)
		{
			fireEvent(TiC.EVENT_CLICK, dictFromEvent(lastUpEvent));
		}
	});
}
...to use a Java 8 lambda as follows...
protected void setOnClickListener(View view)
{
	view.setOnClickListener((clickedView) -> {
		fireEvent(TiC.EVENT_CLICK, dictFromEvent(lastUpEvent));
	});
}
We can then test the above via a Titanium Ti.UI.Button object's "click" event.
var window = Ti.UI.createWindow();
var button = Ti.UI.createButton({ title: "Show Alert" });
button.addEventListener("click", function() {
	alert("Button clicked!");
});
window.add(button);
window.open();

Comments

  1. Yordan Banev 2020-01-15

    PR: https://github.com/appcelerator/titanium_mobile/pull/11434
  2. Satyam Sekhri 2020-01-22

    FR Passed. Waiting for Jenkins.
  3. Christopher Williams 2020-01-23

    merged to master for 9.0.0
  4. Sohail Saddique 2020-01-30

    Verified in build: 9.0.0.v20200130071742. Java8 has been fully implemented in Titanium for Android. Checked using testcase, and checking console
       [INFO]  TiApplication: (main) [342,342] Titanium Javascript runtime: v8
       
    Ticket closed.

JSON Source