Friday, January 13, 2012

YAML parser for Android

I needed YAML parser for Android this week and after a bit of research I found that SnakeYAML might work: http://code.google.com/p/snakeyaml
The only problem was if you try to use the latest code the application would fail with exception that looks something like this in the logcat:

VFY: unable to resolve exception class 21 (Ljava/beans/IntrospectionException;)


This happens due to the dependency on java.bean package APIs that is used to parse getters/setters types of properties (methods). 
There is a prebuilt library already available in the download section for version 1.8 adjusted for Android: http://snakeyaml.googlecode.com/files/snakeyaml-android-1.8-SNAPSHOT.jar (thanks to Alexander Maslov), but I wanted to use the latest code. 
The adjustment for Android is quite straight forward:
* Remove all the imports of java.bean package components
* Remove the throws IntrospectionException from methods declarations in few files (grep, there are only 4 .java files to change 
* In the introspector/PropertyUtils.java adjust the getPropertiesMap method implementation, throw away the use of Introspector. Yep, the library will not support java getters/setters methods serialization after that, only fields, but that worked fine for me.
* Remove introspector/MethodProperty.java file
* If you want to save some time and don't care building and running tests (I know it's wrong, but still), then you can build the .jar library with mvn package -DskipTests=true command.
* Add the final .jar file into your Android project and enjoy YAML on Android

Update (02-22-2012):
Alexander just landed the patches for Android (http://code.google.com/p/snakeyaml/source/list).
Now the latest source code can be easily built with one command: mvn package -Pandroid or to skip the tests mvn package -Pandroid -DskipTests=true. After the build,  just use snakeyaml-1.11-SNAPSHOT-android.jar.

2 comments:

maslovalex said...

Hi.

Thx for your interest in snakeyaml.

I have added "android" profile to snakeyaml pom and few patches needed to build it for android.

Now it is possible to build latest snakeyaml for android using -Pandroid

-alex

Alex said...

Thanks a lot Alex!