This project is a simplest Java wrapper for the adblock-rust
library, allowing you to use the powerful ad-blocking capabilities of adblock-rust
in your Java applications.
adblock-rust
is a high-performance ad-blocking library written in Rust. This project provides a Java wrapper around adblock-rust
, enabling Java developers to integrate ad-blocking functionality into their applications seamlessly.
- High-performance ad-blocking using
adblock-rust
. - Easy-to-use Java API.
- Cross-platform support.
- Java Development Kit (JDK) 8 or higher.
- Rust and Cargo (for building the native library).
First, you need to build the adblock-rust
library and generate the shared library file (.so
, .dll
, or .dylib
depending on your OS).
- Clone the
adblock-rust
repository: - Build the library:
cargo build --release --manifest-path adblock-rs/Cargo.toml
mvn install
To build it for android platforms just use ndk (see installation on official site):
cargo ndk -t aarch64-linux-android -o ./target build --release
mv arm64-v8 release
Go back to project root and enter:
mvn install
If you want to build library to another platform add option --target
with needed platform like aarch64-unknown-linux-gnu
, .
After that check that path of built library exists into pom.xml
maven-resources-plugin
section. As default rustup
using current platform and creates target/{debug,release}
directories.
After switching target platform by rustup
these directories created as targer/{target-platform}/{debug,release}
directories.
Example building library for aarch64-unknown-linux-gnu
target:
cargo build --release --target aarch64-unknown-linux-gnu --manifest-path adblock-rs/Cargo.toml
And you have to update pom.xml
file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-native-libs</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/target/lib</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/adblock-rs/target/aarch64-linux-android/debug/</directory>
<includes>
<include>**/*.dylib</include>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/adblock-rs/target/aarch64-linux-android/release/</directory>
<includes>
<include>**/*.dylib</include>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
And build jar:
mvn install
- Locate the generated shared library file in the
target/release
directory.
public class Main {
public static void main(String[] args) {
List<String> rules = new ArrayList<>(List.of(
"-advertisement-icon.",
"-advertisement-management/",
"-advertisement.",
"-advertisement/script."
));
AdvtBlocker blocker = AdvtBlocker.createInstance(rules);
}
}
-
Copy the shared library file to a directory accessible by your Java application.
-
Add the Java wrapper library to your project. You can do this by including the JAR file or adding the source code directly to your project.
Before using the wrapper, you need to load the native library. This can be done using System.loadLibrary
or System.load
.
Here is an example of how to use the Java wrapper to block ads:
import com.example.adblock.AdblockEngine;
public class Main {
public static void main(String[] args) {
List<String> rules = new ArrayList<>(List.of(
"-advertisement-icon.",
"-advertisement-management/",
"-advertisement.",
"-advertisement/script."
));
AdvtBlocker blocker = AdvtBlocker.createInstance(rules);
boolean result = blocker.checkUrls(
"http://example.com/-advertisement-icon.",
"http://example.com/helloworld",
"image"
);
System.out.println(result);
}
}