【发布时间】:2022-01-10 16:10:07
【问题描述】:
单击运行时收到此消息:
> Failed to apply plugin 'com.android.internal.application'.
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changing `org.gradle.java.home` in `gradle.properties`.
Error message "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8" 在设置中更改 Gradle JDK 的答案有效。 但仅限于那个项目。当我创建一个新项目时,Android Studio 会自动再次使用默认的 1.8。
我有 11 个可用,AS 只是不会自动使用它。 我尝试过的事情:将 JAVA_HOME 环境更改为 JDK 11 的位置,但仍然出现相同的错误。
无效缓存 /restart 一堆时间什么都没做
如何设置一次,应用到以后所有的新项目中?
附加 app/build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.grid"
minSdk 22
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}
顶级 build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
【问题讨论】:
-
你能显示你的 build.gradle 文件吗?
-
@SyedAffanHamdani 当然,添加了 gradle 文件
-
``` kotlinOptions { jvmTarget = '1.8' }``` 是问题所在。将其更改为:
kotlinOptions { jvmTarget = '11' }应该修复它。 -
您找到解决方案了吗? JAVA_HOME 设置为 JDK 版本 13,但每次我创建一个新项目时,我都会收到消息
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.,我需要在 IDE 设置中更改它。我在 macOS 上使用 Android Studio Bumblebee | 2021.1.1 补丁 1。 -
我安装了 2 个 Android Studio。 Android Studio 北极狐和 Android Studio 4.1。我想正因为如此,我有 2 个版本的 Gradle JDK。我删除了 Android Studio 4.1,它解决了这个问题。我猜当您安装新版本的 Android Studio 时,它会在 Application/Android Studio 2 下安装 Gradle JDK。并且可能是...... Android Studio 中的下拉菜单按字母顺序拉出所有可用的 JDK。我不知道这只是我的一个疯狂的理论。
标签: android android-studio gradle android-gradle-plugin