Using android.jar in Java project - RuntimeException Stub?
I tried to include android.jar into Java project, remove JRE from Build Path and run this code. It throws Runtime exception. Why?
Exception in thread "main" java.lang.RuntimeException: Stub!
at android.content.ContentValues.<init>(ContentValues.java:5)
at JarTest.main(JarTest.java:5开发者_如何学JAVA)
public class JarTest {
public static void main(final String[] args) {
final ContentValues values = new ContentValues();
values.put("test", "test");
System.out.println(values);
}
}
Why is ContentValues used only in Android environment?
The android.jar in the SDK only contains stub implementations of the SDK classes, not the real implementations that are found on the devices (or emulator). The jar just provides the class metadata so that your apps that reference the SDK classes will build properly.
You can't create any project that references the android.jar that will run outside an Android device.
android.jar contains stubs only, enough to let you build your project, as @antlersoft explained.
The Robolectric project http://robolectric.org/ allows you to run tests on your Android code without using a device. They have real builds available on Maven http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.robolectric%22
I have been successfully running most of the Android backend code by using the android-all artifact.
Might not help in your specific case, since some APIs are still out of bounds, but for most other cases of java.lang.RuntimeException: Stub!
including the artifacts from Robolectric should help.
精彩评论