我之前做flutter开发视频拍摄软件的时候遇到过一个androidX编译与运行时不同版本的问题,现在将当时的场景复原,以供大家参考。
引入 image_picker,以实现从手机系统选取视频的功能。安卓端依次报错:
- What went wrong:
Execution failed for task ‘:app:preDebugBuild’.Android dependency ‘androidx.core:core’ has different version for the compile (1.0.0) and runtime (1.1.0-alpha04) classpath. You should manually set the same version via DependencyResolution
- Try:
Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. Run with –scan to get full insights.- Get more help at https://help.gradle.org
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ‘:app:preDebugBuild’.Android dependency ‘androidx.fragment:fragment’ has different version for the compile (1.0.0) and runtime (1.1.0-alpha04) classpath. You should manually set the same version via DependencyResolution
Try:
Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. Run with –scan to get full insights.- Get more help at https://help.gradle.org
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ‘:app:preDebugBuild’.Android dependency ‘androidx.lifecycle:lifecycle-viewmodel’ has different version for the compile (2.0.0) and runtime (2.1.0-alpha02) classpath. You should manually set the same version via DependencyResolution
Try:
Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. Run with –scan to get full insights.Get more help at https://help.gradle.org
sf、flutter issue给出的解决方案有:
https://github.com/flutter/flutter/issues/27254
https://github.com/Sh1d0w/multi_image_picker/issues/78
给出的思路大体是:
- 部分插件未升级androidX、部分已升级androidX,此时需全部升级。pass,我的全是androidX
- 发现版本不一致的依赖,强制使用其中之一。pass,没用
最后,我的解决方案是:
将androidx.legacy
依赖由 api
换为 implementation
。因为api定义的模块依赖是可以被其他模块使用的,而implementation定义的依赖只能在模块内部使用,这样就避免了冲突。
1 |
dependencies { |
分隔线
补充image_picker
几个问题
image_picker同其他插件一样,凡是涉及平台通道的,在两个系统里表现都很难做到完全一致。
- IOS端,image_picker获取的文件带
file://
头部,android端没有,所以拿到文件地址后,如果要创建新的File
对象,需要去掉该头部。 - IOS端,因为权限问题,无法获取应用外部文件的信息。所以需要将文件拷贝至应用内部,再操作。