【问题标题】:Integrate Library module built with Kotlin Coroutines dependency issue集成使用 Kotlin Coroutines 依赖问题构建的库模块
【发布时间】:2020-02-14 17:49:49
【问题描述】:

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

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

  1. 从应用模块获取配置
  2. 创建一个实现 CoroutineScope (ContentListingFragment) 的片段
  3. 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'
}

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

【问题讨论】:

    标签: android kotlin android-library coroutine kotlin-coroutines


    【解决方案1】:

    您必须更改库的 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 关键字会将这些依赖项暴露给应用模块

    【讨论】:

      【解决方案2】:

      ****将此代码放入依赖项块中的 build.gradel 文件中,然后单击同步****

      //Kotlin 协程

      implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
      implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0"
      

      【讨论】:

        【解决方案3】:

        如果您不打算在应用程序模块中使用协程,但想在库中使用它,那么使用 api 依赖项的决定不是最好的。这样你的应用模块也将依赖于协程。

        我认为,最好从ContentListingFragment 中删除CoroutineScope 实现并将CoroutineScope 对象创建为ContentListingFragment 成员。

        class ContentListingFragment : Fragment() {
        
            private val job = Job()
            private val coroutineScope = object : CoroutineScope {
                override val coroutineContext: CoroutineContext
                    get() = job + Dispatchers.IO
        
            }
        
            override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
                super.onViewCreated(view, savedInstanceState)
                coroutineScope.launch { ... }
            }
        
            override fun onDestroyView() {
                super.onDestroy()
                job.cancelChildren()
            }
        }
        

        另外,LifecycleScope 形式 lifecycle-runtime-ktx 可能是最简单的解决方案。它将删除CoroutineScope 创建样板代码。

        class ContentListingFragment : Fragment() {
        
            override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
                super.onViewCreated(view, savedInstanceState)
                viewLifecycleOwner.lifecycleScope.launch { ... }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-05-05
          • 1970-01-01
          • 2014-11-18
          • 1970-01-01
          • 1970-01-01
          • 2014-11-24
          • 1970-01-01
          • 2015-01-23
          相关资源
          最近更新 更多