English | 简体中文
A lightweight and efficient adblock engine library for Android, which has strong compatibility for filters like EasyList and AdGuard Filters.
The native C++ code is based on brave/ad-block. Beyond adapting it to Android platform, I fixed some fatal issues (d85d341, 583f87a), made 40x better parsing performance (ab18236, a0009c8) and implemented some new features.
- All features from brave/ad-block
- Support for element hiding
- Support for Extended CSS selectors
- Support for CSS rules
- Support for Scriptlet rules
- Support for checksum
- Support for background download and installation
- Custom filter subscriptions
- Custom rules
- Android
- Kotlin
- C++
- JavaScript
- JNI
See releases and the code in app/src
.
Note: This development requires you to have fundamental Android WebView experience.
Note: A full application example can be found in :app
module.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency:
dependencies {
implementation 'com.github.Edsuns.AdblockAndroid:ad-filter:1.0'
}
Add the code in your Application
class
class App : Application() {
override fun onCreate() {
super.onCreate()
// Debug configuration.
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
// Start adfilter.
val filter = AdFilter.create(this)
}
}
Add the code in your WebChromeClient
class
class WebClient(private val webViewClientListener: WebViewClientListener) : WebViewClient() {
private val filter = AdFilter.get()
override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
val result = filter.shouldIntercept(view!!, request!!)
return result.resourceResponse
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
filter.performScript(view, url)
}
}
Add the code in your Activity
class
class MainActivity : AppCompatActivity(), WebViewClientListener {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val filter = AdFilter.get()
val filterViewModel = filter.viewModel
// Setup AdblockAndroid for your WebView.
filter.setupWebView(binding.webView)
// Add filter list subscriptions on first installation.
if (!filter.hasInstallation) {
val map = mapOf(
"AdGuard Base" to "https://filters.adtidy.org/extension/chromium/filters/2.txt",
"EasyPrivacy Lite" to "https://filters.adtidy.org/extension/chromium/filters/118_optimized.txt",
"AdGuard Tracking Protection" to "https://filters.adtidy.org/extension/chromium/filters/3.txt",
"AdGuard Annoyances" to "https://filters.adtidy.org/extension/chromium/filters/14.txt",
"AdGuard Chinese" to "https://filters.adtidy.org/extension/chromium/filters/224.txt",
"NoCoin Filter List" to "https://filters.adtidy.org/extension/chromium/filters/242.txt"
)
for ((key, value) in map) {
val subscription = filterViewModel.addFilter(key, value)
filterViewModel.download(subscription.id)
}
}
filterViewModel.onDirty.observe(this, {
// Clear cache when there are changes to the filter.
// You need to refresh the page manually to make the changes take effect.
binding.webView.clearCache(false)
})
}
}
Congratulations, great success!