Flutter集成open_file插件时出现废弃API警告求助
我在Flutter项目(项目名mohaseb)中集成open_file插件时遇到了警告。执行flutter pub add open_file添加open_file: ^3.3.2依赖后,出现以下警告:
Note: C:\Users\mohammed\AppData\Local\Pub\Cache\hosted\pub.dev\open_file-3.3.2\android\src\main\java\com\crazecoder\openfile\OpenFilePlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
已经在pubspec.yaml中添加了path_provider: ^2.0.15依赖,下面是我的配置文件,求解决这个警告的方法。
pubspec.yaml配置
name: mohaseb description: A new Flutter project. publish_to: 'none' version: 1.0.0+1 environment: sdk: '>=3.0.2 <4.0.0' dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 shared_preferences: ^2.1.1 pdf: ^3.10.4 path_provider: ^2.0.15 open_file: ^3.3.2 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.0 flutter: uses-material-design: true
AndroidManifest.xml配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:label="mohaseb" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> </manifest>
解决方法
1. 升级open_file插件到最新版本
该警告源于旧版本open_file使用了Android已废弃的API,优先尝试升级插件到最新稳定版:
flutter pub upgrade open_file
升级后重新编译项目,查看警告是否消失。
2. 忽略警告(临时方案)
如果升级后警告仍存在,或暂时不想升级,可在Android项目的gradle配置中屏蔽废弃API警告:
- 打开项目中
android/app/build.gradle文件 - 在
android代码块内添加lintOptions配置:
android { // 保留原有配置... lintOptions { disable 'Deprecation' } }
保存后重新编译,即可不再显示该类警告。
3. 替换为维护活跃的替代插件(长期方案)
若open_file插件长期未更新,可改用分支插件open_filex,它修复了API废弃问题:
flutter pub remove open_file flutter pub add open_filex
随后修改代码中的导入语句,将import 'package:open_file/open_file.dart';替换为import 'package:open_filex/open_filex.dart';,调用逻辑无需改动。
内容的提问来源于stack exchange,提问作者mohammed programer

