globals
Global variables and functions are available in all modules. However, the following variables are scoped to a module. See module:
- exports
- module
require()
The objects below are specific to Auto.js. Some built-in objects are part of JavaScript itself and are also global.
For convenience, some module functions can also be used globally. They are not repeated here. For example, setInterval and setTimeout from the timers module.
sleep(n)
n{number} Milliseconds
Pauses execution for n milliseconds. 1 second equals 1000 milliseconds.
// Pause for 5 seconds
sleep(5000);setClip(text)
text{string} Text
Sets the system clipboard content. You can paste it into input fields in other apps.
setClip("Clipboard text");getClip()
- Returns {string}
Returns the current system clipboard content.
toast("Clipboard content: " + getClip());toast(message)
message{string} Message text
Shows a toast message for a short time (duration depends on Android; usually ~2 seconds).
Note that showing a toast is asynchronous. The script does not wait for the toast to disappear before continuing. If you call it in a loop, toasts may continue to appear even after the script ends. For example:
for(var i = 0; i < 100; i++){
toast(i);
}This script finishes quickly, but the toast messages keep popping up. In some cases, even stopping scripts from task manager may not stop the pending toasts. If you want to wait between toasts, you can do:
for(var i = 0; i < 100; i++){
toast(i);
sleep(2000);
}Or wrap toast to add a delay:
var _toast_ = toast;
toast = function(message){
_toast_(message);
sleep(2000);
}
for(var i = 0; i < 100; i++){
toast(i);
}toastLog(message)
message{string} Message text
Equivalent to toast(message); log(message). Shows the toast and also prints to the console. See console.log.
exit()
Stops the script immediately.
Immediate termination is implemented by throwing ScriptInterruptedException. If you catch the exception with try...catch, the script will not stop immediately and may continue for a few more lines.
random(min, max)
min{number} Lower bound (inclusive)max{number} Upper bound (inclusive)- Returns {number}
Returns a random integer in ([min..max]). For example, random(0, 2) may produce 0, 1, or 2.
random()
- Returns {number}
Returns a random floating number in ([0, 1)).
requiresApi(api)
api{number} Android API level
Indicates that this script requires the Android API level to be at least the specified value. For example, requiresApi(19) means the script requires Android 4.4+.
When called, Auto.js checks the device OS API level and throws an exception if the requirement is not met.
Android version ↔ API level reference:
Platform version: API level
Android 7.0: 24
Android 6.0: 23
Android 5.1: 22
Android 5.0: 21
Android 4.4W: 20
Android 4.4: 19
Android 4.3: 18
requiresAutojsVersion(version)
version{string} | {number} Auto.js version (name or code)
Indicates that this script requires Auto.js to be at least the specified version. For example, requiresAutojsVersion("3.0.0 Beta") means Auto.js 3.0.0 Beta+ is required.
When called, Auto.js checks the runtime version and throws an exception if the requirement is not met.
The version argument can be an integer version code (e.g. requiresAutojsVersion(250)) or a version string such as "3.0.0 Beta", "3.1.0 Alpha4", "3.2.0", etc.
You can read the current Auto.js version via app.autojs.versionCode and app.autojs.versionName.
runtime.requestPermissions(permissions)
permissions{string[]} Permission strings
Requests Android runtime permissions dynamically. For example:
// Request GPS permission
runtime.requestPermissions(["access_fine_location"]);Although Android has many permissions, runtime requests must be declared in the manifest. To reduce abuse, Auto.js only allows requesting two extra permissions:
access_fine_location: GPS / fine locationrecord_audio: microphone recording
You can use an APK editor to add permissions for Auto.js and for apps packaged by Auto.js.
For the full permission list, see Permissions Overview.
runtime.loadJar(path)
path{string} Path to a.jarfile
Loads the target JAR file. After loading, classes inside the JAR become available.
// Load jsoup.jar
runtime.loadJar("./jsoup.jar");
// Use jsoup to parse HTML
importClass(org.jsoup.Jsoup);
log(Jsoup.parse(files.read("./test.html")));(jsoup is a Java HTML DOM parsing library. Download it from Jsoup.)
runtime.loadDex(path)
path{string} Path to a.dexfile
Loads the target DEX file. After loading, classes inside the DEX become available.
Because loading a JAR internally converts it to DEX first, loading a DEX file directly is usually faster. You can use the Android SDK build-tools dx to convert a JAR into DEX.
let dexFilePath = 'jbdz2019.dex';
if (files.exists(dexFilePath)) {
runtime.loadDex(dexFilePath);
new Packages["jbdz2019"]()()
let a = __hello()
log(a)
} else {
console.warn('DEX file does not exist');
};You can convert JS files into DEX using certain toolchains.
However, you should not rely on DEX alone for security. DEX can also be reverse engineered and often needs additional obfuscation (“app hardening”).
A safer approach is to obfuscate your JS code first, then convert the JS file into DEX.
context
Global variable: an android.content.Context object.
Note: this is an ApplicationContext, so it cannot be used to create UI elements such as activities or dialogs.
currentPackage()
- Returns {string} Current foreground app package name
Gets the foreground app package name.
Returns the package name of the most recently detected running app, which can usually be treated as the current foreground app.
This function requires the accessibility service. If the service is not enabled, it throws an exception and prompts the user to enable it.
// Example:
log(currentPackage());currentActivity()
- Returns {string} Current foreground Activity name
Gets the foreground Activity name.
Returns the name of the most recently detected Activity, which can usually be treated as the current foreground Activity.
This function requires the accessibility service. If the service is not enabled, it throws an exception and prompts the user to enable it.
On Auto.js versions below 7, some Xiaomi devices may return incorrect results.
// Chat page (Douyin/TikTok China)
//com.ss.android.ugc.aweme.im.sdk.chat.ChatRoomActivity
log(currentActivity())