###Remote-control/execute Java on Android phones via Ruby
- Download and compile (
ant build
) the project. Push the .jar file to/data/local/tmp/test.jar
- Run
adb forward tcp:1337 tcp:1337
- Run
adb shell uiautomator runtest test.jar -c imperiusgeorge.TestServer -e port 1337
Send post requests to http://localhost:1337! Pull open a browser and try hitting localhost:7120/execute?on=tests.LanguageAdapterShould$TestClass&method=callReturnString&args=[]
Also try using ImperiusGem to drive your Android via Ruby
##Code examples! Code examples over at the ImperiusGem github page!
##Blog post! We're on the internets! Check out the blog post on lookout hackers blog.
##How it works
We needed a way to drive the Java runtime via text HTTP Get requests. Most of the work is done in LanguageAdapter.java. Here's where we make a single method run
introspectively run any available method on any class or object with any arguments. run
takes three String
parameters, 'on', 'method', and 'arguments'.
on
first tries to find a class via thefindClass()
method (It'll work if it's a fully-qualified class name likecom.android.uiautomator.core.UiDevice
). If that succeeds it'll run The method statically.- If the method is 'new' it'll run the constructor it can find with the given arguments.
- If it can't find the class it'll try looking up the instance (see step 3)
- It then loops through each
method
available, finding ones with the same name and matching number of arguments.- adaptArgs() then tries to adapt, convert, or cast the provided arguments to the method's contract
- If it throws an
IllegalArgumentException
it tries to match more methods (to support overloading).
- Finally it calls the method and calls
adaptReturn()
on the result- If the returned value is a
java.lang.*
object it'll json-serialize it, returning it back to ruby. - A planned improvement is to also return serializable arrays and maps.
- If the object is not directly returnable it stores it in a
HashMap<String,Object>
and returns a unique hash key. That's how the third option for the 'on' parameter works.
- If the returned value is a
A note about bundled code: UI-Automator is actually an Android system tool, invoked via adb shell
. It loads compiled jars' classes and introspectively runs the parameter passed class (it's junit extended). Because of this architecture you can't include separate libs (oh, we tried). We wanted to use gson but chose simple-json for simplicity. We also used NanoHTTPD for the web server functionality. These libraries simplified development a lot! We bundled the source to help simplify yours too, it'll compile and run as-is.