简体   繁体   中英

Java Version 1.7 in Visual Studio 2015 Basic Application (Android, Gradle)

I'm attempting to use the diamond operator in a basic android gradle application:

ArrayList<TextView> texts = new ArrayList<>();
TextView  tv = new TextView(this);
tv.setText("Hello World!");
texts.add(tv);

setContentView(texts.get(0));

Unfortunately, this simple test fails to build:

1>  ... error: diamond operator is not supported in -source 1.6
1>          ArrayList<TextView> texts = new ArrayList<>();
1>                                                    ^
1>    (use -source 7 or higher to enable diamond operator)

In the default build.gradle.template, I only see these compile options being set:

compileOptions.with {
    sourceCompatibility=JavaVersion.VERSION_1_7
    targetCompatibility=JavaVersion.VERSION_1_7
}

How do I tell Visual Studio 2015 and/or gradle to use version 1.7 instead of 1.6? (Where is it being told to use 1.6 for that matter?)

Thanks!

I solved this by adding the following inside allprojects in the build.gradle file located in the Visual Studio 2015 project's root directory:

tasks.withType(JavaCompile) { 
    sourceCompatibility = "1.7"
    targetCompatibility = "1.7" 
}

I'm still not sure why it was being set to 1.6 without this, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM