Creating a Ruboto App Without Java
In my Android Meets Ruby: Using Ruboto UI post I showed you how to use Ruboto to create Android apps. In this piece we’ll look at how to work with AndroidManifest.xml to control your Android app.
When writing a Ruboto app, it’s easy to get carried away with the excitement and freedom of having all your app logic in a script. You can easily make a fully-featured app without ever touching Java code. However, you may find that your app refuses to run, or doesn’t run properly. What’s going on? More often than not, it’s because you need to update the AndroidManifest.xml file.
AndroidManifest.xml controls how the Android environment treats your app. It’s essentially a declaration of what your app offers and what it requires. Unlike the content of your Ruboto app, which can be updated on the fly by loading or generating new scripts, the manifest must be defined up front and cannot be changed later on. Two common omissions can trip up developers new to scripting on Android.
Android has a variety of permissions that apps can declare. For example, if you want to access GPS location data, you’ll need to include the following:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
There are no compile-time checks to confirm that your app has declared the necessary permissions, nor are there checks when the app is loaded on the device. Instead, your app will fail at the moment it attempts to access GPS data. This might seem inconvenient, but, particularly in a Ruboto environment, the purpose is quite clear: since your app can theoretically add new code after it is installed, the only way to enforce security is through a run-time check.
When you create your Ruboto app through the “ruboto gen app” command, it will automatically create an Activity and add it to the manifest. So long as you only use one Activity, you don’t need to make changes. However, if you wish to add a separate screen, then in addition to creating the new Activity java class and script, you’ll also need to add it to the Manifest. Again, there won’t be any compile-time or load-time checks, so your app would fail when you try to open the second screen. Fortunately, recent versions of Ruboto include a convenient command that will make all three required updates for a new screen:
$ ruboto gen class Activity --name MyNewActivity
This will help ensure that your AndroidManifest.xml stays in sync with the contents of your app and ease your development.
I hope you enjoy this, and feel free to comment about your Ruboto experiences.
Safari Books Online has the content you need
Check out these Android books available from Safari Books Online:
|Android in Action, Third Edition is a fast-paced book that puts you in the driver’s seat–you’ll master the SDK, build WebKit apps using HTML 5, explore cross-platform graphics with RenderScript, learn to use Native Development Kit, and master important tablet concepts like drag-and-drop, fragments, and the Action Bar, all new in Android 3.|
|Beginning Android 4 is fresh with details on the latest iteration of the Android platform. Begin at the beginning by installing the tools and compiling a skeleton app. Move through creating layouts, employing widgets, taking user input, and giving back results.|
|Android in Practice is a treasure trove of Android goodness, with over 90 tested, ready-to-use techniques including complete end-to-end example applications and practical tips for real world mobile application developers. The book dives into important topics like multitasking and services, testing and instrumentation, building and deploying applications, and using alternative languages.|
|Android UI Fundamentals: Develop and Design walks developers through the different choices available on their way to creating a well-designed application for Android. While building a simple application, Jason works through the basics of Android UI development including layout, event handling, menus and notifications.|
|Programming Android shows experienced application developers what they need to program for the Android operating system — the core building blocks, how to put those blocks together, and how to build compelling apps that work on a full range of Android devices.|
About this author
|Chris King is a software engineer specializing in mobile development. He is the author of Advanced BlackBerry Development, Advanced BlackBerry 6 Development, and Android in Action, Third Edition. You can learn more about Chris at www.linkedin.com/pub/chris-king|