# 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:

```jsx
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:

```jsx
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:

```jsx
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).

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://e1mazahy.gitbook.io/tomesec/droidtomesec/android-pentesting/frida/script-timing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
