You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WorkManager+Retrofit下载图片,Result.Success触发跨库组调用警告求因

WorkManager中Result.Success触发访问限制警告的原因及解决方法

问题详情

错误提示:

Success can only be called from within the same library group (referenced groupId=androidx.work from groupId=My Composable)

触发警告的代码片段:

Result.Success( // 此处触发警告
    workDataOf(
        WorkerConstants.IMAGE_URI to file.toUri().toString()
    )
)

完整的DownloadWorker类代码:

import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.gk.mycomposable.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import kotlin.random.Random

class DownloadWorker(
    private val context: Context,
    private val workerParam: WorkerParameters
) : CoroutineWorker(context, workerParam) {
    override suspend fun doWork(): Result {
        startForegroundService()
        delay(5000L)

        val response = FileAPI.instance.downloadImage()
        response.body()?.let { body ->
            return withContext(Dispatchers.IO) {
                val file = File(context.cacheDir, "image.jpg")
                val outputStream = FileOutputStream(file)
                outputStream.use { stream ->
                    try {
                        stream.write(body.bytes())
                    } catch (e: IOException) {
                        return@withContext Result.failure(
                            workDataOf(
                                WorkerConstants.ERROR_MSG to e.localizedMessage
                            )
                        )
                    }
                }
                Result.Success( // This is creating problem
                    workDataOf(
                        WorkerConstants.IMAGE_URI to file.toUri().toString()
                    )
                )
            }
        }
        if (!response.isSuccessful){
            if (response.code().toString().startsWith("5")){ 
                return Result.retry()
            }
            return Result.failure(
                workDataOf(
                    WorkerConstants.ERROR_MSG to "Network error"
                )
            )
        }
        return Result.failure(
            workDataOf(
                WorkerConstants.ERROR_MSG to "Unknown error"
            )
        )
    }

    private suspend fun startForegroundService() {
        setForeground(
            ForegroundInfo(
                Random.nextInt(),
                NotificationCompat.Builder(context, WorkerConstants.channelId)
                    .setSmallIcon(R.drawable.skull)
                    .setContentText("Downloading...")
                    .setContentTitle("Download in progress")
                    .build()
            )
        )
    }
}

原因分析

Result.Success是WorkManager库内部的私有实现API,被@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)注解标记,仅允许groupId为androidx.work的库内部代码调用。你的项目groupId(My Composable)不属于这个范围,因此触发了访问限制警告。

WorkManager的设计逻辑是:将大写的Result.Success/Failure作为内部细节,对外提供的公开API是小写的Result.success()/Result.failure()等方法。

解决方法

将触发警告的Result.Success替换为公开的Result.success()方法,同时统一代码中其他返回结果的写法:

修改后的代码片段:

Result.success(
    workDataOf(
        WorkerConstants.IMAGE_URI to file.toUri().toString()
    )
)

另外,检查代码中其他返回失败的部分,确保使用Result.failure()(而非Result.Failure()),Result.retry()本身是公开API,无需修改。

这样修改后,警告会消失,代码也符合WorkManager的官方API使用规范。

内容的提问来源于stack exchange,提问作者Saurabh Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 18:17:46