解析失败:androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

Failed to resolve: androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

我正在尝试理解 android 中的 ViewModel 和 LiveData 概念。我正在做一个练习项目,但是当我在我的应用级别 gradle 文件中添加 implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1' 行时,它显示

Failed to resolve: androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1.

我在 google 上搜索了解决方案,我找到了 答案,当我只编译 viewmodel 库时它有效,但如果我使用与 implementation group: 'androidx.lifecycle', name:'lifecycle-extensions-ktx', version: '2.0.0-alpha1' 相同的方法编译扩展库,它显示与上面相同的错误。 我也试图在 Maven 存储库站点上找到它 here 但我没有关于如何编译它的任何信息。

更新

应用级别Build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "***************"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation group: 'androidx.lifecycle', name:'lifecycle-extensions-ktx', version: '2.0.0-alpha1'
    implementation group: 'androidx.lifecycle', name:'lifecycle-viewmodel-ktx', version: '2.0.0-alpha1'
}

更新

根据 @Dr.jacky 的评论和 Android 开发者文档,

The APIs in lifecycle-extensions have been deprecated. Instead, add dependencies for the specific Lifecycle artifacts you need.

更多信息在 https://developer.android.com/jetpack/androidx/releases/lifecycle


我找到了答案。正如 Thunder Knight 所说 it seems to be that the repository was emptied somehow. Hence, you can not download it from the repository.. I agree with this and so i was looking for answer on mvnrepository.com and i found it here。我必须添加

implementation group: 'androidx.lifecycle', name: 'lifecycle-extensions', version: '2.0.0-alpha1'

用于添加生命周期扩展的行,而且我在库的名称中添加了 -ktx 但这是错误的。在 documentation 中,他们没有评论在 lifecycle-extensions 行中添加 -ktx

致谢:-

只是不要在生命周期扩展中使用 -ktx。与 viewmodel

不同,没有单独的“-ktx”版本的依赖项

当前版本为2.2.0

使用 -ktx 进行 kotlin 实现 和 kapt for annotationProcessor

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0'

查看开发者网站了解更多信息

https://developer.android.com/topic/libraries/architecture/adding-components

添加这些行

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'

并注释这些行

// testImplementation 'junit:junit:4.13.2'
//androidTestImplementation 'androidx.test.ext:junit:1.1.2'
//androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

在我的例子中,我试图将 room 与 LiveData 一起使用。我所做的只是从查询方法中删除暂停关键字。

参考:post in forums.bignerdranch.com

错误:

Failed to resolve: androidx.lifecycle:lifecycle-extensions:2.3.1

解决方案:

The APIs in lifecycle-extensions have been deprecated. Instead, add dependencies for the specific Lifecycle artifacts you need.

更多详情:developer.android.com