且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DalvikVM 上的 CLI 在 JNI 库上失败

更新时间:2023-12-03 22:44:34

好问题!我不得不挖掘一下才能弄清楚这一点.

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

libandroid_runtime.so 中有大量 JNI 方法,因此当您使用 dalvikvm 命令时,默认情况下它们不会被绑定.不幸的是,你不能只做一个 System.loadLibrary("android_runtime"),因为这实际上并没有绑定所有的本地方法.

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.

然而,经过一番挖掘,事实证明有一个内部的、非公开的、不保证存在的类,名为 com.android.internal.util.WithFramework,其目的是加载 libandroid_runtime.so 并绑定其所有JNI 方法.

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.

要使用它,只需将 com.android.internal.util.WithFramework 放在类名前面,在 dalvikvm 命令上,如下所示:

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"

(注意:这仅适用于 M 之前的设备,因为 WithFramework 类是 在 M 中删除 - 感谢@JaredRummler 的提醒)

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