android - 集成使用 Kotlin Coroutines 依赖问题构建的库模块

标签 android kotlin android-library coroutine kotlin-coroutines

我正在构建一个使用协程的库模块。

我的图书馆执行以下操作:

  • 从应用模块获取配置
  • 创建一个实现 CoroutineScope (ContentListingFragment) 的 fragment
  • ContentListingFragment 处理从网络获取数据并显示它们的所有过程

  • 从应用程序模块:
    我们应该能够从 ContentListingFragment 中获取一个实例并将其添加到容器中

    问题:当我构建应用程序时,我收到以下错误:
    Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
        class nabed.apps.nabedutilslibrary.ui.base.ContentListingFragment, unresolved supertypes: kotlinx.coroutines.CoroutineScope 
    

    下面是库模块 build.gradle
    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: "kotlin-kapt"
    apply plugin: 'androidx.navigation.safeargs'
    
    android {
        compileSdkVersion 28
        buildToolsVersion "29.0.0"
    
    
        defaultConfig {
            minSdkVersion 21
            targetSdkVersion 28
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            consumerProguardFiles 'consumer-rules.pro'
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    
        tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    
    androidExtensions{
        experimental = true
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.1.0'
    
        // Navigation
        implementation "android.arch.navigation:navigation-fragment:$navigation_version"
        implementation "android.arch.navigation:navigation-ui:$navigation_version"
        implementation "android.arch.navigation:navigation-fragment-ktx:$navigation_version"
        implementation "android.arch.navigation:navigation-ui-ktx:$navigation_version"
    
    
        //Kotlin Coroutines
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1"
    
    
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }
    

    如何在不更改应用程序模块的情况下处理此问题?

    最佳答案

    您必须更改库的 build.gradle 中的依赖项声明:

    //Kotlin Coroutines
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1"
    
    api关键字会将这些依赖项暴露给应用程序模块

    关于android - 集成使用 Kotlin Coroutines 依赖问题构建的库模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58438859/

    相关文章:

    hibernate - @Autowired lateinit 属性 '' 尚未初始化

    java - 从 Kotlin 实现 Java 接口(interface)时出现 NullPointerException

    ADT17 中带有外部库的 Android 测试项目

    java - 我的 Android 库中的包结构更好

    android - Google 的侧载检查文档发生了什么变化?

    android - 如何在android中将drawable设置为ListView

    android - 创建新文件时权限被拒绝错误安卓系统

    android - 在模块(android 库)中使用改造 2.4.0 时出现 NoClassDefFoundError

    java - 如何修复 'android.os.NetworkOnMainThreadException' ?

    java - Libgdx 角色运行动画未显示