Flutter查看库的依赖关系

1,432 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

在纯Android项目中我们可以执行Task :app:dependencies(Gradle窗口中的app->Tasks->help->dependencies,选中右键运行)查看依赖关系: 例如:

+--- androidx.multidex:multidex:2.0.1
+--- androidx.annotation:annotation:1.1.0
+--- androidx.appcompat:appcompat:1.1.0
|    +--- androidx.annotation:annotation:1.1.0
|    +--- androidx.core:core:1.1.0
|    |    +--- androidx.annotation:annotation:1.1.0
|    |    +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.2.0
|    |    |    +--- androidx.lifecycle:lifecycle-common:2.2.0
|    |    |    |    \--- androidx.annotation:annotation:1.1.0
|    |    |    +--- androidx.arch.core:core-common:2.1.0
|    |    |    |    \--- androidx.annotation:annotation:1.1.0
|    |    |    \--- androidx.annotation:annotation:1.1.0
|    |    +--- androidx.versionedparcelable:versionedparcelable:1.1.0
|    |    |    +--- androidx.annotation:annotation:1.1.0
|    |    |    \--- androidx.collection:collection:1.0.0 -> 1.1.0
|    |    |         \--- androidx.annotation:annotation:1.1.0
|    |    \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)

而在Flutter中同样也可查看依赖关系。方法是在Android studio中的terminal下输入如下命令

 flutter pub deps

结果示例:

Dart SDK 2.9.0-20.0.dev.flutter-c190fc3a31
Flutter SDK 1.20.0-3.0.pre.130
FlutterDemo 1.0.0
|-- connectivity 0.4.9
|   |-- connectivity_for_web 0.3.0
|   |   |-- connectivity_platform_interface...
|   |   |-- flutter...
|   |   |-- flutter_web_plugins...
|   |   '-- js 0.6.2
|   |-- connectivity_macos 0.1.0+3
|   |   '-- flutter...
|   |-- connectivity_platform_interface 1.0.6
|   |   |-- flutter...
|   |   |-- meta...
|   |   '-- plugin_platform_interface 1.0.2
|   |       '-- meta...
|   |-- flutter...
|   '-- meta...
|-- cupertino_icons 0.1.3
|-- decimal 0.3.5
|   '-- rational 0.3.8

我们查询这个依赖关系无非就是解决不同库之间的引用冲突问题,有了这个依赖关系有时,我们虽然可能可以看到冲突的库是哪个,但是如何解决,不同库之间引用的同一个库不同版本之前的冲突,应该选用哪个版本才能兼顾所有引用它的库。 其实在pubspec.yaml中引用库时,我们在库的版本号处,可以直接写any,这样的话,flutter就自动匹配不冲突的库了。是不是很方便,那么匹配完,我们如何知道到底使用的是哪个版本呢?在工程的根目录中有个.flutter-plugins-dependencies的文件,这个是编译自动生成的。可以看到具体每个库使用的版本号和库在电脑中的实际目录。 类似如下:

# This is a generated file; do not edit or check into version control.
app_settings=/Users/xxx/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/app_settings-4.1.6/
connectivity=/Users/xxx/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/connectivity-3.0.6/
connectivity_for_web=/Users/xxx/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/connectivity_for_web-0.4.0+1/
connectivity_macos=/Users/xxx/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/connectivity_macos-0.2.1+2/
device_info_plus=/Users/xxx/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/device_info_plus-3.2.4/