Android Studio 北极狐 | 2020.3.1 Canary 15 |撰写构建错误:找不到 androidx.compose:compose-compiler:1.0.0-beta06 |

时间:2021-05-11 13:19:19

标签: android android-jetpack-compose

正如标题所示,当我尝试在设备上进行部署时,我无法理解问题出在哪里。

当我尝试将 kotlinCompilerExtensionVersion '1.0.0-beta06' 添加到 gradle 时,部署失败 并且错误消息是“无法解析配置的所有文件':app:kotlin-extension'。找不到androidx.compose:compose-compiler:1.0.0-beta06。”

我想要做的是从现有项目开始添加一段用 compose 编写的代码,我正在遵循本指南 https://developer.android.com/jetpack/compose/interop/interop-apis

我将我的gradle粘贴在下面:

    plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-parcelize'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs'
}

android {
    compileSdkVersion rootProject.findProperty("android.compileSdkVersion") as Integer
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId findProperty("android.applicationId")
        targetSdkVersion findProperty("android.targetSdkVersion") as Integer
        minSdkVersion findProperty("android.minSdkVersion") as Integer
        versionCode findProperty("android.minSdkVersion") as Integer
        versionName findProperty("android.versionName")
        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 = JavaVersion.VERSION_1_8.toString()
    }

    buildFeatures {
        dataBinding true
        // Enables Jetpack Compose for this module
        compose true
    }

    composeOptions {
        kotlinCompilerExtensionVersion '1.0.0-beta06'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation "androidx.core:core-ktx:$ktx_version"
    implementation "androidx.legacy:legacy-support-v4:$legacy_version"
    implementation "androidx.constraintlayout:constraintlayout:$constraint_layout_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
    implementation "com.google.android.material:material:$material_version"

    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"

    // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    // Compose
    implementation "androidx.navigation:navigation-compose:$navigation_compose_version"
    // Material Design
    implementation "androidx.compose.material:material:$compose_version"
    // Tooling support (Previews, etc.)
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
    implementation "androidx.compose.foundation:foundation:$compose_version"
    // Material design icons
    implementation "androidx.compose.material:material-icons-core:$compose_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_version"
    // Integration with activitie
    implementation "androidx.activity:activity-compose:1.3.0-alpha07"
    // Integration with ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha04"
    // Integration with observable
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
    implementation "androidx.compose.runtime:runtime-rxjava2:$compose_version"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutine_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"

    // Room
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"

    // Work Manager
    implementation "androidx.work:work-runtime-ktx:$work_version"

    // Timber
    implementation "com.jakewharton.timber:timber:$timber_version"

    // Glide
    implementation "com.github.bumptech.glide:glide:$glide_version"

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

我正在编写时使用的是最新版本:

android.targetSdkVersion=30
android.minSdkVersion=28
android.versionCode=1
android.versionName=1.0.0
# gradle.properties
kotlin_version=1.4.30
gradle_version=4.1.1
ktlint_version=9.2.1
safe_args_version=1.0.0
appcompat_version=1.2.0
ktx_version=1.3.2
legacy_version=1.0.0
constraint_layout_version=2.0.4
lifecycle_version=2.3.1
recyclerview_version=1.2.0
material_version=1.3.0
retrofit_version=2.9.0
moshi_version=1.9.2
navigation_version=2.3.5
navigation_compose_version=1.0.0-alpha10
compose_version=1.0.0-beta06
coroutine_version=1.4.2
room_version=2.3.0
work_version=2.5.0
timber_version=4.7.1
glide_version=4.12.0

我正在遵循官方设置,但似乎没有醒来

https://developer.android.com/jetpack/compose/setup

enter image description here

2 个答案:

答案 0 :(得分:1)

我昨天遇到了这个问题。我所做的是在撰写选项中添加编译器。

composeOptions {

       kotlinCompilerVersion "1.4.32"
       kotlinCompilerExtensionVersion "1.0.0-beta06"
   }

在 stackoverflow here 中发现与您有些类似的问题。您可能需要升级您的 gradle 版本。

还有here,(如果你还没有)在项目gradle中设置ext.kotlin_version = .....

buildscript {
    ext.kotlin_version = "1.4.32"
    ext {  ....   }

    repositories {
        mavenCentral()
        google() 
    }

    allprojects {
        repositories {
            mavenCentral()
            google()
            maven { url "https://jitpack.io" }
        }
    }

}

答案 1 :(得分:0)

终于找到问题了。

在我使用的 build.gradle(项目级别)中:

classpath "com.android.tools.build:gradle:$gradle_version"

所以创建新的 Compose 项目我找到了解决问题的方法。特别是:

build.gradle(项目级别)就像:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0-alpha15"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
        classpath "org.jlleitschuh.gradle:ktlint-gradle:$ktlint_version"
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5")
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

build.gradle(应用级别)就像:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-parcelize'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs'
}

android {
    compileSdkVersion rootProject.findProperty("android.compileSdkVersion") as Integer
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId findProperty("android.applicationId")
        targetSdkVersion findProperty("android.targetSdkVersion") as Integer
        minSdkVersion findProperty("android.minSdkVersion") as Integer
        versionCode findProperty("android.minSdkVersion") as Integer
        versionName findProperty("android.versionName")
        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 = '1.8'
        useIR = true
    }
    buildFeatures {
        dataBinding true
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.4.32'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation "androidx.core:core-ktx:$ktx_version"
    implementation "androidx.legacy:legacy-support-v4:$legacy_version"
    implementation "androidx.constraintlayout:constraintlayout:$constraint_layout_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
    implementation "com.google.android.material:material:$material_version"

    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"

    // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    // Compose
    implementation "androidx.navigation:navigation-compose:$navigation_compose_version"
    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.1'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha07'

    // Kotlin
    implementation "androidx.fragment:fragment-ktx:$fragment_version"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutine_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"

    // Room
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"

    // Work Manager
    implementation "androidx.work:work-runtime-ktx:$work_version"

    // Timber
    implementation "com.jakewharton.timber:timber:$timber_version"

    // Glide
    implementation "com.github.bumptech.glide:glide:$glide_version"

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

并使用嵌入式 JDK 将 JDK 升级到 11

enter image description here

最后将 gradle 升级到 7.0.0

enter image description here

附言

这里是 gradle.properties:

# Common Android settings
android.compileSdkVersion=30
android.applicationId=com.composetest.application
android.targetSdkVersion=30
android.minSdkVersion=28
android.versionCode=1
android.versionName=1.0.0
# gradle.properties
kotlin_version=1.4.32
gradle_version=4.1.1
ktlint_version=9.2.1
safe_args_version=1.0.0
appcompat_version=1.3.0-beta01
ktx_version=1.3.2
legacy_version=1.0.0
constraint_layout_version=2.0.4
lifecycle_version=2.3.1
recyclerview_version=1.2.0
material_version=1.3.0
retrofit_version=2.9.0
moshi_version=1.9.2
navigation_version=2.3.5
navigation_compose_version=1.0.0-alpha10
compose_version=1.0.0-beta06
coroutine_version=1.4.2
room_version=2.3.0
work_version=2.5.0
timber_version=4.7.1
glide_version=4.12.0
fragment_version = 1.3.3