我之前做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 //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 //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 //help.gradle.org
sf、flutter issue給出的解決方案有:
//github.com/flutter/flutter/issues/27254
//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端,因為權限問題,無法獲取應用外部文件的信息。所以需要將文件拷貝至應用內部,再操作。