← recent

Ruby on Android -- Part 2

Continuing towards my goal of developing an application in ruby for my Android (see part 1), I’ve been getting into Rhodes, which is an open-source project that aims to allow you to build native apps for a variety of mobile platforms, all from a lightweight ruby framework. For Android, it uses xruby (not Jruby).

I started with the documentation at the Rhodes wiki, and checked out a Google Talk. I tried to duplicate the sample application in the video, but quickly starting hitting walls, which I have been trying to work through. I haven’t had a ton of success with it yet, but I’ll document what success I have had.

To start with, rhodes only seems to support Windows and Mac out of the box. I have forked the rhodes project, to add in linux support, as well as try to fix whatever isn’t working for me (which I’ll get to later). You can check out my forked rhodes my github page. The easiest way to use the repo version (rather than the gems) is to set up an alias in your bash_profile such as:

alias

rh

=

"

RUBYLIB=/home/rellik/repos/rhodes/rhodes/rhodes-build/lib:/home/rellik/repos/rhodes/rhodes/rhodes-framework/lib

"

Prefacing any command with ‘rh ’ will then use the repo version of the Rhodes libraries.

After generating an application with rhogen (found in the repo under rhodes-generator/bin/ or the rhodes-generator gem), the other platform-specific bit that you need to override is the build.yml file that gets generated for a new project. Since I’m only concerned about Android at the moment, the only line I needed to change here was the last one, to point to the android.jar that came with my SDK. For me, this amounts to:
    

android

: /home/rellik/Desktop/android

-sdk

-linux_x86

-

1.5_r3/platforms/android

-

1.5

With all this, I was still not having much luck with Rhodes. I used ‘rh rake run:android:app’ to generate the apk and push it to the emulator. I was getting an error about no space left on the virtual SD card, but I do like having the apk be signed as part of the rake task, so I usually just run:android:app (and let it fail), then install the application manually using ‘adb install -r bin/target/Rhodes-debug.apk’ (the emulator must already be running).

Once the application is successfully installed on the emualator, you can slide the applications tray open and click on the Rhodes icon. You’ll see a ‘Loading…’ screen, then a blank screen. For me, that’s all there is. I tried simplifying the index.erb file, and even taking out any logic, but still I get the same blank screen. I renamed the non-dynamic index.erb to index.html, and that worked fine (I saw that page after ‘Loading…’), so there seems to be something funky about rendering the ERB file.

To debug this issue, I added a series of raises and debug statements to the rake task that builds the apk, and I discovered a few things. First, the erb file gets converted to ruby, which contains the use of String#force_encoding (which is ruby 1.9). I tried downloading and using ruby 1.9 for this, as well as just taking that line out of a generated erb file, but neither helped.

Second, I left #force_encoding out, and I added “puts _erbout” as the last line of the generated erb file (bin/RhoBundle/apps/app/index_erb.rb), and then used the following to test if the generated java was any good, and indeed it was (but failed when I put force_encoding back in):

$ java -cp /home/rellik/repos/rhodes/rhodes/rhodes-build/res/xruby-0.3.3.jar:bin/RhoBundle.jar xruby.apps.app.index_erb.main

<

div

 

class

=

“toolbar”

>
  

<

h1

 

id

=

“pageTitle”

>

       Contacter

  

h1

>

div

>

<

ul

 

id

=

“home”

 

selected

=

“true”

 

title

=

“FirstApp”

>
 

<

li

>Hello Android! (This was generated dynamically and can add ten and two to make: 15)

li

>

ul

>

So, that’s pretty much where I am so far. I can build an app with static views, and I can confirm that the java that gets generated for my dynamic views seems ok (but won’t display on the emulator, despite the apparent lack of exceptions). Maybe the force_encoding bit is the problem (that’ll be my next avenue to explore).