Search code examples
androidpythonkivypyjnius

jnius.find_javaclass , jnius.JavaException: Class not found 'android/webkit/WebView'


I am getting this error while trying to use jnius's autoclass to get the android webview inside Kivy app. What could be the fix?

Thanks

My code looks like this:

import kivy                                                                                     
from kivy.app import App                                                                        
from kivy.lang import Builder                                                                   
from kivy.utils import platform                                                                 
from kivy.uix.widget import Widget                                                              
from kivy.clock import Clock                                                                    
from jnius import autoclass                                           
#from android.runnable import run_on_ui_thread                                                   

WebView = autoclass('android.webkit.WebView')                                                   
WebViewClient = autoclass('android.webkit.WebViewClient')                                       
activity = autoclass('org.renpy.android.PythonActivity').mActivity                    


class Wv(Widget):                                                                               
    def __init__(self, **kwargs):                                                               
        super(Wv, self).__init__(**kwargs)                                                      
        # Clock.schedule_once(self.create_webview, 0)  `                                           

# @run_on_ui_thread                                                                           
# def create_webview(self, *args):                                                            
#     webview = WebView(activity)                                                             
#     webview.getSettings().setJavaScriptEnabled(True)                                        
#     wvc = WebViewClient();                                                                  
#     webview.setWebViewClient(wvc);                                                          
#     activity.setContentView(webview)                                                        
#     webview.loadUrl('http://www.google.com')


class ServiceApp(App):                                                                          
    def build(self):                                                                            
        return Wv()                                                                          


if __name__ == '__main__':                                                                      
    ServiceApp().run

Solution

  • First, you should check if you are running the code in an Android enviroment as the android.webkit.WebView runs only under Android and not on a PC under Windows or Linux.

    Recommended is an Android device and run the app using Buildozer command buildozer android debug deploy run with debugging options.

    In case, you want to run it on a PC then you can install the Android VM from Kivy and follow the following steps on https://kivy.org/docs/guide/packaging-android-vm.html:

    • Download the Kivy / Buildozer VM, in the Virtual Machine section. The download is 1.2GB. Extract the file and remember the location of the extracted directory.

    • Download the version of VirtualBox for your machine from the VirtualBox download area and install it.

    • Start VirtualBox, click on “File”, “Import Appliance”.

    • Select the extracted directory, file should be named “Buildozer VM.ovf”

    • Start the Virtual machine and click on the “Buildozer” icon.

      and let in run in kivy.org/docs/guide/packaging-android-vm.html

    If you still face the problem of a jnius.JavaException: Class not found 'android/webkit/WebView' exception, then you have to add that jar file to the classpath:

    import os
    os.environ['CLASSPATH'] = 'absolute/path/file.jar'
    

    In case of the Class not found 'android/webkit/WebView' exception you could either locate the jar on your device or download it and add the path where you store it to the classpath.