Crosswalk 集成到 Android Studio

Crosswalk介绍:

Crosswalk是一款开源的Web引擎,其基于 Chromium/Blink 的应用运行环境,对于混合开发的轻量级应用尤为受欢迎。

Crosswalk项目的优势:

  • 最大限度降低Android碎片化的影响,得到一致的,可预测的行为。
  • 使用最新的Web技术及API。在Android 4.0+版本上提供丰富的功能。
  • 使用Chrome DevTools轻松调试。
  • 提升应用中HTML,CSS和JavaScript的性能。

总之,Crosswalk就是替代Android中WebView的一个开源库。 
官网:https://crosswalk-project.org

Crosswalk集成到AndroidStudio

1.首先在项目的build.greadle中声明maven仓库


    repositories {

        maven {
            url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'
        }
        jcenter()
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.在module下的build.greadle中添加依赖

compile 'org.xwalk:xwalk_core_library:21.51.546.7'
  • 1
  • 1

具体的版本号可以在这里面查看使用最新的。

3.在module下的build.greadle中添加

    productFlavors {
        armv7 {
            ndk {
                abiFilters "armeabi-v7a", ""
            }
        }
        x86 {
            ndk {
                abiFilters "x86", ""
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

配置基本上到这就结束了,接下来就是敲代码啦啦啦~

1.activity_main.xml

    <org.xwalk.core.XWalkView
        android:id="@+id/xwalkWebView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

2.MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // turn on debugging
        XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
        xWalkWebView.load("http://www.baidu.com", null);
        // load local html file
        // xWalkWebView.load("localfile.html", null);
   }

    @Override
    protected void onPause() {
        super.onPause();
        if (xWalkWebView != null) {
            xWalkWebView.pauseTimers();
            xWalkWebView.onHide();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (xWalkWebView != null) {
            xWalkWebView.resumeTimers();
            xWalkWebView.onShow();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (xWalkWebView != null) {
            xWalkWebView.onDestroy();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

结果如图: 
这里写图片描述

到这里就集成成功啦~

Project Here

原文地址:https://www.cnblogs.com/hzcya1995/p/13317722.html