【问题标题】:Failed to resolve: androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1无法解决:androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1
【发布时间】:2018-11-26 08:47:39
【问题描述】:

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

无法解决: androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1.

我在 google 上搜索了解决方案,我找到了this 的答案,当我只编译 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'
}

【问题讨论】:

  • 您应该添加google() jcenter()stackoverflow.com/a/50890280/3395198
  • 我已经添加了。
  • 向我们展示您的应用级build.gradle
  • 请看更新@YosiPramajaya
  • 您需要显示顶级 build.gradle,特别是 repositories 块。

标签: android kotlin android-architecture-components android-viewmodel android-livedata


【解决方案1】:

更新

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

生命周期扩展中的 API 已被弃用。相反,为您需要的特定生命周期工件添加依赖项。

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


我找到了答案。正如Thunder Knight 所说的here it seems to be that the repository was emptied somehow. Hence, you can not download it from the repository.。我同意这一点,所以我在mvnrepository.com 上寻找答案,我找到了here。我必须添加

实现组:'androidx.lifecycle',名称:'lifecycle-extensions',版本:'2.0.0-alpha1'

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

学分:- @Thunder Knight

【讨论】:

【解决方案2】:

只是不要在生命周期扩展中使用 -ktx。与 viewmodel 不同,没有单独的“-ktx”版本的依赖项

【讨论】:

    【解决方案3】:

    当前版本是2.2.0

    使用 -ktx 实现 kotlin 和 kapt 用于 annotationProcesssor

    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

    【讨论】:

      【解决方案4】:

      添加这些行

      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'
      

      【讨论】:

        【解决方案5】:

        在我的情况下,我试图将房间与 LiveData 一起使用。我所做的只是从查询方法中删除了暂停关键字。

        参考:post in forums.bignerdranch.com

        【讨论】:

          【解决方案6】:

          错误:

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

          解决方案:

          生命周期扩展中的 API 已被弃用。相反,为您需要的特定生命周期工件添加依赖项。

          更多详情:developer.android.com

          【讨论】:

            猜你喜欢
            • 2018-10-22
            • 1970-01-01
            • 2020-02-11
            • 2021-04-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-26
            • 1970-01-01
            相关资源
            最近更新 更多