Skip to content

fcat97/local-server

Repository files navigation

Local Host Library

A local server library to load web projects into android web view.

How To Use?

  • add jitpack to your project

    // in project's root build.gradle
    allprojects {
        repositories {
    		// ...
    		maven { url 'https://jitpack.io' }
    	}
    }
    
    
    // or in settings.gradle file
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
            maven { url "https://jitpack.io" }
        }
    }
  • Then import the library. tag:

    dependencies {
        implementation 'com.github.fcat97:local-server:Tag'
    }

Now to use,

  1. Start the server & Set the web project's root path (where the index.html file is):

    ServerProvider.startServer()
    ServerProvider.setDistDir(distHelper.getDistPath(distName))
  2. Now just load the local host root i.e. http://127.0.0.1:9099 in WebView.

    if (ServerProvider.currentState is ServerState.Running) {
      val url = (ServerProvider.currentState as ServerState.Running).url
      webView.loadUrl(url)
    }
  3. Remember to enable javascript and domStorage

    webView.apply {
        @SuppressLint("SetJavaScriptEnabled")
        settings.javaScriptEnabled = true
        settings.domStorageEnabled = true
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            webViewClient = this@DistViewActivity.webViewClient
        }
    }

    All done.


Happy coding 🚀