flutter_downloader是Fultter中使用最多的一个下载库,但是作者的文档关于Android端的配置在几个关键部分语焉不详,因为我是前端出身,缺乏安卓开发的常识,导致我在调用的时候反复闪退,后面用adb logcat抓取闪退时的报错,并结合网上关于该库的只言片语解决了闪退的问题,下面将Android端详细的配置流程记录如下:
- 在pubspec.yaml中的dependencies中加入引用
dependencies:
flutter_downloader: 1.3.4
- 在MainActivity.kt同目录的位置增加文件MyApplication.kt
package com.example.flutter
// 上述改为你的package包名,这一行原文档中省略掉了
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
}
}
- 在android\app\src\main\AndroidManifest.xml中增加配置,引用上一步的文件, 即将<application>中的android:name="io.flutter.app.FlutterApplication" 改为android:name=".MyApplication"
此处文档中写的也较为简略,我在初次配置的时候也有出入
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.YOUR_APP_NAME">
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name=".MyApplication"
android:label="YOUR_APP_NAME"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
完成上述步骤,android端的配置就完成了,可以在dart文件中进行使用了,注意在使用之前调用
FlutterDownloader.initialize();
方法进行初始化,否则无法正常执行
网友评论