欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

flutter 的jar包

发表日期:2018-07 文章编辑:小灯 浏览次数:1449

jar包 位于

flutter\bin\cache\artifacts\engine

目录下有几个文件夹,
因为jar包里包含so 所以分为不同平台的

看下面3个类
FlutterApplication
FlutterActivityDelegate
FlutterView

FlutterApplication.java

onCreate(){ FlutterMain.startInitialization(this); } // 主要调了 (){ initConfig(applicationContext); initAot(applicationContext);initResources(applicationContext); } 

看到那些字符窜有没有什么想法?

private static void initConfig(Context applicationContext) { try { Bundle metadata = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), 128).metaData; if (metadata != null) { sAotSharedLibraryPath = metadata.getString(PUBLIC_AOT_AOT_SHARED_LIBRARY_PATH, "app.so"); sAotVmSnapshotData = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_DATA_KEY, "vm_snapshot_data"); sAotVmSnapshotInstr = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_INSTR_KEY, "vm_snapshot_instr"); sAotIsolateSnapshotData = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_DATA_KEY, "isolate_snapshot_data"); sAotIsolateSnapshotInstr = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_INSTR_KEY, "isolate_snapshot_instr"); sFlx = metadata.getString(PUBLIC_FLX_KEY, "app.flx"); sSnapshotBlob = metadata.getString(PUBLIC_SNAPSHOT_BLOB_KEY, "snapshot_blob.bin"); sFlutterAssetsDir = metadata.getString(PUBLIC_FLUTTER_ASSETS_DIR_KEY, "flutter_assets"); }} catch (NameNotFoundException var2) { throw new RuntimeException(var2); } } 

FlutterActivityDelegate.java

看下他

public void onCreate(Bundle savedInstanceState) { if (VERSION.SDK_INT >= 21) { Window window = this.activity.getWindow(); window.addFlags(-2147483648); window.setStatusBarColor(1073741824); window.getDecorView().setSystemUiVisibility(1280); }String[] args = getArgsFromIntent(this.activity.getIntent());这个方法下面给出来了,看下 FlutterMain.ensureInitializationComplete(this.activity.getApplicationContext(), args);下面是view 的创建,看到 setContentView 是不是很眼熟 this.flutterView = this.viewFactory.createFlutterView(this.activity); if (this.flutterView == null) { FlutterNativeView nativeView = this.viewFactory.createFlutterNativeView(); this.flutterView = new FlutterView(this.activity, (AttributeSet)null, nativeView); this.flutterView.setLayoutParams(matchParent); this.activity.setContentView(this.flutterView);这个可以改加载页面 this.launchView = this.createLaunchView(); if (this.launchView != null) { this.addLaunchView(); } } boolean reuseIsolate = true; if (!this.loadIntent(this.activity.getIntent(), true)) { if (!this.flutterView.getFlutterNativeView().isApplicationRunning()) { String appBundlePath = FlutterMain.findAppBundlePath(this.activity.getApplicationContext()); if (appBundlePath != null) {这里看到 runFromBundle, main 是不是有所联想 this.flutterView.runFromBundle(appBundlePath, (String)null, "main", true); } }} } 

看到下面的字符串有没有什么想法?

public static void ensureInitializationComplete(Context applicationContext, String[] args) { if (Looper.myLooper() != Looper.getMainLooper()) { throw new IllegalStateException("ensureInitializationComplete must be called on the main thread"); } else if (!sInitialized) { try { sResourceExtractor.waitForCompletion(); List<String> shellArgs = new ArrayList(); shellArgs.add("--icu-data-file-path=" + new File(PathUtils.getDataDirectory(applicationContext), "icudtl.dat")); if (args != null) { Collections.addAll(shellArgs, args); }if (sIsPrecompiledAsSharedLibrary) { shellArgs.add("--aot-shared-library-path=" + new File(PathUtils.getDataDirectory(applicationContext), sAotSharedLibraryPath)); } else { if (sIsPrecompiledAsBlobs) { shellArgs.add("--aot-snapshot-path=" + PathUtils.getDataDirectory(applicationContext)); } else { shellArgs.add("--cache-dir-path=" + PathUtils.getCacheDirectory(applicationContext)); shellArgs.add("--aot-snapshot-path=" + PathUtils.getDataDirectory(applicationContext) + "/" + sFlutterAssetsDir); }shellArgs.add("--vm-snapshot-data=" + sAotVmSnapshotData); shellArgs.add("--vm-snapshot-instr=" + sAotVmSnapshotInstr); shellArgs.add("--isolate-snapshot-data=" + sAotIsolateSnapshotData); shellArgs.add("--isolate-snapshot-instr=" + sAotIsolateSnapshotInstr); }if (sSettings.getLogTag() != null) { shellArgs.add("--log-tag=" + sSettings.getLogTag()); }String appBundlePath = findAppBundlePath(applicationContext); nativeInit(applicationContext, (String[])shellArgs.toArray(new String[0]), appBundlePath); sInitialized = true; } catch (Exception var4) { Log.e("FlutterMain", "Flutter initialization failed.", var4); throw new RuntimeException(var4); } } } 

加载界面相关,就是flutter 界面出来之前的界面

private View createLaunchView() { if (!this.showSplashScreenUntilFirstFrame()) { return null; } else { Drawable launchScreenDrawable = this.getLaunchScreenDrawableFromActivityTheme(); if (launchScreenDrawable == null) { return null; } else { View view = new View(this.activity); view.setLayoutParams(matchParent); view.setBackground(launchScreenDrawable); return view; } } } private Boolean showSplashScreenUntilFirstFrame() { try { ActivityInfo activityInfo = this.activity.getPackageManager().getActivityInfo(this.activity.getComponentName(), 129); Bundle metadata = activityInfo.metaData; return metadata != null && //这一窜你会在 Manifesst种看到 //<meta-data// android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"// android:value="true" /> metadata.getBoolean("io.flutter.app.android.SplashScreenUntilFirstFrame"); } catch (NameNotFoundException var3) { return false; } } private Drawable getLaunchScreenDrawableFromActivityTheme() { TypedValue typedValue = new TypedValue();这里的 id=16842836 是 android.R.attr.windowBackground = launch_background.xml if (!this.activity.getTheme().resolveAttribute(16842836, typedValue, true)) { return null; } else if (typedValue.resourceId == 0) { return null; } else { try { return this.activity.getResources().getDrawable(typedValue.resourceId); } catch (NotFoundException var3) { Log.e("FlutterActivityDelegate", "Referenced launch screen windowBackground resource does not exist"); return null; } } }

FlutterView.java

上面看到一个 方法runFromBundle
这里还有另外一个runFromSource
但是是private 的没看到哪里调用到

private void runFromSource(final String assetsDirectory, final String main, final String packages) { Runnable runnable = new Runnable() { public void run() { FlutterView.this.assertAttached(); FlutterView.this.preRun(); FlutterView.this.mNativeView.runFromSource(assetsDirectory, main, packages); FlutterView.this.postRun(); synchronized(this) { this.notify(); } } };try { synchronized(runnable) { this.post(runnable); runnable.wait(); } } catch (InterruptedException var8) { Log.e("FlutterView", "Thread got interrupted waiting for RunFromSourceRunnable to finish", var8); } } 

然后里面还有好几个MethodChannel

public FlutterView(Context context, AttributeSet attrs, FlutterNativeView nativeView) { ... this.mAccessibilityManager = (AccessibilityManager)this.getContext().getSystemService("accessibility"); this.mActivityLifecycleListeners = new ArrayList(); this.mFirstFrameListeners = new ArrayList(); this.mFlutterLocalizationChannel = new MethodChannel(this, "flutter/localization", JSONMethodCodec.INSTANCE); this.mFlutterNavigationChannel = new MethodChannel(this, "flutter/navigation", JSONMethodCodec.INSTANCE); this.mFlutterKeyEventChannel = new BasicMessageChannel(this, "flutter/keyevent", JSONMessageCodec.INSTANCE); this.mFlutterLifecycleChannel = new BasicMessageChannel(this, "flutter/lifecycle", StringCodec.INSTANCE); this.mFlutterSystemChannel = new BasicMessageChannel(this, "flutter/system", JSONMessageCodec.INSTANCE); this.mFlutterSettingsChannel = new BasicMessageChannel(this, "flutter/settings", JSONMessageCodec.INSTANCE); PlatformPlugin platformPlugin = new PlatformPlugin(activity); MethodChannel flutterPlatformChannel = new MethodChannel(this, "flutter/platform", JSONMethodCodec.INSTANCE); flutterPlatformChannel.setMethodCallHandler(platformPlugin); this.addActivityLifecycleListener(platformPlugin); this.mImm = (InputMethodManager)this.getContext().getSystemService("input_method"); ... } 

比如 mFlutterNavigationChannel ="flutter/navigation"
看完你可能已经猜到是 两种语言间的相互调用

public void setInitialRoute(String route) { this.mFlutterNavigationChannel.invokeMethod("setInitialRoute", route); } public void pushRoute(String route) { this.mFlutterNavigationChannel.invokeMethod("pushRoute", route); } public void popRoute() { this.mFlutterNavigationChannel.invokeMethod("popRoute", (Object)null); } 

其实这里你会发现和rn 很像,但他更加彻底些,
rn 和原生的view 掺杂在一起,
而flutter 直接SurfaceView , 脱离了原生的view 体系

看完这几个类你大体知道

1.flutter 加载了那些资源
2.flutter 怎么放到界面上的,通个 setContentView( FlutterView也就是 SurfaceView )
3.flutter界面出来之前的白屏是什么?
4.怎么开始 flutter 的代码 runFromBundle/runFromSource
5.两种语言的通信 通过 MethodChannel/BasicMessageChannel


本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:http://www.dengtar.com/17679.html
相关APP开发
    SQL执行错误,请检查