Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

android - Override Java version when building a Cordova project with gradle

I am trying to build a Cordova project using gradle as a build tool. In the Cordova project I have my own plugin that requires Java 1.7.

In the build.gradle that comes with Cordova the java version is 1.6. build.gradle:

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

The build.gradle comes with a great big warning that says it is a generated file that should not be edited, the way to customize the gradle build step is - as I understand - to create a build-extras.gradle file.

I have created a build-extras.gradle file and tried the following:

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

But it does not seem to work. I still get an error when I build my project.

Error:

> strings in switch are not supported in -source 1.6
>         switch (action) {
>                 ^   (use -source 7 or higher to enable strings in switch)

Can someone please help me figure out how to setup gradle to make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...