Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-15781] Android: KitKat crash when calling WebView method on wrong thread

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2013-11-23T01:18:52.000+0000
Affected Version/sn/a
Fix Version/s2013 Sprint 24, 2013 Sprint 24 Core, Release 3.2.0
ComponentsAndroid
Labels4.4, android, crash, kitkat, merge-3.1.4, module_webview, qe-closed-3.2.0, qe-testadded, triage, webview
ReporterMark Mokryn
AssigneeAllen Yeung
Created2013-11-20T20:44:15.000+0000
Updated2014-03-03T18:30:13.000+0000

Description

The following code crashes on Android 4.4, does not crash on Android 4.1.2. The crash log is: 11-20 22:32:31.861: E/TiExceptionHandler(9656): (main) [0,995] - Message: Uncaught Error: java.lang.Throwable: A WebView method was called on thread 'KrollRuntimeThread'. All WebView methods must be called on the same thread. (Expected Looper Looper{41e338c0} called on Looper{41e52348}, FYI main Looper is Looper{41e338c0}) 11-20 22:32:31.861: E/TiExceptionHandler(9656): (main) [0,995] - Source: Ti.API.debug("user agent: " + $.webview.userAgent); index.xml:
<Alloy>
	<Window class="container" fullscreen=true>
		<WebView id="webview" url="/test.html" />
	</Window>
</Alloy>
index.js:
Ti.API.debug('user agent: ' + $.webview.userAgent); // CRASH HERE ON KitKat

$.index.open();
lib/test.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
	<h2>Hi there</h2>
</body>
</html>

Comments

  1. Mark Mokryn 2013-11-21

    Prior to KitKat we're used to seeing this warning: 11-21 16:06:29.515: W/webview_proxy(4299): java.lang.Throwable: Warning: A WebView method was called on thread 'KrollRuntimeThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads. Looks like in KitKat they finally made good on their warning. :-(
  2. Lokesh Choudhary 2013-11-21

    Verified this for a non alloy & an alloy app & was able to reproduce the issue. Removing the "useragent" property in Ti.API.debug does not throw the error. Environment: Appcel Studio : 3.2.0.201311200357 Ti SDK : 3.2.0.v20131119142443 Mac OSX : 10.8.5 Alloy : 1.3.0 CLI - 3.2.0-alpha
  3. Mark Mokryn 2013-11-21

    On a general note: In KitKat Google changed the WebView to Chrome, so there may be more issues: https://developers.google.com/chrome/mobile/docs/webview/overview
  4. Lokesh Choudhary 2013-11-22

    I was able to reproduce this issue today on a nexus 4 running android 4.3 Environment: Appcel Studio : 3.2.0.201311211712 Ti SDK : 3.2.0.v20131119142443 Mac OSX : 10.8.5 Alloy : 1.3.0 CLI - 3.2.0-alpha
  5. Allen Yeung 2013-11-23

    Test case:
        var webview = Titanium.UI.createWebView({url:"/test.html", height: 400, width: 400, top: 0});
           var window = Titanium.UI.createWindow();
           window.add(webview);
           window.open({modal:true});
          	var button = Ti.UI.createButton({
          		title: 'webview tests',
          		height: 200,
          		width: 200,
          		bottom: 0
          	});
          	window.add(button);
          	button.addEventListener('click', function(){  			
          			webview.evalJS('console.log("test")');
          			webview.html = "<html><head><title>Test</title></head><body><h1>Test</h1></body></html>";
          			Ti.API.debug('----------------------------------setHTML: ' + webview.html);
          			Ti.API.debug('----------------------------------user agent: ' + webview.userAgent);
          			webview.userAgent = "test";
          			webview.setBasicAuthentication('ayeung', 'password123');
          			webview.canGoBack();
          			webview.canGoForward();
          			webview.goBack();
          			webview.goForward();
          			webview.reload();
          			webview.stopLoading(true);
          			Ti.API.debug('----------------------------------pluginState: ' + webview.pluginState);
          			webview.pause();
          			webview.resume();
          			webview.enableZoomControls = true;
          			Ti.API.debug('----------------------------------enableZoomControls: ' + webview.enableZoomControls);
          			webview.release();
          			alert('DONE, Please check logs');
          	});
       
    1. Set your target SDK to API 18 or 19 by putting this in your tiapp.xml:
           <android xmlns:android="http://schemas.android.com/apk/res/android">
               <manifest>
                   <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18"/>
               </manifest>
           </android>
       
    2. Run the test case 3. click the button 4. verify that no warning/errors appear relating to webview method called from wrong thread.
  6. Allen Yeung 2013-11-23

    PR: https://github.com/appcelerator/titanium_mobile/pull/5016
  7. Samuel Dowse 2013-11-25

    Verified fixed on: Mac OSX 10.9 Mavericks Titanium Studio, build: 3.2.0.201311221859 Titanium SDK, build: 3.2.0.v20131125103938 CLI: 3.2.0-alpha Alloy: 1.3.0-alpha6 Android Device: Nexus 5, 4.4 (KitKat) Ran test case provided for Alloy and Classic projects. The test.html page is located and displayed correctly on screen. No error is thrown in console log. Closing.
  8. Mark Mokryn 2013-11-26

    This should be backported to 3.1.X . This bug will crash a lot of apps as KitKat adoption ramps up.
  9. Ingo Muschenetz 2013-11-26

    Yes, it is already scheduled for that. [~ayeung], can we prepare a backport for 3.1.X?

JSON Source