Setup
Add Dari to your Android project
Two Ways to Capture
| Manual (default) | Gradle plugin (new in 2.6.0, optional) | |
|---|---|---|
| Dependencies | dari + dari-noop | The same dari + dari-noop |
| Extra setup | None | mavenCentral() in pluginManagement of settings.gradle.kts, and plugins { id("io.github.easyhooon.dari") } plus a dari { } mapping in the app module's build.gradle.kts |
| Bridge code | Interceptor calls where requests enter and responses leave | No Dari calls |
| Performance | Baseline | Same as manual, except bridges that pass large JSON strings (Performance) |
- The dependency alone doesn't turn on the plugin way. Adding or upgrading
darichanges nothing until you also apply the plugin and writedari { }. Without them, only your own interceptor calls are recorded. - Upgrading with manual calls: bump the version. No code changes.
- Pick one way per bridge. If a mapped method already calls
DariInterceptor, the plugin skips that method and prints a build warning. It only checks the mapped method's own body, so a manual call elsewhere on the same path, such as in a handler, is recorded a second time.
This page sets up the manual way. For the plugin, add the Gradle plugin setup on top of the same dependencies.
Installation
Add the dependencies to your app module's build.gradle.kts:
dependencies {
debugImplementation("io.github.easyhooon:dari:3.8.0")
releaseImplementation("io.github.easyhooon:dari-noop:3.8.0")
}The dari-noop module implements the same public APIs with no-op behavior, so your code compiles in release with no runtime overhead.
Initialize
Dari starts automatically through AndroidX Startup. Call Dari.init() only when you need custom configuration:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
Dari.init(
context = this,
config = DariConfig(
showNotification = true,
shakeToOpen = true,
)
)
}
}If you use the default configuration, you do not need any initialization code.
Connect Your Bridge
Create an interceptor through Dari.createInterceptor() so the same app code compiles in debug and release builds:
val interceptor = Dari.createInterceptor(tag = "MyBridge")Call it where requests enter your bridge and in the one function that sends responses back, not in each handler. In release builds, dari-noop returns null, so use safe calls:
inner class Bridge {
@JavascriptInterface
fun onBridgeRequest(handlerName: String, requestId: String, data: String?) {
// Every request enters here
interceptor?.onWebToAppRequest(handlerName, requestId, data)
when (handlerName) {
"getAppInfo" -> sendResponseToWeb(handlerName, requestId, true, appInfoJson())
// ...other handlers reply through sendResponseToWeb too
}
}
}
// Every reply leaves through here
private fun sendResponseToWeb(handlerName: String, requestId: String, isSuccess: Boolean, data: String) {
interceptor?.onWebToAppResponse(handlerName, requestId, data, isSuccess)
val script = "onBridgeResponse(${JSONObject.quote(requestId)}, $isSuccess, ${JSONObject.quote(data)})"
webView.post { webView.evaluateJavascript(script, null) }
}Replies sent later, e.g. from a permission callback, are still paired by requestId as long as they go through the same function.
Without Bridge Code (optional)
The interceptor calls above are the default way and keep working. If you'd rather not touch your bridge code, the Gradle plugin inserts the same calls into debug builds from a one-time dari { } mapping.