Script Timing

When using Frida, timing is everything—because classes and methods might not be loaded or instantiated when your script runs. If you try to use Java.choose() or Java.use() too early, you’ll get errors like:

ClassNotFoundException: Didn't find class ...

Here’s how to control timing properly:

🛠️ Frida's App Launch Modes

Mode

Description

-U -f <package>

Spawns the app (cold start). Script runs before app fully loads.

-U -n <package>

Attaches to a running app. Script runs after the app is fully loaded.

-U -p <pid>

Attaches to a process by PID (also used after app is already running).

  • Use -n if you want to avoid timing issues and let the app load fully.

Delaying Execution with setTimeout()

If you must use -f (spawn), add a delay so your code runs after the app initializes:

Java.perform(function () {
    setTimeout(function () {
        // Hook logic here
    }, 2000); // 2-second delay
});

🔄 Hooking Lifecycle Methods for Precise Timing

Instead of guessing with setTimeout(), you can hook a known method like onCreate() of an activity:

Java.perform(function () {
    var Activity = Java.use("com.mobilehackinglab.FridaSix.MainActivity");

    Activity.onCreate.overload("android.os.Bundle").implementation = function (bundle) {
        console.log("[+] onCreate called — app is ready");

        // Safe to do stuff here
        var checker = Java.use("com.mobilehackinglab.FridaSix.Checker");
        var checker_obj = checker.$new();
        checker_obj.x.value = 1337;
        checker_obj.y.value = 1200;

        this.getFlag(checker_obj);

        // Call original
        return this.onCreate(bundle);
    };
});

Java.perform() vs Java.performNow()

Function

Behavior

Java.perform()

Executes your code after Java VM is ready. Safe and preferred.

Java.performNow()

Executes immediately, assumes VM is ready (risky with -f).

  • Use Java.performNow() only if you're sure the VM and classes are ready (e.g., with -n or from another hook).


Last updated