无法访问 Flutter 项目中的类“com.google.common.util.concurrent.ListenableFuture”

Kar*_*zel 15 android gradle guava flutter flutter-dependencies

在向我的项目添加一些依赖项+将camerax依赖项本机添加Flutter项目的android部分之后(我想创建本机Android视图,然后在Flutter中显示它)。构建项目时,我\xe2\x80\x99ve遇到错误:

\n
e: pathandroid/camera/AndroidCameraView.kt: (35, 58): Cannot access class \'com.google.common.util.concurrent.ListenableFuture\'. Check your module classpath for missing or conflicting dependencies\n        e: pathandroid/camera/AndroidCameraView.kt: (36, 9): Cannot access class \'com.google.common.util.concurrent.ListenableFuture\'. Check your module classpath for missing or conflicting dependencies\n        e: pathandroid/camera/AndroidCameraView.kt: (36, 30): Unresolved reference: addListener\n        e: pathandroid/camera/AndroidCameraView.kt: (37, 30): Cannot access class \'com.google.common.util.concurrent.ListenableFuture\'. Check your module classpath for missing or conflicting dependencies\n        e: pathandroid/camera/AndroidCameraView.kt: (37, 51): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:\npublic operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text\n
Run Code Online (Sandbox Code Playgroud)\n

那些指向这样的代码:

\n
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)\n    cameraProviderFuture.addListener({\n        cameraProvider = cameraProviderFuture.get()\n        bindPreview(cameraProvider, lifecycleOwner)\n    }, ContextCompat.getMainExecutor(context))\n
Run Code Online (Sandbox Code Playgroud)\n

I\xe2\x80\x99ve 生成​​了依赖关系树,并且在多个项目/库中存在如下行:

\n
com.google.guava:guava:28.1-android -> 31.0.1-android\ncom.google.guava:listenablefuture:1.0 -> 9999.0-empty-to-avoid-conflict-with-guava\ncom.google.guava:listenablefuture:{strictly 9999.0-empty-to-avoid-conflict-with-guava} -> 9999.0-empty-to-avoid-conflict-with-guava (c)\n
Run Code Online (Sandbox Code Playgroud)\n

所以我所做的就是添加到 build.gradle 中:

\n
configurations.all {\n        resolutionStrategy {\n            force \'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava\'\n            force \'com.google.guava:guava:31.0.1-android\'\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

在常规的 Android 项目中,我希望它能够工作。但对我来说不幸的是它没有。添加这些行后,仍然出现相同的问题。\n然后我也尝试过:

\n
configurations.all {\n    all*.exclude group: \'com.google.guava\', module: \'listenablefuture\'\n}\n
Run Code Online (Sandbox Code Playgroud)\n

遵循这个答案,并且也尝试过这种方法。什么都没起作用。

\n

我\xe2\x80\x99m想知道它\xe2\x80\x99s是否与Flutter如何构建\xe2\x80\x99s项目有关。它如何构建它传递使用的所有必要的本机依赖项。\n在我的项目 build.gradle 中添加此配置是否足够?或者也许它\xe2\x80\x99s还不够,它不会\xe2\x80\x99t影响从flutter代码\xe2\x80\x9d添加的\xe2\x80\x9c的包?\n是否有一些额外的步骤/处理方法Flutter项目中的依赖冲突?

\n

Kar*_*zel 40

发布问题后,我实际上能够弄清楚。上面提到的SO问题的建议几乎是正确的,但就我而言,我需要添加到本机android依赖项中的app build.gradle是整个番石榴:

implementation "com.google.guava:guava:31.0.1-android"
Run Code Online (Sandbox Code Playgroud)

之后构建成功通过。

  • 感谢您的回答,它对我有用。就我而言,这个问题只是在我实现 CameraX 库时出现的。 (4认同)