Android App Development Hybrid
What is hybrid?
From the user's point of view, hybrid apps and native apps don't have a clear difference. They download or install APK from the App Store, open the app and start using it all the same, and both contain various types of apps, games, social, e-commerce, and more. Most importantly, users simply don't care if you're hybrid or native, they just want to install a favorite app and start using it.
The essential difference between native and hybrid is the different ways of construction, and these differences are mainly embodied in the application framework and language.
The language used by Android native is known to all, primarily Java, and in some cases, C + +. and hybrid in addition to the language native to use, but also need to HTML, JavaScript, CSS and so on. As for which type of language to use more, it depends on the developer's Choice of Mix (Hybrid) in the form and proportions. Since hybrid development requires more language knowledge, why are so many mainstream developers and companies advocating it? A more detailed introduction will be made later.
For the framework, native uses the standard Android Sdk,hybrid, which is actually on top of the Android SDK, but adds a layer that is more suitable for web development. The existing hybrid framework mainly includes, crosswalk, cordova,phonegap and so on.
Why use hybrid?
In general, if we develop the app to be published to each mobile platform, and are used native development, then we need to be familiar with the Platform SDK and language of the technical staff, if the use of hybrid way, then web-based development of the page code can be shared across the platform. Note, however, that if native provides some custom plug-ins to the Web tier, it is necessary to ensure the consistency of each platform interface definition, otherwise the web code will not work on different platforms.
In addition, many times the development of the same page using Web technology will be more time-saving than native.
Of course, using web technology is not always good. First is the performance problem, the general web technology development of the page is always slower than the development of native, which is a large web of the mishap. However, with the ongoing optimization of hardware and browser engine performance, the gap between the two will be less and less, which is one of the important reasons that hybrid or pure web can develop, on the other hand, in the comprehensiveness and capability of the interface, the Web layer is not rich in native layer, However, this problem can be solved by opening up the interface of the web layer and Java layer. In general, it is necessary to use native to write pages that require high or overly complex real-time requirements.
What are the ways of hybrid?
There are probably two major types of hybrid development, not the use of frames and frameworks, and the use of frameworks that can be divided into native blends and web blends.
The non-use framework refers to the developer directly facing the standard Android native interface itself to implement the Web page, in general, mainly with the use of WebView and its related interfaces, in some cases also use the third-party framework provided by the embedded class WebView module, such as the Xwalkview of the crosswalk framework. The advantage of this approach is that the flexibility is greater, and developers can arbitrarily organize their own H5 code and native code. It is more suitable for developers who are familiar with native and can control the various details of the upper and lower layers.
Native blending means that the framework used allows developers to focus on native-related development, while using the Web as a companion. This approach is somewhat similar to the non-applicable framework, except that the Web uses a framework. Suitable for developers who are more familiar with native, but want to use the web framework efficiently.
Web blending refers to the framework used to allow developers to focus on web-related development and to native as a Web plugin. This is a good fit for the web, and the main pages are web-based. Notification of this approach is also the mainstream way forward.
Many hybrid frameworks, such as Crosswalk,cordova, offer a mix of native and web-based development models that developers can choose according to their own and company circumstances.
Do not use a third-party framework
This approach requires both Android native development technology and web development technology. Instead of more in-depth discussion of native or the Web, this article focuses on the combination of the two.
First, use the native method to define an activity or fragment, where the layout file contains WebView. The WebView is the core control that displays our web pages. Here is a demo layout file:
<?XML version="1.0"encoding="Utf-8"?> <linearlayoutxmlns:Android="Http://schemas.android.com/apk/res/android" Android: orientation="Vertical" Android: Layout_width="Match_parent" Android: layout_height="Match_parent">
<webview Android: id="@+id/webview" Android: Layout_width="Match_parent" Android: layout_height="Match_parent"/>
</LinearLayout> |
Then, the activity or fragment gets the WebView instance and initializes it. The initialization here involves the ability of the Web view to be set according to the needs of the developer. For example, support for JavaScript, support for viewport, and so on. See WebView and Web App best practice for more details. Here's an example of setting up a Web view:
|
Mwebview = (WebView) Findviewbyid (r.id. WebView );
Init WebView Mwebview. GetSettings (). setjavascriptenabled (true); Mwebview. GetSettings (). Setusewideviewport (true); Mwebview. Setwebchromeclient (NewWebchromeclient () { Public voidonprogresschanged(WebView View, intProgress) { //Activities and webviews measure progress with different scales. //The progress meter would automatically disappear when we reach 100% mactivity. Setprogress (Progress * +); }
}); Mwebview. Setwebviewclient (NewWebviewclient () { Public voidOnreceivederror(WebView View, intErrorCode, String description, String Failingurl) {
Toast.Maketext(mactivity, "Oh no!"+ Description, Toast.Length_short). Show (); }
});
Mwebview. Addjavascriptinterface (NewObject () {
@JavascriptInterface Public voidstartactivity(FinalString URL) {
Runonuithread (NewRunnable () { @Override Public voidRun() {
Intent Intent =NewIntent (mactivity, Mainactivity.class); Intent.putextra ("url", URL); mactivity. StartActivity (Intent); }
}); }
}, "Android"); Mwebview. Loadurl (Murl);
|
In this example, "Murl" represents the URL to be loaded. In addition, page jumps use native's startactivity to instantiate mainactivity multiple times, this way can make the jump between Web pages have similar native experience, such as customizing various page switch animation, of course, Developers can choose to use the location.href of the web.
Third, add our web code in assets, which can be used only with JavaScript, image, video, CSS, and so on. Here is a demo of the Web Code directory structure:
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/85/F7/wKiom1exMo2Cun3_AACzQqO8MO4935.png-wh_500x0-wm_3 -wmp_4-s_3634196891.png "title=" 3bc2fa94-fe85-4030-9dea-6cc75188430d.png "alt=" wkiom1exmo2cun3_ Aaczqqo8mo4935.png-wh_50 "/>
The code for page1.html is as follows:
<title>Page1</title> <metaname="Viewport"content="Width=device-width, initial-scale=1.0, maximum-scale=1.0, User-scalable=no"/> <linkrel="stylesheet"type="Text/css"href="./css/style.css"/> <scripttype="Text/javascript"src="./script/eventhandler.js"></script>
<body> <p>This is Page1.</p> <buttontype="button"value="Goto Page2"onclick="OnClick (' file:///android_asset/page2.html ')">Goto Page2</button> src="./image/red_night.png"> </body>
|
Finally, the demo program works as follows:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/85/F7/wKioL1exMuGjxJVMAAQnmdIgBHw075.png-wh_500x0-wm_3 -wmp_4-s_144686342.png "style=" Float:none; "title=" A1ac7eab-e143-414f-a319-8b3d776ddcfb.png "alt=" Wkiol1exmugjxjvmaaqnmdigbhw075.png-wh_50 "/>
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/85/F7/wKiom1exNYnA8jDlAATb8gWYGk4807.png-wh_500x0-wm_3 -wmp_4-s_241970703.png "title=" 73eb2f9a-cc18-4341-92fc-7d626ec51d11.png "alt=" Wkiom1exnyna8jdlaatb8gwygk4807.png-wh_50 "/>
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/85/F7/wKioL1exNcGC6zzJAAQQYHhix9E319.png-wh_500x0-wm_3 -wmp_4-s_675977524.png "title=" 813c909e-5a32-4b9b-b1ff-26286fad139b.png "alt=" Wkiol1exncgc6zzjaaqqyhhix9e319.png-wh_50 "/>
The code for this demo can be downloaded from GitHub.
Cordova/phonegap
Cordova is a core of the hybrid world, and is the main driving frame of the famous phonegap. In addition to PhoneGap, there are many other toolsets that can be collaborated with, such as "Ionic", "crosswalk", "Framework 7", etc., which can be said to support the main ecology of hybrid.
Cordova Main Features:
1. The Web engine can be replaced. This guarantees a great deal of flexibility in its operation, such as Cordova, which by default uses the system's own webview, which behaves poorly before Android 4.3, so you can replace the runtime with crosswalk. Crosswalk can pack the latest chromium engine into the APK to provide unified UI rendering and efficient JS execution performance for all Android devices over 4.1.
2. Rich native plug-in, with a rich plug-in shared library, and developers can customize their own plugins.
3. Lightweight, the core framework is very compact, can be said to provide only web and native adhesives, but by the combination with other excellent frameworks, such as FRAMEWORK7, agular.js, etc., can flexibly meet the needs of developers.
PhoneGap is one of the most popular distributions in Cordova, the core of which is Cordova, in addition to some handy debugging and build tools. If you compare Cordova to a Linux kernel, Phonagap is the equivalent of Ubuntu or Redhat.
PhoneGap provides the main tools for testing, GUI development tools (for developers who like the graphical interface), browser-side or mobile-side graphical debugging tools (no need to build apk to see the Web page being written in real time), cloud build ( Developers do not need to install the Android SDK locally) and so on.
For developers who prefer the pure command line or their own custom development tools, it is also more flexible to recommend direct use of Cordova,cordova. But in general, when we develop mobile app, we want to see the effect of Web page in real time, so it is recommended to start with PhoneGap for most cases.
Cordova Official Address: http://cordova.apache.org/
PHONEGAP Official Address: http://phonegap.com/
Crosswalk
Crosswalk is a hybrid framework out of Intel Open Source Technology Center. The framework uses the Chromium engine as its core, and the Web runtime using the Framework app does not use the WebView controls and the underlying rendering engine that comes with the Android system, but rather crosswalk its own engine. Developers can choose to package the crosswalk runtime directly into the APK, or choose to load the app remotely from the first run. The former will cause the package apk larger than the general size of 25MB or so, the latter will cause the first run when the load is relatively slow. Neither is perfect, but crosswalk has its own advantages.
First, using the crosswalk runtime guarantees consistent UI rendering on different Android devices, while the high performance of the Chromium engine ensures that JS execution is much more efficient than other frameworks. for Android 4.1 ~ 4.3 devices, because the system still uses the WebKit kernel, so the rendering and execution of the web is very inefficient, and crosswalk can solve this problem.
Second, the crosswalk can be guaranteed to synchronize with the chromium, and developers can use the most up-to-date features of this excellent engine in time.
Conclusion
|
Frame selection |
Mixed mode |
|
If you do not care about 4.1 to 4.3 of the interface uniformity and performance, want the framework of lightweight, considering the community's active level, then recommend Cordova/phonegap.
If you have to consider 4.1 to 4.3 of users, and have higher requirements for interface rendering and execution efficiency, we recommend crosswalk. Of course, you can also use the CORDOVA/PHONEGAP framework to combine the crosswalk runtime approach.
|
If you are more concerned about the flexibility of the interface, extensibility, underlying performance, framework stability then the recommended native mix.
Web Blending is recommended if you want to take a web-based technology. |
If the performance requirements of the abnormal, want to accurately control the memory network, etc., recommended pure native development.
Hybrid Another problem is that, in most cases, mobile users have become accustomed to the native style of interactive habits, if we want to write pages like traditional PC Web, it is likely that users will not buy, and the traditional Web page style on the mobile side does not really experience is very good. This leads to the need for our designers and developers to use web technology to write an interactive experience like native.
Although, the zero-based use of CSS can also write similar native effect. However, it takes a lot of effort to implement all the pages of the entire application into a set of more harmonized native styles. So, when hybrid is developing, we need a Web UI framework that fits the mobile side. At present the more famous has the framework 7, Ionic,telerik, Onsenui, Famo and so on.
On the merits and demerits of each frame, this article does not do too much introduction. However, judging from the UI style, maturity, usage, this article recommends the Framework 7.
This article is from the "Technical Summary" blog, please be sure to keep this source http://9797337.blog.51cto.com/9787337/1838047
Android App Development Hybrid