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

Dev*_*raj 12 android android-jetpack-compose

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

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

我的 app\build.gradle 文件在下面

    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'
}
Run Code Online (Sandbox Code Playgroud)

我的 build.gradle 文件在下面

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
}
Run Code Online (Sandbox Code Playgroud)

这是我的 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
Run Code Online (Sandbox Code Playgroud)

Mar*_*usz 26

Android Gradle 插件 7 需要 Java 11。

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


sha*_*nwu 19

检查下图重新定向JDK源:

步骤1。
在此输入图像描述

第2步。
在此输入图像描述

步骤 3.
在此输入图像描述

你应该可以走了。

  • 已经11了所以不,不是解决方案 (7认同)

小智 10

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

Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JDK
Run Code Online (Sandbox Code Playgroud)

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

  • 没什么,还是同样的错误 (4认同)

0xA*_*iHn 5

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

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.
Run Code Online (Sandbox Code Playgroud)

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


Yaq*_*tti 5

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