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" } } }
-
dependencies { implementation 'com.github.fcat97:local-server:Tag' }
Now to use,
-
Start the server & Set the web project's root path (where the
index.html
file is):ServerProvider.startServer() ServerProvider.setDistDir(distHelper.getDistPath(distName))
-
Now just load the local host root i.e.
http://127.0.0.1:9099
inWebView
.if (ServerProvider.currentState is ServerState.Running) { val url = (ServerProvider.currentState as ServerState.Running).url webView.loadUrl(url) }
-
Remember to enable
javascript
anddomStorage
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 ๐