使用gradle构建Cordova项目时覆盖Java版本 [英] Override Java version when building a Cordova project with gradle

查看:347
本文介绍了使用gradle构建Cordova项目时覆盖Java版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用gradle作为构建工具来构建Cordova项目。在Cordova项目中,我有自己的需要Java 1.7的插件。



在Cordova附带的build.gradle中,java版本是1.6。
build.gradle:

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

build.gradle有一个伟大的大警告,说它是一个生成的文件,不应该编辑,自定义gradle构建步骤的方式是 - 我理解 - 创建一个build-extras.gradle文件。



我创建了一个build-extras.gradle文件并尝试了以下操作:

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

但它似乎不工作。我在建立专案时仍然收到错误讯息。



错误:

 > -source中不支持开关中的字符串1.6 
> switch(action){
> ^(使用-source 7或更高版本在交换机中启用字符串)

有人可以帮助我如何设置gradle来使这个工作?

解决方案

我一直在试图解决同样的问题,找到答案!无论如何,虽然没有答案,你提到的 build-extras.gradle 让我在正确的轨道,以下现在为我工作...所以感谢。 / p>

首先我想我可能会尝试使用与你相同的语法,以便尝试找出错误。就我可以告诉它失败,因为 build-extras.gradle 文件不是神奇地合并到 build.gradle ,而是使用Gradle apply 方法加载和执行。因为这发生在 android 闭包之前,它在这个过程中太早了,那些 android 的值会覆盖我们的extra '值。



(我没有时间去比Gradle或Groovy更多的内容,所以如果我的术语不精确,请道歉...) / p>

但是,如果我使用 postBuildExtras()方法,我可以使用它。



如果你看看Cordova生成的 build.gradle 文件的底部,你会看到,如果这样一个方法, postBuildExtras )存在于 ext ,然后它被调用。因为这是配置脚本中的最后一个东西,所以我想这个方法的意思是我们可以用它来覆盖任何东西。



因此,我的 build-extras.gradle

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

我实际上在我的调查早期工作,但仍然尝试其他方法,因为我怀疑这种技术将失败,当我们尝试多个 build-extras.gradle 文件 - 由于我们都专门讨论Cordova插件,因此很有可能。



有一种看起来很有前途的插件技术,它使用 plugin.xml中的< framework> 元素,但是使用这种方法我永远不能得到插件特定的模块加载/导入/任何。我将再次看到这个,当我得到插件#2,但现在上面描述的技术让我一个大跃进进一步,我今天早上,所以再次感谢 build-extras .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?

解决方案

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

这篇关于使用gradle构建Cordova项目时覆盖Java版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆