英文:
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 '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)
}
}
}
答案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 'java-library'
into id 'com.android.library'
See the documentation on creating an Android library for more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论