@qinyun
2018-06-14T11:09:53.000000Z
字数 3649
阅读 1759
未分类
近日,Facebook在GitHub开源了可扩展的移动应用程序调试平台Sonar。
Sonar是iOS和Android上调试移动应用程序的平台。通过桌面界面,就能可视化、检查和控制你的应用程序,它还提供了一系列有用的工具,包括日志查看、交互式布局检查器和网络检查器。
你可以直接使用Sonar,也可以使用插件API来扩展它,通过插件来可视化和调试移动应用程序中的数据,Sonar负责来回发送数据、调用函数以及在移动应用程序上监听事件。
Sonar由两个部分组成:
macOS的桌面应用程序
适用于Android和iOS的原生移动SDK
要使用Sonar,你需要将移动SDK添加到你的应用程序中。
Sonar在桌面应用上不需要任何特定的设置,只需下载应用程序的最新版本并启动它即可。桌面应用程序可用于macOS,并需要在系统上安装Android、iOS开发工具。
一旦启动Sonar和启动模拟器或连接设备之后,你就可以在Sonar中查看设备的日志,如果要查看应用特定的数据,你需要将原生SDK和应用相集成在一起。
Sonar通过JCenter发布,将依赖关系添加到你的build.gradle文件中。
repositories {
jcenter()
}
dependencies {
debugCompile 'com.facebook.sonar:sonar:0.0.1'
}
现在,你可以在你的应用程序中初始化Sonar onCreate,方法如下:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, 0);
if (BuildConfig.DEBUG && SonarUtils.shouldEnableSonar(this)) {
final SonarClient client = AndroidSonarClient.getInstance(this);
client.addPlugin(new MySonarPlugin());
client.start();
}
}
}
要与iOS应用程序集成,你可以使用CocoaPods,将移动Sonar SDK及其依赖项添加到你的Podfile中:
platform :ios, '8.0'
swift_version = '4.1'
target 'MyApp' do
pod 'RSocket', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/iOS/third-party-podspecs/RSocket.podspec?token=ADr9NE_I05Vu8g7oq_g6g_9FLx784NFmks5bJ5LvwA%3D%3D'
pod 'DoubleConversion', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/iOS/third-party-podspecs/DoubleConversion.podspec?token=ADr9NOxtIEmr5ODP9PWq6-sht-Ye6UYGks5bJ5MjwA%3D%3D'
pod 'glog', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/iOS/third-party-podspecs/glog.podspec?token=ADr9NBHbrlbkFR3DQTPzj0CnZdria4jvks5bJ5M3wA%3D%3D'
pod 'Folly', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/iOS/third-party-podspecs/Folly.podspec?token=ADr9NNTjwJ8xqLFwc3Qz3xB3GsCk-Esmks5bJ5NGwA%3D%3D'
pod 'PeerTalk', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/iOS/third-party-podspecs/PeerTalk.podspec?token=ADr9NB8frQTrUWytsMXtdv_P8km7jV_Mks5bJ5NbwA%3D%3D'
pod 'Yoga','~>1.8.1', :modular_headers => true
pod 'Sonar', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/xplat/Sonar/Sonar.podspec?token=ADr9NFO7byH9uAuhGAIEYuoJeBNyBxf6ks5bJ5N8wA%3D%3D'
pod 'SonarKit', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/SonarKit.podspec?token=ADr9NBuYoodM_NeysQg899hkxXw0WZ7Xks5bJ5OVwA%3D%3D'
pod 'SonarKit/SonarKitLayoutComponentKitSupport', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/SonarKit.podspec?token=ADr9NBuYoodM_NeysQg899hkxXw0WZ7Xks5bJ5OVwA%3D%3D'
pod 'SonarKit/SKIOSNetworkPlugin', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/SonarKit.podspec?token=ADr9NBuYoodM_NeysQg899hkxXw0WZ7Xks5bJ5OVwA%3D%3D'
pod 'ComponentKit', :podspec => 'https://raw.githubusercontent.com/facebook/Sonar/master/iOS/third-party-podspecs/ComponentKit.podspec?token=ADr9NNV9gqkpFTUKaHpCiYOZIG3Ev-Hyks5bJ5O-wA%3D%3D'
post_install do |installer|
installer.pods_project.targets.each do |target|
if ['YogaKit'].include? target.name
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = swift_version
end
end
end
end
end
通过运行pod install来安装依赖项,当你打开应用的Xcode工作区文件时,你可以在AppDelegate中导入并初始化Sonar。
#import <SonarKit/SonarClient.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#if DEBUG
SonarClient *client = [SonarClient sharedClient];
[client addPlugin:[MySonarPlugin new]];
[client start];
#endif
...
}
@end
最后,你需要将插件添加到你的Sonar客户端中,具体添加方式,可通过以下链接查看:
https://fbsonar.com/docs/network-plugin.html
https://fbsonar.com/docs/layout-plugin.html
GitHub:
https://github.com/facebook/sonar
入门: