当尝试使用lambda表达式时,我得到了一些Gradle构建错误:
错误:(41,100)错误:-source 1.7不支持lambda表达式(使用-source 8或更高版本启用lambda表达式)
错误:任务':app:compileDebugJava'的执行失败.编译失败; 请参阅编译器错误输出以获取详细信
快速搜索帮助我理解lambda表达式仅支持JDK 1.8,显然我没有使用它.
我的问题是我的计算机上安装了JDK 1.8(我使用Yosemite),在模块设置→SDK位置→JDK位置它说:
但是在设置模块→应用程序→源/目标兼容性中,没有诸如"1.8"之类的选项,只有Java 1.6或1.7的选项.
我知道我可以管理没有匿名功能,但我仍然想解决这个问题.
最近,我们将Jdk版本升级1.6
到1.8
我的一个Java项目中.但是有一些编译或运行时错误,所以我必须升级一些库:
1.9
to1.10
3.x
到4.x
那是因为他们使用的是早期版本的ASM,但仅支持jdk 1.8 5.x
Java表示它是向后兼容的,但为什么原始版本的库无法直接与jdk 1.8一起使用?
我刚刚下载了Google的新Android工作室软件包(适用于Windows的x64),还安装了JDK8(C:\ Program Files\Java\jdk1.8.0_25)(x64版本).
但是在android studio设置中,它不会检测JDK并要求找到它的路径.我找到了它的路径,但它仍然说它无效.
我尝试声明新的环境变量(系统和用户),但我仍然得到相同的错误.
我也试过cmd,它说安装了java和JDK.
之后,我从Oracle重新安装了Windows x64的JDK 8.但android studio没有变化......
请帮忙.!
我得到了"匿名新的runnable()可以替换为lambda"警告与以下代码.
final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
sv.post(new Runnable() {
@Override
public void run() {
sv.fullScroll(ScrollView.FOCUS_DOWN);
}
});
Run Code Online (Sandbox Code Playgroud)
我非常努力地在Google上搜索,似乎是使用lambda表达式重写...
final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
Runnable test = () -> sv.fullScroll(ScrollView.FOCUS_DOWN);
test.run();
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行应用程序时,Android Studio会因错误而停止,如下所示:
Error:(78, 40) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么Android Studio让我使用lambda表达式,即使它无法编译.这是一个错误吗?
此外,我尝试使用gradle-retrolambda,但它很难用于biginner.
由于我无法编译我的代码,我不确定上述lambda expresssion是否正确.
在我看来,IDE不应该抱怨代码无法编译.所以我认为应该抑制使用lambda表达式的警告.但我不知道怎么会......
任何帮助表示赞赏.
如何在android中使用lambda表达式?例如,我在IntelliJ IDEA中编译此代码:
package com.example.myapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test s = () -> {return "Lambda expressions test";};
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Lambda expression")
.setMessage(s.t())
.create();
alertDialog.show();
}
}
interface test {
public String t();
}
Run Code Online (Sandbox Code Playgroud)
但有这样的错误:
Information:Using javac 1.8.0_05 to compile java sources
Information:36 errors
Information:0 warnings
Information:Compilation completed with 36 errors and 0 warnings in 29 sec
Error:Android Dex: [myapp?] UNEXPECTED TOP-LEVEL …
Run Code Online (Sandbox Code Playgroud) 这是我的.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.test.test"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/license.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/notice.txt'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.mcxiaoke.volley:library:1.0.15'
compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.17'
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行我的Android应用时,我收到下一个错误:
错误:任务':app:dexDebug'的执行失败.
com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:进程'命令'/ usr/lib/jvm/java-8-oracle/bin/java''以非零退出结束价值2
当我向gradle文件添加jackson库时,我收到此错误.谷歌搜索一段时间,我发现杰克逊库与Android应用程序兼容,并且它比其他库像gson更快.
我怎么解决呢?我是Android初学者.
正如我在这篇文章中看到的那样,Java 8
现在还没有正式支持Android
.所以我很感兴趣是否可以Android
使用Java 7
和Java
模块(作为依赖项)构建模块Java 8
.
作为一个例子,我正在尝试创建一个Gradle
包含一个Android
模块和一个Java
模块作为依赖项的项目.通过以下compileOptions
两个模块的设置,一切正常.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将模块更改compileOptions
为Java
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error:Execution failed for task ':fc-android:preDexFreeDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 1
Run Code Online (Sandbox Code Playgroud)
那么问题是,实际上可以用Android
编译的1.7
版本和依赖Java
模块编译模块1.8
吗?如果没有,为什么呢?
更新:
Retrolamba为Gradle
(由@Saeed提到)是很好的,但是他们有lambda表达式的唯一支持,所以不能访问 …
我使用android studio,我想使用只能从java 8中获得的lambda表达式,当我尝试使用它时,我得到:
Lambda expression are not supported at this language level.
Run Code Online (Sandbox Code Playgroud)
那么,我怎样才能在AndroidStudio上改变它?
我开始遇到项目问题.每当我将build.gradle文件中的源和目标功能设置为java版本1_8时,我的项目就无法编译.
继承人build.gradle代码
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 22
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "xxxx"
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
}
Run Code Online (Sandbox Code Playgroud)
适用于java 1.6
这里是来自gradle assembleDebug --stacktrace的一些堆栈跟踪
An exception has occurred in the compiler (1.8.0_25). Please file a bug at …
Run Code Online (Sandbox Code Playgroud) 我使用Java 1.8来创建我的jar.
我可以在Java项目中使用它,但在Android项目中,我有以下错误:
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android-tool\JAVA\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD …
Run Code Online (Sandbox Code Playgroud)