90 lines
2.8 KiB
Rust
90 lines
2.8 KiB
Rust
//! Build script — compiles bundled Java SMS sources to `classes.dex`.
|
|
//!
|
|
//! Pipeline:
|
|
//! 1. `javac` compiles each `.java` against `android.jar` → `.class` files.
|
|
//! 2. `d8` converts the `.class` files into a single `classes.dex` in `OUT_DIR`.
|
|
//!
|
|
//! No-op on non-Android targets.
|
|
|
|
use std::{env, fs, path::PathBuf};
|
|
|
|
const JAVA_FILES: &[&str] = &[
|
|
"src/android/SmsReceiver.java",
|
|
"src/android/SmsForegroundService.java",
|
|
"src/android/BootReceiver.java",
|
|
];
|
|
|
|
fn main() {
|
|
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
|
|
|
|
if target_os == "android" {
|
|
for f in JAVA_FILES {
|
|
println!("cargo:rerun-if-changed={f}");
|
|
}
|
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
if env::var("JAVA_SOURCE_VERSION").is_err() {
|
|
env::set_var("JAVA_SOURCE_VERSION", "11");
|
|
}
|
|
if env::var("JAVA_TARGET_VERSION").is_err() {
|
|
env::set_var("JAVA_TARGET_VERSION", "11");
|
|
}
|
|
|
|
let android_jar_path =
|
|
android_build::android_jar(None).expect("Failed to find android.jar");
|
|
|
|
let mut java = android_build::JavaBuild::new();
|
|
java.class_path(android_jar_path.clone())
|
|
.classes_out_dir(out_dir.clone());
|
|
|
|
for rel in JAVA_FILES {
|
|
java.file(manifest_dir.join(rel));
|
|
}
|
|
|
|
assert!(
|
|
java.compile()
|
|
.expect("failed to acquire exit status for javac invocation")
|
|
.success(),
|
|
"javac invocation failed"
|
|
);
|
|
|
|
let d8_jar_path =
|
|
android_build::android_d8_jar(None).expect("Failed to find d8.jar");
|
|
|
|
let class_root = out_dir.join("robius").join("trigger");
|
|
let mut class_files: Vec<PathBuf> = Vec::new();
|
|
if let Ok(entries) = fs::read_dir(&class_root) {
|
|
for entry in entries.flatten() {
|
|
let path = entry.path();
|
|
if path.extension().and_then(|e| e.to_str()) == Some("class") {
|
|
class_files.push(path);
|
|
}
|
|
}
|
|
}
|
|
assert!(
|
|
!class_files.is_empty(),
|
|
"no .class files found in {class_root:?} — javac may have failed silently"
|
|
);
|
|
|
|
let mut run = android_build::JavaRun::new();
|
|
run.class_path(d8_jar_path)
|
|
.main_class("com.android.tools.r8.D8")
|
|
.arg("--classpath")
|
|
.arg(android_jar_path)
|
|
.arg("--output")
|
|
.arg(&out_dir);
|
|
|
|
for class_file in &class_files {
|
|
run.arg(class_file);
|
|
}
|
|
|
|
assert!(
|
|
run.run()
|
|
.expect("failed to acquire exit status for java d8.jar invocation")
|
|
.success(),
|
|
"java d8.jar invocation failed"
|
|
);
|
|
}
|
|
}
|