4

I m trying to use crosswalk runtime in my android app. I tried this on android 4+.

I got some js & html codes and it worked perfect for me. But it is not working like android webview. In the webview i can call javascript functions from java code. But i couldnt find any option in crosswalk. Any idea?

Thanks

gokhangokce
  • 461
  • 3
  • 8
  • 17

4 Answers4

10

In Android Studio inside app/module/lib level build.gradle add this to dependencies:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'org.xwalk:xwalk_core_library:12.41.296.5'}

Sync Project

create xml layout resource

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<org.xwalk.core.XWalkView
    android:id="@+id/xwalkWebView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    />

</LinearLayout>

In activity onCreate or called by it

//        Don't know how this helps if the preferences are connected?
XWalkPreferences.setValue("enable-javascript", true);
XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);

xWalkView=(XWalkView)findViewById(R.id.xwalkWebView);
xWalkView.addJavascriptInterface(new JS_Bind(this, xWalkView),"Android");

xWalkView.clearCache(true);
xWalkView.load(COM_URL, null);

create class JS_Bind:

public class JS_Bind {
    private static final String TAG = "JS_Bind";
    private Context context;
    private XWalkView xWalkWebView;

    public JS_Bind(Context c, XWalkView xWalkWebView) {
        context = c;
        this.xWalkWebView = xWalkWebView;
    }

    @JavascriptInterface
    public void showToast(String toast) {
        Log.d(TAG, "showToast(String toast)");
        Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
    }
}

in web Java script call the function thus:

Android.showToast(toast);

make sure you import the correct xwalk @JavaScripInterface decorator instead of the standard web view decorator.

Rubber Duck
  • 3,673
  • 3
  • 40
  • 59
  • Thanks for this excellent answer ! Question: how do you know that the "enable-javascript" key name is correct? I would have thought that the XWalkPreferences class would have had that key name defined... – Someone Somewhere Apr 06 '15 at 19:18
  • I still get the error from javascript "Uncaught TypeError: undefined is not a function" eventhough the native method is public, the containing class is public, the native method shows `@JavascriptInterface` – Someone Somewhere Apr 06 '15 at 20:05
  • I am not sure what your question is but I think you should make sure the java binding is named "Android" in addJavaScriptInterface and the JS refers to the Android object – Rubber Duck Apr 06 '15 at 20:48
  • 5
    So I found out that javascript is enabled by default. However, the one key piece of magic is... change "import android.webkit.JavascriptInterface;" to "import org.xwalk.core.JavascriptInterface;" – Someone Somewhere Apr 06 '15 at 21:04
  • you are correct I ment to adf it and had trouble with stack code editor. will do – Rubber Duck Apr 07 '15 at 08:03
  • this is a great tutorial - very concise and direct to the point. The next step is asynchronous image downloads for web pages... which has me scratching my head. – Someone Somewhere Apr 07 '15 at 18:23
  • 1
    this is not telling you how to call javascript from java but it is telling you how to call java from javascript through interface – hariszaman Jul 29 '15 at 11:40
  • You can also write `@org.xwalk.core.JavascriptInterface` instead of `@JavascriptInterface` without an import. – AlexioVay Jun 18 '16 at 10:24
7

Crosswalk support the similar API interface (evaluateJavascript) as Android WebView for calling JavaScript function from Java:

https://crosswalk-project.org/apis/embeddingapidocs/reference/org/xwalk/core/XWalkView.html#evaluateJavascript(java.lang.String, )

You can also use load for calling javascript function directly like: xwalkView.load("javascript:" + jsCode, null).

did you meet any issue with the Crosswalk API?

Shouqun
  • 746
  • 4
  • 3
  • Hi, i used normal webview and at that time i used loadUrl. But with crosswalk api there is no option like loadUrl. Just load function works. also evaluateJavascript not workin – gokhangokce Jul 07 '14 at 07:55
  • Sorry, you can use `load` method in XWalkView: https://crosswalk-project.org/apis/embeddingapidocs/reference/org/xwalk/core/XWalkView.html#load(java.lang.String, java.lang.String), for example: `xwalkView.load("javascript:" + jsCode, null)` – Shouqun Jul 07 '14 at 07:59
  • @gokhangokce How are you invoking `evaluateJavascript` method? – vArDo Sep 26 '14 at 07:27
  • @vArDo i really forget. i checked my code but i dont use crosswalk now. it is not an efficient solution. i advice you new chromium webview. – gokhangokce Sep 26 '14 at 12:04
  • @gokhangokce OK. New chromium in KitKat is nice, but, unfortunately, I need to support older versions of Android and have consistent results. Crosswalk is probably the most stable way to do this. Had no issues so far. – vArDo Sep 27 '14 at 20:28
  • @vArDo too big package and it spends to much cpu. – gokhangokce Sep 28 '14 at 13:12
  • It's a big package yes, but it's still far more stable than both the Chromium Webview, and the previous non-Chromium webview. It doesn't have hidden "unimplemented" things going on like the webview, which doesn't even properly support inline video... – EpicPandaForce Feb 17 '15 at 12:51
  • And supports more devices, right? I'm just implementing Crosswalk for the first time and don't want to regret it later. What's better with chromium webview or isn't this still the case in 2016? – AlexioVay Jun 18 '16 at 10:18
3

The response above is almost complete. as @Someone Somewhere commented it is needed to add these to lines in order to call native functionality from Crosswalk Webview.

  1. add this import statement import org.xwalk.core.XWalkView;
  2. before any exposed method to javascript put this @org.xwalk.core.JavascriptInterface instead of the default annotation @JavascriptInterface
bboydflo
  • 907
  • 1
  • 15
  • 24
0

For calling js method and return back result to java . you have to mix Javascript interface and evaluateJavascript.

public class JS_Bind {
    private static final String TAG = "JS_Bind";
    private Context context;
    private XWalkView xWalkWebView;

    public JS_Bind(Context c, XWalkView xWalkWebView) {
        context = c;
        this.xWalkWebView = xWalkWebView;
    }

    public String GetFullname(String FirstName , String LastName){
        xWalkWebView.evaluateJavascript(String.format(
        "Android.ReturnGetFullName(getFullName('%s','%s'))" , FirstName , LastName));
    }

    @JavascriptInterface
    public void ReturnGetFullName(String Fullname) {
        Log.d(TAG, "showToast(String toast)");
        Toast.makeText(context, Fullname, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void showToast(String toast) {
        Log.d(TAG, "showToast(String toast)");
        Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
    }
}

and in javascript :

<script>
    function getFullName(FirstName , LastName){
        return FirstName +  " " + LastName;
    }
</script>
mehdi
  • 645
  • 1
  • 9
  • 9