androidkotlinwebviewgeckoview

Failed to initialize GeckoRuntime | GeckoView | Android


 private fun setupGeckoView() {
        val runtime = GeckoRuntime.create(this) // crashes on this line
        geckoSession.open(runtime)
        geckoView.setSession(geckoSession)
        val url = String(Base64.decode(MYURL, Base64.DEFAULT))
        geckoSession.loadUri(url)
        geckoSession.progressDelegate = createProgressDelegate()
        geckoSession.settings.allowJavascript = true
    }

i call setUpGeckoView methon in onCreat() but when i click back and reopen the app then app crashes with IllegalStateException saying "Failed to initialize GeckoRuntime. It works first time only crashed when i click back and then open app again"

Logs are given below

 Process: arholding.kargoshop.mk, PID: 16444
    java.lang.RuntimeException: Unable to start activity ComponentInfo{arholding.kargoshop.mk/arholding.kargoshop.mk.SeckoActivity}: java.lang.IllegalStateException: Failed to initialize GeckoRuntime
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3447)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7762)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
     Caused by: java.lang.IllegalStateException: Failed to initialize GeckoRuntime
        at org.mozilla.geckoview.GeckoRuntime.create(GeckoRuntime.java:458)
        at org.mozilla.geckoview.GeckoRuntime.create(GeckoRuntime.java:333)
        at arholding.kargoshop.mk.SeckoActivity.setupGeckoView(SeckoActivity.kt:23)
        at arholding.kargoshop.mk.SeckoActivity.onCreate(SeckoActivity.kt:19)
        at android.app.Activity.performCreate(Activity.java:7981)
        at android.app.Activity.performCreate(Activity.java:7970)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

Solution

  • This exception will be thrown if there is already an active Gecko instance running. There are many ways to resolve this problem.

    Solution 1: Get the default runtime for the given context.

    Change your code from

    val runtime = GeckoRuntime.create(this)
    

    to

    val runtime = GeckoRuntime.getDefault(this)
    

    Solution 2: Kill the process when exit app by finishing activity, add this code into your activity.

    override fun onDestroy() {
        Process.killProcess(Process.myPid())
        super.onDestroy()
    }
    

    Solution 3: Only create a new instance if there is no active instance running

    private fun setupGeckoView() {
        if (geckoRuntime == null) {
            geckoRuntime = GeckoRuntime.create(this)
        }
    
        geckoSession.open(geckoRuntime!!)
        geckoView.setSession(geckoSession)
        val url = String(Base64.decode(MYURL, Base64.DEFAULT))
        geckoSession.loadUri(url)
        geckoSession.progressDelegate = createProgressDelegate()
        geckoSession.settings.allowJavascript = true
    }
    
    companion object {
        var geckoRuntime: GeckoRuntime? = null
    }