集成 Jetpack Compose 时应用插件请求 [id: 'com.android.application'] 发生异常

An exception occurred applying plugin request [id: 'com.android.application'] while integration Jetpack Compose

我正在尝试使用 Canary 8 实现 JetPack Compose,但每次尝试同步时都会出现此错误 gradle

An exception occurred applying plugin request [id: 'com.android.application']

My app\build.gradle file is below

    plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 21
        targetSdkVersion 30
        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 = '11'
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.4.30'
    }
}
dependencies {
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-alpha06'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

My build.gradle file is below

buildscript {
    ext {
        compose_version = '1.0.0-beta01'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha08'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and this is my gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

根据 documentation,AGP 7.x.x 需要 Gradle 7. 如您所见,您是 gradle-wrapper,您正在使用 Gradle 6.8.2.尝试更新项目以使用 Gradle 7(当前最新版本为 7.0-milestone-2)。您可以手动执行此操作,也可以简单地通过“文件”>“项目结构”执行此操作。

Android Gradle 插件 7 需要 Java 11.

转到 File > Project Structure 并将 JDK 位置更改为 Embedded JDK

我遇到构建缓存问题和错误:

An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > The option 'android.enableBuildCache' is deprecated.
     The current default is 'false'.
     It was removed in version 7.0 of the Android Gradle plugin.

从根 gradle 属性文件中删除 org.gradle.caching=true 解决了这个问题。

对于 Android Studio Arctic Fox (2020.3.1),您可以在此处找到 JDK 设置:

Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JDK

然后将设置更改为“嵌入式 JDK”

对于 Android Studio Artic Fox (2020.3.1) Android Gradle 插件 7 需要 Java 11。 使用短键 Ctr+Alt+Shift+S 或 转到 文件 > 项目结构 并将 JDK 位置更改为嵌入式 JDK。相同的答案@Mariusz

我想你需要做的就是先去:

  1. Android Studio 菜单 -> 首选项 -> 构建、执行、部署
  2. 在 gradle 项目部分,select:Gradle JDK 版本下拉菜单
  3. 它将显示您在设备上安装的所有 JDK // 请参阅右下角的附件图像下拉菜单。
  4. Select JDK 11.

完成,现在您应该可以毫无问题地构建了。

或者,如果您使用 CLI 构建,则将 ~/.zshrc(或 bash 等效文件更改为 Ubuntu,我使用的是 MacOS 11,所以这里是 Zsh,您应该在您的设备上安装 Bash)到 JDK11。修改完Zsh/Bash资源文件后,保存,然后执行命令:

  • $ source ~/.zshrc // 更新变化
  • $ java -version // 检查 java 版本

示例输出 java 版本:

openjdk version "11.0.8" 2020-07-14 LTS
OpenJDK Runtime Environment Corretto-11.0.8.10.1 (build 11.0.8+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.8.10.1 (build 11.0.8+10-LTS, mixed mode)

注意: 我使用的是 MacOS 11.2.1,Android Studio Artic Fox,2020.3.1,Gradle 版本 7.0.2

检查下图以重新定向 JDK 来源:

步骤 1.

第 2 步

第 3 步

你应该可以开始了。