Flutter崩溃报告插件flutter_bugly的使用
Flutter崩溃报告插件flutter_bugly的使用
腾讯 Bugly 插件,支持Android/iOS 运营统计、原生异常上报、Flutter 异常上报。应用更新可以使用 pgyer_updater。
一、引入
AndroidX
在 pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter_bugly: lastVersion
Google Play(停止维护)
dependencies:
flutter_bugly_play: lastVersion
Android Support
dependencies:
flutter_bugly:
git:
url: git://github.com/crazecoder/flutter_bugly.git
ref: dev
二、项目配置
iOS 配置
如果 iOS 报错 building for iOS Simulator, but linking in object file built for iOS
时,在 ios/Podfile
的 post_install
下加入:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
end
end
end
Android 配置
在 android/app/build.gradle
的 android 下加入:
lintOptions {
// 如打包出现Failed to transform libs.jar to match attributes
checkReleaseBuilds false
}
defaultConfig {
ndk {
// 设置支持的 so 库架构 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
abiFilters 'armeabi-v7a'
}
}
三、使用
以下是完整的示例代码,展示如何初始化和使用 flutter_bugly
插件进行异常捕获和日志记录。
import 'package:flutter/material.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
void main() {
FlutterBugly.postCatchedException(() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
await FlutterBugly.init(
androidAppId: "your android app id",
iOSAppId: "your iOS app id",
);
// 设置用户信息
FlutterBugly.setUserId("user id");
FlutterBugly.putUserData(key: "key", value: "value");
int tag = 9527;
FlutterBugly.setUserTag(tag);
// 示例:上传自定义异常
try {
throw Exception("This is a test exception");
} catch (e, stackTrace) {
FlutterBugly.uploadException(
type: e.runtimeType.toString(),
message: e.toString(),
detail: stackTrace.toString(),
);
}
// 示例:记录日志
FlutterBugly.log(tag: "TestTag", message: "This is a test log message", level: LogLevel.INFO);
});
// 如果报错 Zone mismatch. 使用下面的方法
runZonedGuarded(() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}, (exception, stackTrace) async {
FlutterBugly.uploadException(
type: exception.runtimeType.toString(),
message: exception.toString(),
detail: stackTrace.toString(),
);
});
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
四、Release 打包(Android)
64-bit
flutter build apk --release --target-platform android-arm64
32-bit(目前配合 armeabi-v7a 可以打出 32 位 64 位通用包)
flutter build apk --release --target-platform android-arm
五、支持属性(Android)
String channel; // 自定义渠道标识
bool autoCheckUpgrade = true; // 自动检查更新开关
bool autoInit = true; // 自动初始化
bool customUpgrade = true; // 设置是否使用 Flutter 自定义窗口,false 为 Bugly 自带弹窗
int initDelay = 0; // 延迟初始化,单位秒
FlutterBugly.setUserId("user id");
FlutterBugly.putUserData(key: "key", value: "value");
int tag = 9527;
FlutterBugly.setUserTag(tag);
更多关于Flutter崩溃报告插件flutter_bugly的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter崩溃报告插件flutter_bugly的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter项目中集成和使用flutter_bugly
插件来进行崩溃报告,可以帮助你更好地监控应用的稳定性和性能。以下是一个基本的集成和使用flutter_bugly
的示例代码案例。
1. 添加依赖
首先,你需要在pubspec.yaml
文件中添加flutter_bugly
的依赖:
dependencies:
flutter:
sdk: flutter
flutter_bugly: ^最新版本号 # 请替换为最新的版本号
然后运行flutter pub get
来安装依赖。
2. 配置Android平台
在android/app/src/main/AndroidManifest.xml
文件中添加必要的权限和Bugly的初始化配置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<!-- 添加必要的权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
...>
<!-- Bugly初始化配置 -->
<meta-data
android:name="com.tencent.bugly.BUGLY_APP_ID"
android:value="你的Bugly App ID" /> <!-- 请替换为你的Bugly App ID -->
</application>
</manifest>
3. 配置iOS平台
对于iOS平台,你需要在ios/Runner/Info.plist
中添加一些必要的配置,以及在AppDelegate.swift
或AppDelegate.m
中进行初始化。
在Info.plist
中添加:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
在AppDelegate.swift
中添加:
import UIKit
import Flutter
import flutter_bugly // 导入Bugly模块
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// 初始化Bugly
Bugly.start(withAppId: "你的Bugly App ID") // 请替换为你的Bugly App ID
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
或在AppDelegate.m
中添加:
#import "AppDelegate.h"
#import <Flutter/Flutter.h>
#import <flutter_bugly/FlutterBuglyPlugin.h> // 导入Bugly模块
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
// 初始化Bugly
[Bugly startWithAppId:@"你的Bugly App ID"]; // 请替换为你的Bugly App ID
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
4. 在Flutter代码中初始化并使用
在你的Flutter项目的main.dart
文件中,初始化flutter_bugly
插件:
import 'package:flutter/material.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
void main() {
// 初始化Bugly
FlutterBugly.init(
androidAppId: '你的Bugly App ID', // 请替换为你的Bugly App ID
iosAppId: '你的Bugly App ID', // 对于iOS,通常与Android相同,但根据需求可不同
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _causeCrash() {
// 故意制造一个崩溃,用于测试Bugly的崩溃报告功能
int.parse('not_a_number');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Demo Home Page'),
),
body: Center(
child: ElevatedButton(
onPressed: _causeCrash,
child: Text('Cause Crash'),
),
),
);
}
}
5. 运行并测试
现在,你可以运行你的Flutter应用,并点击按钮来制造一个崩溃。崩溃信息将会被发送到Bugly服务器,你可以在Bugly控制台中查看详细的崩溃报告。
这个示例展示了如何在Flutter项目中集成和使用flutter_bugly
插件来进行崩溃报告。根据你的实际需求,你可能需要进一步配置和优化Bugly的使用。