This project is the 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-rustrepository: - Build the library:
cargo build --release --manifest-path adblock-rs/Cargo.toml
mvn installTo 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 installIf you want to build library to another platform add option --target with needed platform like aarch64-unknown-linux-gnu, .
After that, check the path of the built library exists in pom.xml maven-resources-plugin section. By default, rustup uses the current platform and creates target/{debug,release} directories.
After switching the target platform using rustup, these directories are created as targer/{target-platform}/{debug,release} directories.
Example of building the library for the aarch64-unknown-linux-gnu target:
cargo build --release --target aarch64-unknown-linux-gnu --manifest-path adblock-rs/Cargo.tomlYou alse need to update the 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/releasedirectory.
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);
}
}