Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using android.jar in Java project - RuntimeException Stub?

Tags:

java

android

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)

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?

like image 505
emeraldhieu Avatar asked Aug 30 '11 17:08

emeraldhieu


2 Answers

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.

like image 149
soulseekah Avatar answered Nov 17 '22 06:11

soulseekah


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.

like image 30
antlersoft Avatar answered Nov 17 '22 05:11

antlersoft