Override Java version when building a Cordova project with gradle

Regarding the answer of Mark, i got a solution to add the build-extras.gradle into the plugin.xml file. So it will be added automatically to the platform folder to the right place.

<source-file src="src/android/lib/build-extras.gradle" target-dir="src/.." />

I've been trying to solve the same problem, and came here hoping to find an answer! Anyway, although there was no answer, your mention of build-extras.gradle put me on the right track, and the following now works for me...so thanks.

To begin with I thought I might as well try using the same syntax as you, so as to try to work out what was wrong. As far as I can tell it fails because the build-extras.gradle file is not magically merged into build.gradle, but is instead loaded and executed using the Gradle apply from approach. And since this happens before the android closure it is too early in the process, and those android values override our 'extra' values.

(I don't have time to delve more than I need to into Gradle or Groovy, so apologies if my terminology is not precise...)

However, I could get it to work if I used the postBuildExtras() method.

If you look at the bottom of the build.gradle file that is generated by Cordova you'll see that if such a method (i.e., postBuildExtras) exists on ext, then it gets called. Since this is the last thing in the configuration script then I guess the point of this method is that we can use it to override anything.

I therefore ended up with this as my build-extras.gradle:

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

I actually had this working quite early in my investigations but kept trying other approaches since I suspect this technique will fail when we try to have multiple build-extras.gradle files -- which, given that we're both talking specifically about Cordova plugins, is very likely.

There is a technique that looks promising for plugins, which is to use the <framework> element in plugin.xml, but with this approach I could never get the plugin-specific module to load/import/whatever. I'll take another look at this when I get to plugin #2, but for now the technique described above gets me a big leap further on where I was this morning, so thanks again for the build-extras.gradle clue. ;)