Java Code

Allows you to write and execute arbitrary Java code. The code is interpreted by the BeanShell interpreter, which uses a syntax similar to older versions of Java (pre-Java 5).

AI Code Helper

Click the magnifying glass icon next to the code field to get help writing your code. You can use the built-in AI to generate the code for you directly in the editor, or you can copy the system instructions to use with your preferred external AI (like ChatGPT, Gemini, etc.).

Available Variables

A special variable named context is always available for you to use in your code. This is an Android Context object from a Service. Because it is not from an Activity, you must add the flag FLAG_ACTIVITY_NEW_TASK when starting new activities to avoid errors.

You can also access and use Java objects that were created and returned by other Tasker Java actions (like Java Function) within your code.

Return Value

The value from your code's return statement will be stored in the output variable. It's important to note that any variables you declare inside your code are temporary and will be discarded once the action finishes. Only the object or value you explicitly return will be stored.

The type of storage depends on the variable name you choose:

Warning: This is a very powerful action for advanced users. The code you write is not sandboxed and can modify your device's settings or data. The BeanShell interpreter does not support modern Java features like Generics (<...>), Lambdas, or Streams.

Example
You can use the following code to set the %wifi variable to the name of currently connected Wifi Network.
Java Code:
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;

/* Get the WifiManager service. */
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

String ssid = null;

/* Check if WiFi is enabled and manager is available. */
if (wifiManager != null && wifiManager.isWifiEnabled()) {
    /* Get information about the currently connected WiFi network. */
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        ssid = wifiInfo.getSSID();
        /* Remove surrounding quotes from the SSID if present. */
        if (ssid != null && ssid.startsWith("\"") && ssid.endsWith("\"")) {
            ssid = ssid.substring(1, ssid.length() - 1);
        }
    }
}

/* Return the SSID, or null if not connected or WiFi is off. */
return ssid;
Return Field
%wifi