hybrid
webrtc
hybrid
WebView
webview
webkit
chromium
webrtc

Currently, the browser kernel that can be integrated mainly includes Tencent X5 service and Intel CrossWalk Firefox_Focus Chromium

This article mainly explains the integration of Tencent X5 browser. It has to be said that the official document describes too little. If you encounter problems, you can go to the X5 forum to search for relevant problems.

Tencent X5 kernel is also based on Chromium, compared with the native WebView, optimized the user’s browsing experience. SDK is the X5 kernel that has been downloaded by sharing wechat, QQ, Qzone and other software on the user’s mobile phone. The SDK is small and easy to integrate with just a few lines of code.

integration

First download the SDK from the official website and import the AAR and SO files required into our project.

X5 kernel does not provide 64-bit so files for the time being,so can only keep the “armeabi” folder, which is backward compatible when the phone runs.

Import the SDK

Add to build.gradle in module

// The SDK only provides the armeabi folder and the corresponding so file NDK {abiFilters"armeabi"."armeabi-v7a"."x86"."mips" 
        }
Copy the code
// Add aar file repositories {flatDir {dirs 'libs'} dependencies {compile(name: 'aar')}Copy the code
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Copy the code
Initialize the X5 service
// Initialize in application
@Override
    public void onCreate(a) {
        super.onCreate();
        
        // Collect local TBS kernel information and report it to the server. The server returns the result to decide which kernel to use.
        QbSdk.PreInitCallback callback = new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg) {
                //x5 kernel initialization completed callback,
                // true indicates that the X5 kernel is loaded successfully.
                // false indicates that the X5 kernel fails to be loaded and the system kernel is automatically switched to.
            }

            @Override
            public void onCoreInitFinished(a) {}}; QbSdk.initX5Environment(getApplicationContext(), callback); }Copy the code
Add a WebView to your layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressbar"
        style="? android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="3dp" />

    <com.tencent.smtt.sdk.WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Copy the code
Initialization of the Webview
public class MainActivity extends AppCompatActivity {
    private ProgressBar progressBar;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = findViewById(R.id.progressbar);
        webView = findViewById(R.id.webview);
        initWebView();
    }

    private void initWebView(a) {
        WebSettings settings = webView.getSettings();           // Same as the system webView
        settings.setJavaScriptEnabled(true);                    // Support Javascript and JS interaction
        settings.setJavaScriptCanOpenWindowsAutomatically(true);// Support to open new Windows through JS
        settings.setAllowFileAccess(true);                      // Set access to files
        settings.setSupportZoom(true);                          // Supports scaling
        settings.setBuiltInZoomControls(true);                  // Set the built-in zoom control
        settings.setUseWideViewPort(true);                      // Adaptive screen
        settings.setSupportMultipleWindows(true);               / / window
        settings.setDefaultTextEncodingName("utf-8");            // Set the encoding format
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheMaxSize(Long.MAX_VALUE);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);       // Cache mode
        webView.setWebViewClient(new WebViewClient() {          

            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
// super.onReceivedSslError(webView, sslErrorHandler, sslError);
                sslErrorHandler.proceed();// Ignore SSL certificate error}}); webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView webView, String s, String s1, JsResult jsResult) {
                return super.onJsAlert(webView, s, s1, jsResult);
            }

            @Override
            public void onReceivedTitle(WebView webView, String s) {
                super.onReceivedTitle(webView, s);
            }

            @Override
            public void onProgressChanged(WebView webView, int progress) {
                super.onProgressChanged(webView, progress);
               		if(...). {... progressBar.setProgress(progress);// Set the progress bar. }else{... }}}}); webView.loadUrl("http://www.baidu.com");
// webView.reload(); Refresh the page
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if((keyCode == KeyEvent.KEYCODE_BACK) && webView ! =null && webView.canGoBack()) {
            webView.goBack();
            return true;
        } else{... }}}Copy the code

conclusion

The X5 kernel integration is complete. First time users may use the system kernel while still downloading the X5 kernel. If you have special requirements, you can use static integration (it is hard to update the X5 kernel after integration).

prompt

If the X5 kernel is set to RECORD_AUDIO, the microphone still cannot collect the sound. Later found no add < USES – permission android: name = “android. Permission. MODIFY_AUDIO_SETTINGS” / >

After the addition, the microphone can collect sound normally. This permission is to modify audio Settings. If you have more questions, please feel free to comment in the X5 forum.

Finally, I recommend several tools and sites to check whether the X5 kernel has successfully loaded.

  1. debugtbs
  2. TBS development debugging tool
  3. Detection gadget

The last

For Crosswalk integration please skip to:

Crosswalk with Android integrated three-party browser