Skip to content

Instantly share code, notes, and snippets.

@nakato
Created May 30, 2019 09:15
Show Gist options
  • Save nakato/d53ae15a7c4eaecd97594fe22eff3d02 to your computer and use it in GitHub Desktop.
Save nakato/d53ae15a7c4eaecd97594fe22eff3d02 to your computer and use it in GitHub Desktop.
Calling an Android Framework CLI tool from Java on Android 7.0
package io.nakato.inputserver;
import java.io.File;
import java.lang.reflect.Method;
import dalvik.system.DexClassLoader;
public class InputServer {
public static void main(String[] args) throws Exception {
(new InputServer()).run(args);
}
private void run(String[] args) throws Exception {
DexClassLoader child = new DexClassLoader(
"/system/framework/input.jar",
"/data/local/tmp/",
"/system/framework",
this.getClass().getClassLoader()
);
Class classToLoad = Class.forName("com.android.commands.input.Input", true, child);
Method method = classToLoad.getDeclaredMethod("main", String[].class);
Object instance = classToLoad.newInstance();
String[] h = {"text", "hello"};
String[] w = {"text", "world"};
Object result1 = method.invoke(instance, (Object) h);
Object result2 = method.invoke(instance, (Object) w);
}
}

Why

Because a 1.2second execution for a pause command is way too long.

% time adb shell input       
Usage: input [<source>] <command> [<arg>...]

<SNIP>

adb shell input  0.00s user 0.01s system 0% cpu 1.209 total

And I don't seem to be able to use the Android Framework API's without building ASOP, and I'm not doing that.


Obviously There's no server here yet.

And it doesn't work with Android 8.0... Because now the framework jar's are empty and are vdex's or something...

So hopefully this doesn't take so long to spin up on 8.0 and this won't be nesessary...

Common TV update... -_______-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment