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

Viewed 31103

I am trying to implement JetPack Compose using Canary 8 but getting this error every time I try to sync 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
8 Answers

Android Gradle Plugin 7 requires Java 11.

Go to File > Project Structure and change JDK location to Embedded JDK.

For Android Studio Artic Fox (2020.3.1), you can find the JDK settings here:

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

Then change the setting to "Embedded JDK"

For Android Studio Artic Fox (2020.3.1) Android Gradle Plugin 7 requires Java 11. use short key Ctr+Alt+Shift+S or Go to File > Project Structure and change JDK location to Embedded JDK. Same Answer @Mariusz Here is Screenshot

Check the following picture to re-direct the JDK source:

Step 1.
enter image description here

Step 2.
enter image description here

Step 3.
enter image description here

And you should be good to go.

I have issue with build cache and the error:

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.

Removing org.gradle.caching=true from root gradle properties file solved the issue.

According to documentation, AGP 7.x.x requires Gradle 7. As seen in you're gradle-wrapper, you're using Gradle 6.8.2. Try updating the project to use Gradle 7 (The current latest version is 7.0-milestone-2). You can do so manually or simply through File > Project Structure.

I think all you need to do is first go to the:

  1. Android Studio menu -> Preferences -> Build, Execution, Deployment
  2. In the gradle projects section, select: Gradle JDK version drop down menu
  3. It will show all the JDKs you have installed on your device // See attached image bottom right drop down menu.
  4. Select JDK 11.

Done, now you should be able to build without issues.

Or if you are using CLI to build, then change the ~/.zshrc ( or bash equivalent file for Ubuntu, I am using MacOS 11 so it is Zsh here, you should be having Bash on your device) to JDK11. After your change to the Zsh/Bash resource file, save it and then execute command:

  • $ source ~/.zshrc // to update change
  • $ java -version // to check java version

Sample output java version:

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)

Note: I am using MacOS 11.2.1, Android Studio Artic Fox, 2020.3.1, Gradle Version 7.0.2

enter image description here

File -> Project Structure -> Module enter image description here

Preferences -> Build, Execution, Deploment -> Build Tools -> Gradle

enter image description here

Worked for me

Related