CLI on DalvikVM fails on JNI lib

Good question! I had to dig a bit to figure this out.

There are a slew of JNI methods in libandroid_runtime.so that don’t get bound by default, when you’re using the dalvikvm command. Unfortunately, you can’t just do a System.loadLibrary(“android_runtime”), because this doesn’t actually bind all the native methods.

However, after some digging, it turns out there is an internal, non-public, not guaranteed to be there class called com.android.internal.util.WithFramework, whose purpose is to load libandroid_runtime.so and bind all its JNI methods.

To use it, just throw com.android.internal.util.WithFramework in front of your class name, on the dalvikvm command, like so:

dalvikvm -cp /some/path/classes.dex com.android.internal.util.WithFramework my.example.cls "This is an argument"

(Note: This only works on pre-M devices, due to the WithFramework class being removed in M – thanks for the heads up @JaredRummler)

Leave a Comment