【问题标题】:Gradle cannot find Android Compose CompilerGradle 找不到 Android Compose 编译器
【发布时间】:2021-01-30 07:00:51
【问题描述】:

我对这个问题感到非常困惑。我的 gradle 文件中有以下几行:

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"

但是,当我构建时,我收到以下错误:

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

我对此感到困惑,因为编译器确实存在于 Google 的存储库中:https://maven.google.com/web/index.html#androidx.compose.compiler:compiler

如有必要,我可以发布更多信息,但我想保持简单。即使compose设置完全错误,为什么找不到POM文件?

【问题讨论】:

  • 忘了说明我使用的是 Android Studio 4.2,Canary 13
  • 您使用的是哪个版本的com.android.tools.build:gradle?这也是4.2.0-alpha13吗?
  • @ianhanniballake 是的,是的
  • @MarkHerscher 有什么更新可以解决这个问题吗?我也停留在那个时候:(

标签: android android-gradle-plugin android-jetpack-compose


【解决方案1】:

更新 gradle 版本后遇到同样的问题并解决

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

而不是在 composeoptions 中声明编译器版本 kotlinCompilerExtensionVersion,而是作为依赖项声明

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

【讨论】:

    【解决方案2】:

    我必须升级我的 Gradle 版本:

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

    【讨论】:

      【解决方案3】:

      我遇到了一个非常相似的问题,但是在 IntelliJ IDEA 中运行 Android 项目时,而不是 Android Studio。

      我似乎升级 com.android.tools.build:gradle 依赖项确实解决了原来的问题(“Gradle 找不到 Android Compose 编译器”),但后来我开始有 other problems,所以不得不另谋出路。

      我使用了 IDEA 的新项目向导,然后选择了 Kotlin,然后选择了“Multiplatform”(在 Jetpack Compose for Desktop 组下)。这创建了一个新项目,其中包含一个 Android 模块、一个桌面模块和一个通用模块。通过这个模板,我基本上将 Android 和 Common 模块的设置方式复制到了我原来的 Android 项目中。

      主要区别在于 JetBrains 似乎提供了一个(又一个)Gradle 插件(org.jetbrains.compose),它为我们设置了 Compose(我猜是因为一些更改也支持桌面),并且有了这个插件,一切似乎可以工作,并且“Gradle 找不到 Android Compose 编译器”消失了。

      这是新项目模板中使用的插件:

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

      这是添加 Compose 依赖项的方式:

      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")
      

      请记住,我不一定在这里使用桌面,至少现在不是,但这是我可以使 Android 应用与最新(2021 年及以上)版本的 Compose 一起使用的唯一方法,仅使用 IDEA(不是Android Studio) 安装。

      【讨论】:

        【解决方案4】:

        21 年 3 月 27 日

        Jetpack Compose 是一项预览功能,仅在 Canary 版本的 Android Studio 中包含对 Compose 的支持。要在您的应用项目中使用 Compose,请下载并安装最新的 Canary 版本的 IDE。


        如果您使用的是 Canary,只需使用 composeOptions 添加编译器

        build.gradle

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

        compose.compiler

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多