4

Up until recently I have been using PhoneGap CLI 5.0 with Node 4.1 and Java 1.7. To build Android APKs I created the build-extras.gradle file under platforms/android with the content

ext.postBuildExtras = {
android {
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
allprojects {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }
}

}
}

which allowed me to compile my apps using Java 7. I originally ran into this with Phonegap CLI 6.3.1 about a week ago but have now upgraded to Phonegap CLI 6.3.3 with Node 4.5 and Java 1.8. The same build-extras.gradle file still works just fine. However, I thought I would try VERION_1_8 only to be given the error message

Error: Error code 1 for command: /path/to/app/platforms/android/gradlew 
with args: cdvBuildDebug,-b, /path/to/app/platforms/android/build.gradle,
-Dorg.gradle.daemon=true,-Pandroid.useDeprecatedNdk=true

I had originally switched to Java 7 since it allowed me to use try-with-resources. I dare say Java 8 brings a few other goodies - better handling of time without using Joda-time etc, I suspect - which might well make a fresh transition worthwhile. However, this error leaves me stumped. I am hoping that someone here might be able to tell me what is going on.


I should mention that for good measure I tried the switch to version 1_8 with a freshly created phonegap project, phonegap create newprj --template=hello-world

DroidOS
  • 8,530
  • 16
  • 99
  • 171

1 Answers1

1

To enable Java 8 in Android projects you need to use new Jack compiler. That's the relevant changes to build.gradle:

android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

For more information see the original guide by Google.

Also, additionally take into account that Jack compiler is available starting from Build Tools version 21.1.1. You need to update them to at least this version to be able to use them. See Experimental New Android Tool Chain - Jack and Jill.

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • android { ... defaultConfig { ... jackOptions { enabled true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } – Vigneshwaran T Oct 04 '16 at 13:53
  • Thank you for putting me on the right track here. In my own Phonegap CLI enviornment merely doing this is not enough - it complains about not knowing jackOptions. This will require investigation... – DroidOS Oct 05 '16 at 09:14
  • Quoted from here https://sites.google.com/a/android.com/tools/tech-docs/jackandjill "Jack and Jill are available in Build Tools version 21.1.1, and greater, via the SDK Manager." So, you should check your Build Tools are updated. – Xavier Rubio Jansana Oct 05 '16 at 09:44