Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run android code in REPL

I have an android application with a lot of communication. I'd like to run code for android inside REPL , for development, debug etc

like this

HttpClient client = new DefaultHttpClient();
print(client.execute(new HttpGet("some url"), new BasicResponseHandler()));

by this way I can see my changes very fast, without running on phone or emulator.

I've tried to add android.jar to beanshell, but it does not work that way.

like image 282
logcat Avatar asked Feb 15 '12 15:02

logcat


1 Answers

You won't be able to run classes that are part of the Android platform (e.g. HttpClient) in a REPL, because the methods in android.jar are all stubs. The real implementation of these methods resides on your phone or emulator, so you'll have to run the emulator at a minimum.

See Apache DefaultHttpClient invocation results in "java.lang.RuntimeException: Stub!" for more details.

For example, if you run the code you posted in a REPL, you'll get a RuntimeException similar to the following:

java.lang.RuntimeException: Stub!
    at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:5)
    at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:7)
    at CodeSnippet_10.run(CodeSnippet_10.java:7)
    at org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain1.eval(ScrapbookMain1.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain.evalLoop(ScrapbookMain.java:54)
    at org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain.main(ScrapbookMain.java:35)

If you want to verify this firsthand, create a new Java project in Eclipse, then create a new scrapbook page in your project (File > New > Other... > Java Run/Debug > Scrapbook Page). Add android.jar to the project's build path, then right-click the scrapbook page, select Set Imports and import the org.apache.http.* package. When you execute the snippet (Ctrl + U), you will get an exception similar to the one I posted above.

like image 84
Justin Garrick Avatar answered Oct 18 '22 00:10

Justin Garrick