Bypassing Android’s “Resources”

Android relies heavily on something called “resources” to access portions of the system that aren’t directly in the Java code. For instance, if you had an ogg file that you wanted to use to play a sound with, you would add it as res/raw/foo.ogg. Then, during compilation, the aapt tool transforms all your resources into a Java file called, which is basically a series of integers that can be used to reference the actual resources. For instance, to set up our hypothetical ogg file, we would do:

MediaPlayer mp = MediaPlayer.create(context,;

That’s all well and good as far as it goes, and if you don’t have any reason not to use it, you should.

However, with Hecl, one of my goals is to make it very easy to create applications. This means:

  • No complex directory layout structure.

  • No big long build files that require tools like ant.

  • No complex compilation steps.

I’m not there yet, on the Android platform, which is a bit more complex than Java ME (where it is indeed so easy to create new Hecl .jars that it’s possible to do it on the fly via this web site:, but I am making progress.

One thing that a user might want to do is add their own files to the .apk file that actually gets installed on the phone. Since it’s just a zip file, that’s pretty easy:

zip -r Hecl.apk res/raw/foo.ogg

However, without a resource, there is no way to access that file, so the question is: how can we get ahold of it somehow?

The first piece of the puzzle is Java’s ZipFile class, which lets us open up a zip compressed file, such as the .apk file that contains the ogg file we’re interested in in this case.

The critical passage is getting ahold of the actual filename on the Android filesystem of the code that’s currently running. It turns out to not be too difficult, if a bit obscure:

String filename = this.getPackageManager().getApplicationInfo(packagename, 0).sourceDir;

With that information in hand, we can feed it to ZipFile, and then access the contents at run time.

It’s not a perfect nor complete solution, yet. So many of the Android API’s require a resource ID, so it takes some extra coding to do things from the InputStream that we can get out of the zip file, but there may be ways to make this easier. For instance, we could simply dump the contents of raw/ into an application specific data directory the first time an application runs, in order to at least be able to access the files as files, rather than InputStreams connected to ZipEntries. In any case, at least it’s possible to access files added to the .apk without requiring the compilation step.

I’m not sure how many other use cases there might be for direct access to the .apk file, but if other people need to do this, hopefully this information will be helpful.

iPhone – nice, but not for me

Being fairly interested in this whole mobile phone software thing, it’s pretty hard not to sit up and take notice of the iPhone SDK that was just announced. A few thoughts:

  • Of course it’s nice, polished, and lets you do some interesting things with the phone. That was not too surprising though.

  • The “investment fund” seems like something of a response to the Android Developer Challenge, and is certainly an interesting move on Apple’s part. Strategically, it certainly alters the focus of the money flowing into the equation. Comparing the two, the dollar amounts in the ADC are much smaller, but they are also more “concrete”: N people will get X dollars in time frame T. As an independent developer, that works fairly well for me as something to have a go at and then move on from, win or lose. An investment fund backed by Kleiner Perkins is on a whole different scale and level. The amount of money is huge (they won’t even bother with less than 100K), but who knows how that works out in practice, or how much of that is actually 100% committed to iPhone companies. KP aren’t the sort of people that are going to just hand that out to any old company that does an iPhone app. They’re also going to want to be involved in the companies, which means there won’t be too many of them. In short, it brings in all the good and bad aspects of venture capital. In short: in one corner, lots of money for relatively big, organized players, and in the other, small, quick incentives to do cool things that anyone has a shot at. We won’t really be able to judge the results until we see how it plays out. (As an aside, I still have some bad memories of the management that KP foisted off on Linuxcare).

  • The system for selling applications is smart. You will have one place to go to to get your apps, and as a user you’ll have some assurance that they’re up to certain quality standards. People will like that kind of thing, and is far nicer than anything JavaME ever had. Instead of multiple, fractured marketplaces, you get one big one. Of course, it also keeps Apple in control of everything, which also has some negative implications as well.

  • 37 Signals seem to be wildly ecstatic about Apple’s potential, but I’m a bit less sanguine. I don’t doubt that the iPhone has a bright future, that much is pretty obvious. However… dominate? First of all, if it’s so widespread, it loses some of its cachet, doesn’t it? I don’t think Apple wants that. I don’t see them licensing the system so that other phone manufacturers can use it, so it will only be adapted as widely as Apple sees fit. Cheaper Nokia models are very popular over here in Europe. Will iPhones at those price points (less than 100 euro) be available and competitive with Nokia’s offerings? Open and adaptable aren’t necessarily things that Apple is known for, which might be the difference between “strong presence” and “dominate”, as we saw with Mac vs Windows. Getting 10-15% towards the high end of the market might be what they aspire to, rather than ubiquity.

  • I could, however, very easily envision Apple getting into some kind of business niche and dominating that completely, as they did with education for a while. That might not extend to the broader market, though.

  • Moving on to other considerations, this one is important to me: apparently, according to the license agreement, you can’t run an interpreter that fetches code and executes it, amongst the various other restrictions! That’s kind of a deal breaker for Hecl, which counts that ability as one of its strengths. I don’t think a port was in the works in any case since Hecl is written in Java. Still, though, that’s the kind of annoying restriction that reaffirms my commitment to open source.

  • As an aside, I wonder what Sun is up to at this point – between this and Android, they have to be under enormous pressure to release a successor to Java ME, which was a good effort for its time, but is beginning to look dated and limited, even though it is very widely deployed.

I’m an open source guy at heart. I love openness and source code and the freedom to tinker… and while I’ll certainly give Apple their due for making some fine products, closed and controlled is simply not for me. Others may feel differently – it’s a matter of taste and preferences.

So for the moment, my efforts will be focused on Android, which I see as the best, most open thing out there at the moment.

Android – commercial meets open source

One of the interesting things about Google’s Android project is the mix of cultures.

There are a number of people like me, firmly grounded in the open source world, who are happy to get something that 1) works, 2) looks to have legs in terms of popularity, and 3) will be open source. Think of a lot of your favorite open source projects when they first launched. “Polished” isn’t a word you would probably use to describe them, although it usually happens in later releases (Ubuntu looks better and better, for example).

There are also a lot of people who come from a ‘commercial’ background, who expect to see everything ready and waiting on a plate for them, which isn’t a mistaken expectation if you’re used to using products from big companies who are staking their reputations on them. They want nice, thorough docs for everything, almost all bugs worked out, and lots of easy to use tools.

Android lies in a middle ground somewhere between these two camps, which I think at times makes people uneasy.

It’s not open source yet. You can’t hack it as much as you may want. You can’t rebuild it from the ground up. You don’t get access to the communication between the developers working on it, and cannot participate in that development. However, there is a promise to open source it that would reflect very, very poorly on Google were it not met and is therefore pretty credible. The developers and many people involved with it certainly seem to “get” open source, and are taking good steps to create a community around Android (a few even hang out in the #android IRC channel on Freenode) that will most likely not take too long to start assimilating interested outsiders once the code is open. In terms of quality, it wasn’t bad when it came out, but it was very definitely an “early look”, put out there to solicit feedback and change according to it, something that’s part and parcel of a release early, release often open source model.

On the other side, Android looks to be a big effort from a big company to get into a big market in a big way – it’s not a “under the radar” type of strategy that the open source world is often constrained to follow for lack of monetary and marketing resources to do it any other way. This has attracted a number of developers who either don’t see or don’t really get some of the “open source” aspects of what we have so far: it’s still changing! The docs aren’t complete! The emulator is still ugly in places! And from their point of view, it must seem odd to getting something that’s obviously not a finished product, without crystal clear direction and goals and a roadmap, and without actual hardware only available in the distant (well, in internet terms! – 8 months or so) future. That kind of thing probably isn’t done very often in their world, or where it’s done, is certainly kept quieter and perhaps restricted to registered developers. They came over to have a look at something that was ready to go, and it isn’t, yet. This has caused some disappointment for people who expected to see something ready to go out of the box.

Perhaps Google needed to manage their initial communications slightly better in order to manage the second group’s expectations more carefully. In terms of us open source folks, I think that what we want is more access, as soon as Google is willing to hand it out. More source code access, but also access to the development process and people. “Real” open source isn’t just about the code, it’s about being more or less equal participants in a community. Luckily, I think the people at Google get this, which is one of the reasons I’m enthusiastic about Android, even if I wish we didn’t have to wait so long for the source code.

Looking under the Android hood

Here’s one way of having a look under the android hood:

mkdir /tmp/android/
cp .../android.jar /tmp/android
cd /tmp/android
unzip android.jar
... lots of files unzipped ...
for c in `find  android/ -name "*.class" | sed -e 's/.class//'`; do nice javap "$c" ; done > androidclasses.txt

That gives you a nice dump of all the classes available. It’s a useful way to get an idea of some of the under the hood capabilities of Android. Perhaps someone wiser in the ways of Java hacking can suggest even better ways of accomplishing the same thing.

Of course, anything that’s not officially documented is liable to change!

Remote Android Application Shell with Hecl

The latest component of Android Hecl that I have been working on is a simple shell server that lets you interact with Hecl remotely, in order to be able to telnet in, run commands and see what they do. In order to give it a whirl, fetch Hecl from svn:

svn co hecl

Then install the Hecl.apk:

adb install android/bin/Hecl.apk

Select the Hecl Server demo, at the bottom of the list:

Hecl Server

You also need to make sure that port forwarding is set up:

adb forward tcp:7405 tcp:7405

At this point, you can telnet to port 7405 on localhost, and run Hecl commands:

> puts hi
> androidlog "hello world"

> java android.telephony.TelephonyManager telman
> set phoneinfo [[activity] getSystemService phone]
> $phoneinfo getLine1Number

Since this simple server runs in its own thread/interpreter, you can’t control the GUI from it. To do that, there is a ‘maineval’ function that sends code back to the main interpreter:

> maineval {set layout  [linearlayout -new [activity]]}
> maineval {$layout setorientation VERTICAL}
> maineval {set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}]}
> maineval {$layout addview [textview -new [activity] -text "Hi!" -layoutparams $layoutparams]}
> maineval {[activity] setcontentview $layout}

Gets us this very, very simple screen:

Hecl Hi

Hecl’s Java reflection support is still fairly alphaish, but you can use it to query classes about their methods and constructors, and call them most of the time; errors should be sent to me or the mailing list.

I found this little server particularly useful when fiddling around with some of the undocumented android classes, as a way to experiment in a running system.

Munich Android Mobile Meeting Report

I’m a bit tired after driving up to Munich for the Android developer meeting, hosted by Google, and being “on” all day, but it was worth it. I was a bit apprehensive that it might be too ‘introductory’ in the sense of the Google guys telling you in person things that you could have easily looked up on the web, without spending a bunch of time driving around Austria and Bavaria. Fortunately, that wasn’t the case. There was some of that, but also plenty of unstructured time to chat with Dan Morrill and Jason Chen, the two Google “Developer Advocates”, who are both very friendly, informative and informed, approachable and put a lot of energy into what they do, despite being a bit tired from flying over from the US. It was nice to get a chance to talk with them, and considering that the platform is still changing, useful as well, because they were definitely listening to what people had to say to them. My thanks to them for a job well done.

Here are some “impressions” I took away – note that they’re not what anyone said, just ideas I got about what they said:

  • Source code is being released when handsets ship because they want to work very closely with the hardware partners to make sure the phone experience is good (they said this). They want to “set the bar high”, and they probably run the risk of someone snatching the code and slapping together a phone if they let out the source code to early. (They definitely didn’t say this, it’s just me connecting the dots).

  • Despite interest for ‘other languages’, it really looks like they want everything to go through Dalvik in some way or another. Clearly, at some point in the future, there will be specs for writing Dalvik byte code.

  • They won’t talk about the upgrade strategy, something I asked about. I want to know that as a developer, I won’t be stuck with N combinations of things that I have to support over the years. I want to know how they’ll minimize that pain, but they’re not currently able to discuss it.

  • Talking with other people attending, there seemed to be some doubts about security and permissions for user data, since Android is moving away from the j2me “everything is private” model, to a model where data can be shared between apps, which opens up lots of good things, but also some potentially bad ones as well.

There’s still really a sense of Android continuing to evolve, in a true spirit of openness. I liked that, as it means there is space for people to tell Google what they want, rather than simply being given a finished product and told that that’s that… but then again, I would, being the open source guy that I am.

I didn’t get to show off Hecl quite as much as I would have liked to, but I did get to mention it a few times, so I guess I did ok.

Munich Android Mobile Meeting

Tomorrow (January 28th), I’ll be in Munich for this:

Unfortunately, the event filled up super fast and I barely managed to squeeze in, so there are probably no spaces left. Still, if anyone would like to meet up afterwards, send me an email soonish. If you managed to get in, and would like to see a demo of Hecl, let me know, because I’d like to have the chance to show that off some too!

Hopefully it will be a valuable experience. Android is a pretty convincing system in many ways, but of course has yet to fully come into contact with reality, so I’m curious to meet some of the people involved and hear about it from them. I really hope it doesn’t end up being a rehash of information that’s available on the web, as it’s a two hour drive from Innsbruck.

New Hecl Release

After four or so months, it’s time for another Hecl “checkpoint”. Those who are seriously interested really ought to be getting Hecl from subversion, however, it’s a good idea to tie up the loose ends every now and then, and put something that’s easy to fetch for the casual user.

This release contains a number of interesting things:

  • Alpha support for Java integration. For example:

      java java.lang.System sys
      set ms [sys currentTimeMillis]
  • Alpha support for Google Android. This utilizes the Java integration system above heavily. What sets it apart from just writing code in Java is that on top of the building blocks, I’ve started to write little bits of Hecl code to make common things very straightforward, layering on top of the Java API.

  • Numerous fixes in the code, documentation, and build system.

Android Task List / Selector

Credit for finding out how to do this goes to dims, who has been doing lots of cool Android hacking lately:

If you’re interested in Android and what it can do, you should be following his online journal!

As a way to keep pushing and testing Hecl, I decided to see if I could copy what he’d done in Hecl. It has indeed produced a few improvements to the Java code. That done, making it work was short and sweet:

set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}
set layout [linearlayout -new $context]
$layout setorientation VERTICAL

# Create ourselves some commands to access Android internals.
java activitymanagernative
java iactivitymanager
java {$TaskInfo} taskinfo
java android.content.ComponentName componentname

java java.util.List javalist
set am [activitymanagernative getdefault]
set tasks [$am gettasks 10 0 [null]]
set len [$tasks size]
set tasklist [list]
for {set i 0} {< $i $len} {incr $i} {
    set baseactivity [[$tasks get $i] -field baseactivity]
    lappend $tasklist "Task: [$baseactivity getpackagename]"

$layout addview [textview -new $context -layoutparams $layoutparams 
         -text "Currently running tasks"]

set lview [basiclist $context $tasklist -layoutparams $layoutparams]
$layout addview $lview
$lview requestfocus

set callback [callback -new [list [list SelectTask]]]
$lview setonitemclicklistener $callback

heclcmd setcontentview $layout

proc SelectTask {parent view position id} {
    [activitymanagernative getdefault] movetasktofront $position

It could have been squeezed further by avoiding some of the temporary variables, and perhaps also by implementing a mapping between java.util.List and Hecl lists.

I think one of the next things I want to do is work on some infrastructure so that people will be able to actually work on scripts on their phone. That’s sort of a departure from the past (most phone keyboards would be sheer torture for inputting long scripts), but it’s a cool demo in any case.