MCPcopy Index your code
hub / github.com/adblockplus/libadblockplus-android

github.com/adblockplus/libadblockplus-android @test-version1588695946.96

Chat with this repo
repository ↗ · DeepWiki ↗ · release test-version1588695946.96 ↗ · + Follow
1,436 symbols 4,438 edges 132 files 147 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Adblock Android SDK

An Android library project, tests, settings fragments and demo application for AdblockWebView.

Git commits

This repo uses pre-commit to maintain agreed conventions in the repo. It should be installed (tldr; pip install pre-commit then pre-commit install) before making any new commits to the repo.

Updating the dependencies

Adblock Android SDK has dependencies that aren't in this repository. To update those, call:

./ensure_dependencies.py

Please make sure you have python2 installed

Library

An Android library that provides the core functionality of Adblock Plus. You can find it in the 'adblock-android' directory.

Using as a Gradle dependency

Make sure you have jcenter() in the list of repositories and then add the following dependency:

dependencies {
    implementation 'org.adblockplus:adblock-android:3.0'
}

In general case it's suggested to use the most recent version.

Building

Requirements

Edit 'buildToolsVersion' in 'build.gradle' files if necessary.

Building of libadblockplus

First, make sure all the prerequisites are installed. Second, one needs to build V8 required for libadblockplus. See libadblockplus/README or V8 documentation on how to build V8 or fetch precompiled one. For the latter, run in 'libadblockplus' directory:

make TARGET_OS=android ABP_TARGET_ARCH=arm Configuration=release get-prebuilt-v8
make TARGET_OS=android ABP_TARGET_ARCH=arm64 Configuration=release get-prebuilt-v8
make TARGET_OS=android ABP_TARGET_ARCH=ia32 Configuration=release get-prebuilt-v8
make TARGET_OS=android ABP_TARGET_ARCH=x64 Configuration=release get-prebuilt-v8

Make sure to set ANDROID_NDK_ROOT environment variable to point to Android NDK installation, eg.:

export ANDROID_NDK_ROOT=/Users/developer/ndk/android-ndk-r16b

After that we can build libadblockplus:

make TARGET_OS=android ABP_TARGET_ARCH=arm Configuration=release
make TARGET_OS=android ABP_TARGET_ARCH=arm64 Configuration=release
make TARGET_OS=android ABP_TARGET_ARCH=ia32 Configuration=release
make TARGET_OS=android ABP_TARGET_ARCH=x64 Configuration=release

Building from command-line

In the project root directory create the file local.properties and set sdk.dir and ndk.dir to where you installed it, e.g.:

sdk.dir = /some/where/sdk
ndk.dir = /some/where/ndk

In the project root directory run:

./gradlew assembleDebug

This will generate *.aar artifacts in the '.../build/outputs/aar/' directories:

  • adblock-android-abi_all-... - AAR for all the ARCHs (x86, armv7a, arm64)
  • adblock-android-abi_x86-... - AAR for x86 only
  • adblock-android-abi_x86_64-... - AAR for x86_64 only
  • adblock-android-abi_arm-... - AAR for armv7a only
  • adblock-android-abi_arm64-... - AAR for arm64 only
  • adblock-android-webview-... - AAR for AdblockWebView
  • adblock-android-settings-... - AAR for Settings

Android permissions note

An app that uses the library have to add the following permissions to AndroidManifest.xml: * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> * <uses-permission android:name="android.permission.INTERNET"/>

(added automatically if building with Gradle or should be added manually otherwise).

Build directory configuration

By default Gradle uses build directory to build modules, however it can be undesired for some use cases like CI or building as Chromium submodule. Set GRADLE_BUILD_DIR environment variable to configure build directory:

GRADLE_BUILD_DIR=/tmp ./gradlew clean assemble

Note

[Configuration] Building project in /tmp

output while building

Building with prebuilt shared V8

This can be desired to use product's V8 (let's say Chromium) instead of built-in V8. Put prebuilt shared V8 library file(s) in ARCH directories and set SHARED_V8_LIB_FILENAMES environment variable and SHARED_V8_LIB_DIR before building. You can pass multiple filenames in SHARED_V8_LIB_FILENAMES, separated with space. Libadblockplus is required to be linked with that library file(s).

For example:

SHARED_V8_LIB_FILENAMES=libv8.cr.so SHARED_V8_LIB_DIR="/tmp/shared_v8" ./gradlew clean assembleAbi_arm

or

SHARED_V8_LIB_FILENAMES="libv8.cr.so libv8_libbase.cr.so libv8_libplatform.cr.so" SHARED_V8_LIB_DIR="/tmp/shared_v8" ./gradlew clean assembleAbi_arm

for multiple library files.

Note

[Configuration] Excluding shared v8 library libv8.cr.so from AAR
...
[Configuration] Linking dynamically with shared v8 library /tmp/shared_v8/release/libv8.cr.so
...

output while building.

Building with exposing of libadblockplus classes

Set EXPOSE_LIBABP_OBJECTS environment variable to expose libadblockplus classes in shared library.

For example:

EXPOSE_LIBABP_OBJECTS=y ./gradlew clean assembleAbi_arm

JNI adjustments

In order to load custom library name pass LIBABP_SHARED_LIBRARY_NAME environment variable (without lib and .so):

LIBABP_SHARED_LIBRARY_NAME=adblockplus ./gradlew assembleRelease

In order to skip compilation of JNI classes pass SKIP_JNI_COMPILATION environment variable:

SKIP_JNI_COMPILATION=true ./gradlew assembleRelease

Building for single ARCH

By default adblock-android is built for ARM/ARM64 and x86/x86_64 and it can be filtered when building end-user android application. However sometimes it can be desired to build "adblock-android.aar" for single ARCH.

Pass abi_arm, abi_arm64, abi_x86, or abi_x86_64 to build it for single arch or abi_all for all ARCHs:

`./gradlew clean assembleAbi_arm`

Note

[Configuration] Using adblock-android ABI flavor: abi_arm

output while building.

SDK tests

Pure java tests

You can find pure Java tests in 'src/test' directories of the modules (if provided). In the project directory run:

./gradlew test

You can select test class/method and click 'Run ..Test'. No Android emulator/device running required.

Android tests

You can find Android tests in 'src/androidTest' directories of the modules (if provided). In the project directory run:

./gradlew connectedAbi_x86DebugAndroidTest

to test with x86 device/emulator or run:

./gradlew connectedAbi_armDebugAndroidTest

to test with ARM device/emulator. You can select test class/method and click 'Run ..Test'.

Settings

An Android library that provides a configuration interface for Adblock Plus. You can find it in the 'adblock-android-settings' directory: * GeneralSettingsFragment - main fragment * WhitelistedDomainsSettingsFragment - whitelisted domains fragment

Using as a Gradle dependency

Make sure you have jcenter() in the list of repositories and then add the following dependency:

dependencies {
    implementation 'org.adblockplus:adblock-android-settings:3.0'
}

In general case it's suggested to use the most recent version.

Usage

Create AdblockEngineProvider instance and AdblockSettingsStorage instance. You can use SharedPrefsStorage implementation to store settings in SharedPreferences. Or you can use AdblockHelper:

AdblockHelper
  .get()
  .init(this, getFilesDir().getAbsolutePath(), true, AdblockHelper.PREFERENCE_NAME);

  // optional - provide preloaded subscription files in app resources
  .preloadSubscriptions(AdblockHelper.PRELOAD_PREFERENCE_NAME, map);

Make sure you initialize it once during app launch, call isInit() to check it:

if (!AdblockHelper.get().isInit())
{
  // requires initialization
  ...
}

Sometimes it's desired to initialize or deinitialize FilterEngine instance when created:

AdblockHelper
  .get()
  .init(...)
  .addEngineCreatedListener(engineCreatedListener)

or disposed:

AdblockHelper
  .get()
  .init(...)
  .addEngineDisposedListener(engineDisposedListener)

Make sure you deinitialize it when values used during initialization are no longer valid:

AdblockHelper.get().deinit();

Note one have to initialize it again to be used.

Implement the following interfaces in your settings activity:

  • BaseSettingsFragment.Provider
  • GeneralSettingsFragment.Listener
  • WhitelistedDomainsSettingsFragment.Listener

and return created instance or AdblockHelper instances:

AdblockHelper.get().getProvider().getEngine();  // engine
AdblockHelper.get().getStorage(); // storage

Retain Adblock instance in activity onCreate in synchronous mode (it may take few seconds):

AdblockHelper.get().getProvider().retain(false);

or in asynchronous mode (without current thread lock):

AdblockHelper.get().getProvider().retain(true);

Invoke waitForReady every time you need AdblockEngine instance if retained in asynchronous mode:

AdblockHelper.get().getProvider().waitForReady();

Release Adblock instance in activity onDestroy:

AdblockHelper.get().getProvider().release();

Insert GeneralSettingsFragment fragment instance in runtime to start showing settings UI.

Background operations

By default filter engine will do some background operations like subscriptions synchronizations in background shorly after initialized. If you want to have ad blocking as optional feature, you should consider use setDisabledByDefault

    AdblockHelper
      .get()
      .init(...)
      .setDisabledByDefault()

In this case no background operations will be done once you will call AdblockEngine.setEnabled(true). Please note that this method configures only default state. If user prefrence on enabled state is stored in settings, this value will be preferred.

Other thing to take into account is synchroization time. If you have configured setDisabledByDefault and then enable engine, first synchronization will be done only after some time. You can combine configuration with preloadSubscriptions to load data from local file first time rather then from web.

Theme

Make sure to set application theme with PreferenceThemeOverlay.v14.Material parent theme (see AndroidManifest.xml and styles.xml in adblock-android-webviewapp as an example).

Building

In the project root directory run:

./gradlew assemble

This will generate *.aar in the 'adblock-android-settings/build/outputs/aar' directory.

WebView

An Android library that provides a WebView component with Adblock Plus integrated. You can find it in the 'adblock-android-webview' directory.

AdblockWebView class provides built-in ad blocking (both resource loading filtering and element hiding) and inherits from Android 'WebView'.

Using as a Gradle dependency

Make sure you have jcenter() in the list of repositories and then add the following dependency:

dependencies {
    implementation 'org.adblockplus:adblock-android-webview:3.0'
}

In general case it's suggested to use the most recent version.

Usage

In layout XML:

<org.adblockplus.libadblockplus.android.webview.AdblockWebView
    android:id="@+id/main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

In java source code:

AdblockWebView webView = findViewById(R.id.main_webview);

Use AdblockEngine.setEnabled(boolean enabled) to enable/disable ad blocking for AdblockEngine. Make sure you update the settings model if you want the new value to be applied after application restart, eg:

AdblockSettingsStorage storage = AdblockHelper.get().getStorage();
AdblockSettings settings = storage.load();
if (settings == null) // not yet saved
{
  settings = AdblockSettingsStorage.getDefaultSettings(...); // default
}
...
settings.setAdblockEnabled(newValue);
storage.save(settings);

Android SDK logging system is based on Timber library.

If you are configuring your project using Maven dependencies to consume our Android SDK then Timber dependency is automatically installed. If you are just copying AAR files to your project workspace then you need to add this line to your dependencies:

implementation 'com.jakewharton.timber:timber:4.7.1'.

To enable desired log output level configure Timber logger in your application code.

For example this code enables all debug logs in DEBUG mode:

if (BuildConfig.DEBUG) {
    Timber.plant(new Timber.DebugTree());
}

Please refer to https://github.com/JakeWharton/timber for more information about Timber.

Use setAllowDrawDelay(int allowDrawDelay) to set custom delay to start render webpage after 'DOMContentLoaded' event is fired.

Use setProvider(@NotNull AdblockEngineProvider provider) to use external adblock engine provider. The simplest solution is to use AdblockHelper from -settings as external adblock engine provider:

webView.setProvider(AdblockHelper.get().getProvider());

If adblock engine provider is not set, it's created by AdblockWebView instance automatically.

Use setSiteKeysConfiguration(..) to support sitekeys whitelisting. This is optional but highly suggested. See TabFragment.java on usage example.

Use setEventsListener() to subscribe and unsubscribe to ad blocking and whitelisting events, eg. "resource loading blocked" or "resource loading whitelisted" event that can be used for stats. For the latter there is a convenience class WebViewCounters which can be bound to EventsListener and notify your View about new values. See an example of usage in WebView Application.

Use dispose(Runnable disposeFinished) to release resources (**requi

Extension points exported contracts — how you extend this code

Callback (Interface)
Generic callback [8 implementers]
adblock-android/src/main/java/org/adblockplus/libadblockplus/HttpClient.java
Provider (Interface)
Provides AdblockEngine and SharedPreferences to store settings (activity holding BaseSettingsFragment fragment should im [1 …
adblock-android-settings/src/main/java/org/adblockplus/libadblockplus/android/settings/BaseSettingsFragment.java
EventsListener (Interface)
Listener for ad blocking related events. However, this interface may not be in use if Adblock Plus is disabled.
adblock-android-webview/src/main/java/org/adblockplus/libadblockplus/android/webview/AdblockWebView.java
Disposable (Interface)
(no doc) [32 implementers]
adblock-android/src/main/java/org/adblockplus/libadblockplus/Disposable.java
Listener (Interface)
Listener with additional `onWhitelistedDomainsClicked` event
adblock-android-settings/src/main/java/org/adblockplus/libadblockplus/android/settings/GeneralSettingsFragment.java
EventsListener (Interface)
Listener for changing events.
adblock-android-webview/src/main/java/org/adblockplus/libadblockplus/android/webview/WebViewCounters.java
Storage (Interface)
Interface to remember intercepted subscription requests [4 implementers]
adblock-android/src/main/java/org/adblockplus/libadblockplus/android/AndroidHttpClientResourceWrapper.java
Listener (Interface)
Listener with additional `isValidDomain` method
adblock-android-settings/src/main/java/org/adblockplus/libadblockplus/android/settings/WhitelistedDomainsSettingsFragment.java

Core symbols most depended-on inside this repo

get
called by 138
adblock-android/src/main/java/org/adblockplus/libadblockplus/sitekey/PublicKeyHolder.java
evaluate
called by 111
adblock-android/src/main/java/org/adblockplus/libadblockplus/JsEngine.java
getFilter
called by 76
adblock-android/src/main/java/org/adblockplus/libadblockplus/FilterEngine.java
addToList
called by 69
adblock-android/src/main/java/org/adblockplus/libadblockplus/Filter.java
add
called by 63
adblock-android-webviewapp/src/main/java/org/adblockplus/libadblockplus/android/SubscriptionsManager.java
asString
called by 53
adblock-android/src/main/java/org/adblockplus/libadblockplus/JsValue.java
Get
called by 45
adblock-android/src/main/jni/Utils.h
equals
called by 44
adblock-android/src/main/java/org/adblockplus/libadblockplus/Filter.java

Shape

Method 1,105
Class 164
Function 137
Interface 21
Enum 9

Languages

Java82%
C++11%
TypeScript5%
Python2%

Modules by API surface

adblock-android-webview/src/main/java/org/adblockplus/libadblockplus/android/webview/AdblockWebView.java120 symbols
adblock-android-webview/src/main/assets/elemhideemu.js72 symbols
adblock-android/src/main/java/org/adblockplus/libadblockplus/android/AdblockEngine.java49 symbols
adblock-android/src/androidTest/java/org/adblockplus/libadblockplus/test/FilterEngineTest.java40 symbols
adblock-android/src/main/java/org/adblockplus/libadblockplus/FileSystem.java38 symbols
adblock-android/src/main/java/org/adblockplus/libadblockplus/FilterEngine.java34 symbols
adblock-android-webviewapp/src/main/java/org/adblockplus/libadblockplus/android/webviewapp/TabFragment.java30 symbols
adblock-android-webview/src/main/java/org/adblockplus/libadblockplus/android/webview/WebViewCounters.java28 symbols
adblock-android/src/main/java/org/adblockplus/libadblockplus/JsValue.java27 symbols
ensure_dependencies.py26 symbols
adblock-android/src/test/java/org/adblockplus/libadblockplus/test/SiteKeyVerifierTest.java26 symbols
adblock-android-settings/src/androidTest/java/org/adblockplus/libadblockplus/android/settings/SharedPrefsStorageTest.java25 symbols

For agents

$ claude mcp add libadblockplus-android \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page