Android的切换到gradle这个不能编译JRE7 code功能切换到、功能、Android、gradle

2023-09-07 23:04:01 作者:东风寄千愁

我把我的Andr​​oid应用超过到Android工作室无需切换到摇篮。现在我想移动到摇篮。该应用程序在Android中工作室编译切换到摇篮,但现在我已经摇篮全部成立,它不会编译字符串switch语句或钻石商。我得到的错误是

I moved my android app over to Android Studio without switching to Gradle. Now I want to move to Gradle. The app compiles in Android Studio before switching to Gradle, but now that I have Gradle all set up, it won't compile the String Switch Statements or the diamond operators. The error I am getting is

Gradle: error: strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)

我已经确定,我在JRE 7通过打印运行

I have made sure that I am running on JRE 7 by printing the

System.getProperty("java.version")

中的任务。输出

1.7.0_25

最令我困惑的是-source 1.6和使用-source 7的差异。但我知道,这两种都是Java SDK的的名字,所以也许标题只是被混淆。

What confuses me most is the discrepancy between "-source 1.6" and "use -source 7". But I know that both of these are names for Java sdk's so maybe the titles are just being mixed up.

是否有摇篮的设置,我需要设置?还是在摇篮这是不可能的?如果不是,是混淆了为什么它工作没有摇篮。

Is there a Gradle setting I need to set? or is this not possible in Gradle? If not it is confusing why it works without Gradle.

应该注意的是,没有摇篮版本我的项目运行默认的Andr​​oid Studio生成。我没有写一个Ant脚本或Maven脚本编译。其中之一可能是它正在修建的方式,但我没有任何项目特定的文件他们。就在Android的工作室.iml文件。

It should be noted that the without Gradle version of my project runs the default Android Studio build. I didn't write an ant script or maven script for building it. One of those may be the way it is being built, but I don't have any project specific files for them. Just the Android Studio .iml files.

更新我尝试添加以下的Andr​​oid版的build.gradle {}栏目

UPDATE I tried adding the following to the build.gradle android{} section

compileOptions {
   sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
   targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
}

但.class文件未能建立,这是不包括在APK。请参阅Android的项目需要使用Java 1.6编译库部分在这个帖子

推荐答案

您可以通过添加在以下升级现有的Andr​​oid应用程序/库模块到Java 7中的机器人第您的build.gradle

You can upgrade an existing Android app/library module to Java 7 by adding the following in the android section of your build.gradle:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

对于Java(非Android)模块,你应该只在添加以下的build.gradle 应用插件:Java的

For a Java (non-android) module, you should add the following to build.gradle just after apply plugin: 'java':

sourceCompatibility = 1.7
targetCompatibility = 1.7

对于这两种类型的模块,您将需要手动更改文件项目的语言水平 - >项目结构 - >项目(或者你可以手动编辑 .idea /杂项的配置。 XML JDK_1_6 JDK_1_7 )。

您可能还需要以下添加到 .idea / compiler.xml <成分名称=CompilerConfiguration> 块(但请你上车没有它在前):

You might also need to add the following to .idea/compiler.xml, in the <component name="CompilerConfiguration"> block (but see how you get on without it first):

<bytecodeTargetLevel target="1.7" />