Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Hi my name is Andrew Glover; welcome to Rapid android development with JRuby. In this demonstration I’m going to show you Ruboto, a framework that leverages the power of JRuby to enable you to quickly build and deploy Android apps. In order to get started with Ruboto, you need a few things: JRuby, the Android SDK, and Ant. Lastly, in order to actually see what you’ve built and take advantage of rapid prototyping and turn around, you’ll want to create an emulator instance -- for example, I’ve got one that targets the Android 2.2 platform (which as of this recording, is the largest distribution of android devices (roughly 56%)). The Ruboto framework allows you to create Ruboto projects via the command line (just like you can do with Rails and other full stack frameworks like Grails). For example, in this case, I’m going to create an project named Lagniappe (LAN-YAP) and have it target my Android 2.2 profile. The ruboto gen app command will build a project structure that contains everything you need to build an Android App including all required dependencies (from JRuby to Android). Once you’ve generated a project, you can quickly deploy it -- fire up your emulator (be sure its the target Android platform you specified when you generated your app), then run from within your project’s root folder ‘rake install’. If all goes well, you should see a simple default application, which means everything is working well and you are ready to build something cool. With Ruboto, you are free to code in normal Java in Activity classes generated by Rubuto or you can code your app in JRuby inside scripts which are loaded by Android Activities. For instance, in this case, the Ruboto framework generated a script dubbed lagniapp_activity. Ruboto scripts are simple -- you can import standard android widgets like buttons and other UI elements. In the setup_content block, an app’s ui is assembled -- this is akin to the standard XML file that you’d have to create otherwise in Android. You can also build handler’s for UI actions (such as button clicks). Lagniappe is a simple app that allows you to geo-tag deals, as such, I’m going to change the UI around a bit by adding another 2 text inputs. In Ruboto, UI widgets take a hash (which is akin to a Map in Java) of properties -- for instance, in these cases I’m passing in a hint value for each text input. Next, I’m going to change around the behavior of a button click -- in this case, I’m going to set the text of each input to blank and show a simple dialog. Note JRuby’s syntax is quite simple and allows you to bypass typical setter & getter semantics in Java. I can rapidly redeploy my updated app by running the rake update_scripts command. Sure enough, I’ve got a basic UI for capturing deals. Because my app uses GPS and transmits data to a backend server, I need to request permission for both via the AndroidManifest file. To use GPS, I need to create a listener, which is easy enough to do with Rubuto via the rubuto gen interface command. In this case, I’m going to have Rubuto generate a default implementation of android’s LocationListener, which combined with the permission via the manifest will provide my app location information should the underlying device have GPS. To make use of my newly minted location listener, I have to import a few standard Android classes -- note how I can easily import normal Java classes via the java_import phrase. What’s more, I can use the ruboto_import phrase to import my location listener. To start using my LocationListener, I need to instantiate it and then use Android’s built in classes to obtain a location manager, test to see if GPS is enabled, and then finally get the current location. Note how in JRuby, I can call normal Java methods via idiomatic ruby; that is, while Java code is usually written in camel case, ruby prefers to use underscore to separate words. Thus, in Jruby I can stick with ruby style method calls, which are auto-magically converted into the corresponding Java call (such as get System Service). Also note the provider enabled method ending in a question mark -- which is translated into standard java as is Provider Enabled. Ruby offers an not if expression known as unless -- that is, rather than having a negative if expression, such as if location isn’t null, I can use the unless expression which negates the negative. Thus, in this case, I print out the location so long as it isn’t null. Now that I’ve added code that obtains GPS information, I need to send that (along with the corresponding deal data) to a back end web service. I’m going to use JSON as my data format and in this case, I’m going to use a standard Ruby JSON library. Consequently, with Ruboto I can use Ruby’s bundler, which is a powerful dependency management framework. I can code normal methods in my rubuto scripts too -- and in this case, I’m going to write a simple method that leverages Apache’s HTTP client library (which is included in Android) to issue an HTTP PUT to my service. Using Ruby’s JSON library is amazingly straightforward -- to create a JSON document, I create a hash and call to_json on it. Again notice JRuby’s semantics which enable you to write ruby style code even though you are working with plain jane java objects. For more information on Ruboto, go to Ruboto.org where you can find documentation, downloads, and of course, interact with the ruboto community. Don’t forget to check out IBM developerworks for plenty of android articles including on on Ruboto.