如何从另一个模块导入上下文?

huangapple go评论56阅读模式
英文:

How to import Context from another module?

问题

以下是您要翻译的代码部分:

The project has different modules. The one is that usecase module that I need to use Context inside it. When I added to context to constructor I can't import as `import android.content.Context` How can I use this? I tried to add these implementation to gradle.

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'

When I added these, get this error 

> Could not resolve androidx.appcompat:appcompat:1.6.1.

build.gradle(:usecase)

plugins {
    id 'java-library'
    id 'org.jetbrains.kotlin.jvm'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    implementation project(path: ':data')
    implementation project(path: ':entity')
    
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2'

    implementation 'io.reactivex.rxjava3:rxkotlin:3.0.0'

    implementation 'com.github.akarnokd:rxjava3-retrofit-adapter:3.0.0'
}

UploadImageUseCase.kt

class UploadImageUseCase(
    private val requestCreationRepository: DocumentRepository,
    private val context: Context
) : UseCase<UploadImageAction, UploadImageResult> {

    override fun apply(upstream: Observable<UploadImageAction>): ObservableSource<UploadImageResult> {
        return upstream.flatMap { action ->
                val imageToFile = getImageFileFromUri(requireContext(), action.uri[0])
                val requestFile = file.asRequestBody("image/*".toMediaTypeOrNull())
                val filePart = MultipartBody.Part.createFormData("file", file.name, requestFile)
            requestCreationRepository.uploadImage(action).toObservable().map{ result ->
                result.fold(onSuccess = {
                    UploadImageResult.Successful(it.data!!)
                }, onFailure = {
                    UploadImageResult.Error(it)
                })
            }.startWithItem(UploadImageResult.Loading)
        }
    }

}
英文:

The project has different modules. The one is that usecase module that I need to use Context inside it. When I added to context to constructor I can't import as import android.content.Context How can I use this? I tried to add these implementation to gradle.

implementation &#39;androidx.core:core-ktx:1.7.0&#39;
implementation &#39;androidx.appcompat:appcompat:1.6.1&#39;

When I added these, get this error

> Could not resolve androidx.appcompat:appcompat:1.6.1.

build.gradle(:usecase)

plugins {
id &#39;java-library&#39;
id &#39;org.jetbrains.kotlin.jvm&#39;
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
implementation project(path: &#39;:data&#39;)
implementation project(path: &#39;:entity&#39;)
implementation &#39;io.reactivex.rxjava3:rxandroid:3.0.0&#39;
implementation &#39;com.squareup.retrofit2:converter-gson:2.9.0&#39;
implementation &#39;com.squareup.retrofit2:adapter-rxjava2:2.9.0&#39;
implementation &#39;com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2&#39;
implementation &#39;io.reactivex.rxjava3:rxkotlin:3.0.0&#39;
implementation &#39;com.github.akarnokd:rxjava3-retrofit-adapter:3.0.0&#39;
}

UploadImageUseCase.kt

class UploadImageUseCase(
private val requestCreationRepository: DocumentRepository,
private val context: Context
) : UseCase&lt;UploadImageAction, UploadImageResult&gt; {
override fun apply(upstream: Observable&lt;UploadImageAction&gt;): ObservableSource&lt;UploadImageResult&gt; {
return upstream.flatMap { action -&gt;
val imageToFile = getImageFileFromUri(requireContext(), action.uri[0])
val requestFile = file.asRequestBody(&quot;image/*&quot;.toMediaTypeOrNull())
val filePart = MultipartBody.Part.createFormData(&quot;file&quot;, file.name, requestFile)
requestCreationRepository.uploadImage(action).toObservable().map{ result -&gt;
result.fold(onSuccess = {
UploadImageResult.Successful(it.data!!)
}, onFailure = {
UploadImageResult.Error(it)
})
}.startWithItem(UploadImageResult.Loading)
}
}
}

答案1

得分: 4

你的模块是一个Java库,不了解Android相关内容。因此,你必须将你的模块转换为Android库,以获取像上下文等内容。

你需要将 id 'java-library' 改为 id 'com.android.library'

查看创建Android库的文档以获取更多信息。

英文:

Your module is a java library, which doesn't know about Android stuff. Therefore you have to make your module an Android library in order to get things like context, etc.

You have to change id &#39;java-library&#39; into id &#39;com.android.library&#39;

See the documentation on creating an Android library for more information.

huangapple
  • 本文由 发表于 2023年5月22日 20:26:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定