[TIMOB-27472] Android: Add Java 8 support
GitHub Issue | n/a |
---|---|
Type | Improvement |
Priority | High |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2020-01-30T16:13:49.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 9.0.0 |
Components | Android |
Labels | android, d8, dexer, java, modules |
Reporter | Joshua Quick |
Assignee | Yordan Banev |
Created | 2019-10-16T00:15:23.000+0000 |
Updated | 2020-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();
PR: https://github.com/appcelerator/titanium_mobile/pull/11434
FR Passed. Waiting for Jenkins.
merged to master for 9.0.0
Verified in build: 9.0.0.v20200130071742. Java8 has been fully implemented in Titanium for Android. Checked using testcase, and checking console
Ticket closed.