3

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
Rene B.
  • 6,557
  • 7
  • 46
  • 72
Enkum
  • 635
  • 8
  • 22

2 Answers2

1

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.

Community
  • 1
  • 1
Rene B.
  • 6,557
  • 7
  • 46
  • 72
  • Which .jar file? – Enkum Jun 13 '18 at 13:43
  • Its looking for the class https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/webkit/WebView.java that should be in findsecbugs-plugin-deps-1.4.2.jar – Rene B. Jun 13 '18 at 14:25
  • I have edited my question by adding the code that I am using. @ Rene B. – Enkum Jun 14 '18 at 13:54
  • You could either locate the jar on your device or download it (e.g. from here http://www.java2s.com/Code/Jar/a/Downloadandroid221sourcesjar.htm) and add the path where you store it to the classpath as described in the code above. @EnkumicahelDereje – Rene B. Jun 19 '18 at 08:23
  • I tried as you suggested and it is still not working. If you can use my code as an example and share an example that would be nice. Thank You! @Rene B. – Enkum Jun 20 '18 at 11:21
  • 1
    @EnkumicahelDereje Another question, which operating system are you using? The android.webkit.WebView runs only under Android. If you dant run it on Android device then you should use the virtual machine of andoird: https://kivy.org/docs/guide/packaging-android-vm.html – Rene B. Jun 21 '18 at 18:51
  • Yes, you are right. I was trying to run it on my Linux system. It is supposed to be run on an android device. Thanks for the help. – Enkum Jun 22 '18 at 07:09
  • 1
    @EnkumicahelDereje, I added the description of how to install the Android VM on a Linux System in the answer. – Rene B. Jun 22 '18 at 09:24
  • VM is good. But, I recommend users to use their Android device and run their app using `Buildozer` command `buildozer android debug deploy run` where they can have debugging options. - @Rene B. – Enkum Jun 25 '18 at 07:41
0

Use Pydroid3 to test your apps without compiling them

yaxter
  • 85
  • 1
  • 8