2011. március 2., szerda

Android Development, pitfalls so far

The Android platform is now the fancy development target for Java programmers. Therefore, I started to learn it and as expected, I fell right into some of its pitfalls. Things that should nicely work according to the various tutorials, but there are things that you will only get working after searching through dozens of forums and forum mirrors.

For fun, I tried to move some of the bits of my Open Imperium Galactica project and spent hours to resolve the most simplistic problems.

So lets see the problems step by step.

Device size limits

By default, most pre 2.2 applications are limited to 24MB of application size. In reality, you'll start to get error messages after 12MB. After 2.2, you can specify your application is willing to run from the device's SD card:

Set android:installLocation="preferExternal" in AndroidManifest.xml

(see this, this).

You can create a custom device with more Java heap and you can set a debug option in Eclipse for the emulator: -partition-size 1024

Assets vs Res

The main difference is that for things in res/ directory, you get a special class R where you can easily reference them by name in your code. Most utility classes can use this kind of referencing. However, you have more freedom with the assets/ directory contents.

The trouble with both directories is that the package builder can take extremely long time to build your project. Autobuild can cause headaches too after a simple save.

One option is to simply remove the file extension from the resource files. But this may get you into trouble with the MediaPlayer.

The second trouble is with the assets directory itself. It seems that
referencing resources in this directory does not work. The workaround is to place the resources under a subdirectory of assets: assets/media and reference them via media/sound1.wav.

MediaPlayer with assets

There are several caveats with the MediaPlayer.

First, media player takes the media format primarily from the file name. If you renamed your resources, you are have to rename them back.

The second thing is that you need to pass in the start and size of the referenced asset along with its FileDescriptor:


AssetFileDescriptor afd = getAssets().openFD("media/sound1.wav");
MediaPlayer mp = new MediaPlayer();

mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

mp.prepare();
mp.start();


Rendering Open-IG videos fast

Next time, once I figure out how to speed up, I will talk about how to render a series of bitmaps.

Nincsenek megjegyzések: