Gradle cannot find Android Compose Compiler

Viewed 19903

I am very perplexed by this issue. I have the following lines in my gradle file:

implementation "androidx.compose.runtime:runtime:1.0.0-alpha04"
implementation "androidx.compose.compiler:compiler:1.0.0-alpha04"
implementation "androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha04"

However, when I build, I get the following error:

Could not determine the dependencies of task ':app-name-omitted:prepareDebugKotlinCompileTask'.
> Could not resolve all task dependencies for configuration ':app-name-omitted:kotlin-extension'.
   > Could not find androidx.compose:compose-compiler:1.0.0-alpha04.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/androidx/compose/compose-compiler/1.0.0-alpha04/compose-compiler-1.0.0-alpha04.pom
       - https://repo.maven.apache.org/maven2/androidx/compose/compose-compiler/1.0.0-alpha04/compose-compiler-1.0.0-alpha04.pom
       - https://jcenter.bintray.com/androidx/compose/compose-compiler/1.0.0-alpha04/compose-compiler-1.0.0-alpha04.pom
     Required by:
         project :app-name-omitted

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

I am confused by this, because the compiler DOES exist in Google's repo: https://maven.google.com/web/index.html#androidx.compose.compiler:compiler

I can post more information if necessary, but I wanted to keep it simple. Even if compose is set up completely incorrectly, why would it not find the POM file?

7 Answers

Had the same issue and resolved after updating the gradle version to

classpath "com.android.tools.build:gradle:4.2.0-alpha14"

and instead of declaring compiler version kotlinCompilerExtensionVersion in the composeoptions, decalred as a dependency

implementation "androidx.ui:ui-tooling:$compose_version"
implementation "androidx.compose.runtime:runtime:$compose_version"
implementation "androidx.compose.compiler:compiler:$compose_version"

I have recently faced the same issue. Some of the Compose compiler versions support specific Kotlin versions.

Compose Compiler Thanks to @Braian

Compose to Kotlin Compatibility Map

In my case, below version works.

compose_version = '1.3.0-alpha01'

with

kotlinCompilerExtensionVersion "1.2.0-beta03"

Here is my gradle file for reference.

build.gradle(:app)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.hometech.composedemo"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    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'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion "1.2.0-beta03"
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
    implementation 'androidx.activity:activity-compose:1.5.0'
    implementation 'androidx.navigation:navigation-runtime-ktx:2.5.0'

    def nav_version = "2.5.0"
    // Navigation Compose
    implementation("androidx.navigation:navigation-compose:$nav_version")

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

    //Icons
    implementation "androidx.compose.material:material-icons-extended:$compose_version"
}

build.gradle(Project Level)

buildscript {
    ext {
        compose_version = '1.3.0-alpha01'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}

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

RECENT CHANGE IN COMPOSE VERSION '1.3.0-alpha01'

Compiler version

kotlinCompilerExtensionVersion '1.3.0'

Kotlin Version

id 'org.jetbrains.kotlin.android' version '1.7.10' apply false

I had to upgrade my Gradle version:

buildscript {
    dependencies {
         classpath "com.android.tools.build:gradle:7.0.0-alpha02"

I was having a very similar problem, but when running an Android project in IntelliJ IDEA, not Android Studio.

I seems that upgrading the com.android.tools.build:gradle dependency indeed fixed the original problem ("Gradle cannot find Android Compose Compiler"), but then I started having other problems, so had to find another way.

I used IDEA's new project wizard, then selected Kotlin, then "Multiplatform" (under Jetpack Compose for Desktop group). This created a new project with an Android module, a Desktop module and a Common module. From this template, I basically copied how the Android and Common modules are setup into my original Android project.

The main difference is that JetBrains seems to provide a (yet another) Gradle plugin (org.jetbrains.compose) that sets up Compose for us (because of some changes to also support desktop, I guess), and with this plugin, everything seems to work and the "Gradle cannot find Android Compose Compiler" is gone.

This is the plugin used in the new project template:

id("org.jetbrains.compose") version "0.3.0"

This is how Compose dependencies are added:

api("androidx.core:core-ktx:1.3.2")

api(compose.runtime)
api(compose.foundation)
api(compose.material)
api(compose.ui)

implementation("androidx.activity:activity-compose:1.3.0-alpha03")

Keep in mind that I'm not necessarily using Desktop here, at least not for now, but that was the only way I could make an Android app work with the latest (2021+) releases of Compose with just IDEA (not Android Studio) installation.

The mistake you made is that compiler has the same version as Compose version, therefore gradle can't find it, it should have the version of kotlin, because the compiler is tight to kotlin not compose

Solution 1

implementation "androidx.compose.compiler:compiler:$kotlinVersion"

Solution 2

Remove compiler dependency

implementation "androidx.compose.compiler:compiler:1.0.0-alpha04"

instead declare compiler version in composeOptions, and the compiler will be automatically fetched for you

composeOptions {
        kotlinCompilerExtensionVersion = composeVersion
        kotlinCompilerVersion = kotlinVersion
    }

You should add the compiler version to the app's build.gradle:

android {
    ...
    composeOptions {
        kotlinCompilerExtensionVersion '1.3.0'
    }
}

The version should match the Kotlin version, for example Kotlin 1.7.10 should go with compose compiler 1.3.0. Please refer to this map to find the correct matching: https://developer.android.com/jetpack/androidx/releases/compose-kotlin

March 27 '21

Jetpack Compose is a preview feature, and support for Compose is included only in Canary versions of Android Studio. To use Compose in your app project, download and install the latest Canary version of the IDE.


If you are using Canary simpply add the compiler using composeOptions

build.gradle

android {
    // ...
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion "1.0.0-beta03"
    }
}

compose.compiler

Related