3

Up until now I have been using Java 8 features in my hybrid Cordova Android project - I have one custom plugin where I use Java 8 features such as try-with-resources. In order to do so all I needed to do, apart from ensuring that JDK 8 was installed, was to create a platforms/android/build-extras.gradle file containing the following

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

Having just setup a new chunkier Ubuntu machine to with the latest version of Android Studio - I use the CLI but find that using Android Studio and SDK Manager is the easiest way of ensuring that the Android platforms, tools etc are correctly instatlled - I did the following

  • edited platforms/android/cordova/lib/builders/GradleBuilder.js so it emits distributionURL as https\\://services.gradle.org/distributions/gradle-4.4-all.zip
  • edited platforms/android/build.gradle so it uses the latest Android Gradle plugin as specicfied here.

    dependencies { classpath 'com.android.tools.build:gradle:3.1.0' }

  • edited platforms/android/build-extras.gradle, as suggested here, to read

    android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }

At this point I felt that I had done everything necessary to appease both Cordova and Android so I proceeded to issue a cordova clean which reported success followed by cordova build android which comes back with the following

Could not determine the dependencies of task ':compileDebugJavaWithJavac'. 
Could not resolve all task dependencies for configuration 
':debugCompileClasspath'.
Could not resolve project :CordovaLib.
Required by:
project :
> Project : declares a dependency from configuration 'debugCompile' to
configuration 'debug' which is not declared in the descriptor for project 
:CordovaLib.

The stacktrace simply contains this

ANDROID_HOME=/home/droidos/Android/Sdk
JAVA_HOME=/usr/lib/jvm/java-8-oracle
Subproject Path: CordovaLib  
The Task.leftShift(Closure) method has been deprecated... .
WARNING: Configuration 'compile' is obsolete...
WARNING: Configuration 'debugCompile' is obsolete...
WARNING: Configuration 'releaseCompile' is obsolete...
publishNonDefault is deprecated and has no effect anymore....

Switching back to the old style build-extras.gradle file does not work any more either and comes back with the complaint along the lines of I don't know Jack.

I must be missing something here. What is the right way to compile a Cordova Android project to use Java 8 from the Cordova CLI? For completeness I should mention that I am working with Node 8.9.2 and Cordova 7.1.0. Upgrading either beyond that point is causing trouble with the Duktape Javascript scripting engine I am using in the plugin.

DroidOS
  • 8,530
  • 16
  • 99
  • 171

0 Answers0