Wednesday, August 04, 2010

HACK: Adding JAR & JNI Libraries to a NBAndroid Project

In general, the Android Plugin for Netbeans (NBAndroid) is a great alternative to the the official ADT Plugin for Eclipse provided by Google. However, as of the timing writing it has two annoying bugs: it does neither support Java nor native libraries to be installed with the application package (.apk) on the phone. Fortunately, there is an easy workaround for both:

1. Java Libraries

Actually, this already works partly as adding .jar files using the GUI allows compilation of dependent code. The problem is that the contained .class files are not converted and bundled. The result is a ClassNotFoundException when running the application. Overriding the "-dex" target in the build.xml does the job:

<target depends="init,compile,-pre-pre-jar,-pre-jar" name="-dex">
    <pathconvert pathsep=" " property="javac.classpath.with.spaces">
        <path path="${javac.classpath}"/>
    </pathconvert>
    <exec executable="${dx}" failonerror="true">
        <arg value="--dex"/>
        <arg value="--output=${basedir}/${intermediate.dex}"/>
        <arg value="--positions=lines"/>
        <arg file="${build.classes.dir}"/>
        <arg line="${javac.classpath.with.spaces}"/>
    </exec>
</target>
Source: This workaround is actually a re-post of Andrey Moiseev's comment on the bug report.

2. Native Libraries

Native libraries are usually found below an optional libs folder in the project directory, e.g. "libs/armeabi/libFoo.so". While some build scripts automatically add the contents to the .apk file, the one generated by the NBAndroid plug-in unfortunately does not. Again the solution is overriding a target by adding the following to the build.xml (requries an exisiting libs directory).

<target depends="init,-package-res-and-assets,-package-res-no-assets,-package-dex" name="-sign">
    <exec executable="${apkbuilder}" failonerror="true">
        <arg value="${basedir}/${dist.apk}"/>
        <arg value="-z"/>
        <arg value="${basedir}/${dist.apk}_"/>
        <arg value="-nf"/>
        <arg value="${basedir}/libs"/>
    </exec>
    <delete file="${dist.apk}_"/>
</target>

Workaround: Using Vertex-Buffer-Objects (VBOs) with OpenGL ES 2.0 on Android 2.2 (Froyo)

Starting with the Android 2.2 SDK the OpenGL ES 2.0 functionality is finally available. Unfortunately however, you can't really use vertex buffer objets (VBO) with it at the moment. The problem is that the class GLES20 is missing (overloaded) variants of glVertexArrtribPointer and glDrawElements with the last parameter of type int (instead of Buffer). These are required if a buffer object is bound to the VERTEX_ARRAY target or VERTEX_ELEMENT_ARRAY respectively.

The good news is that the Android graphics team already checked in a fix. However, because it requires API changes, they won't be able to push it until there is another system update. (Thanks to Chris Pruett for the info!)

For the the mean time, I wrote a few lines of code enabling the missing functionality through two Java-Native-Interface (JNI) calls (fix-GLES20.zip). In order to use it, you need to:

  1. Copy the zip file's content to your Android project folder.
  2. Download and install(unzip) the Native Development Kit (NDK).
  3. Run "$ANDROID_NDK/ndk-build -C %PATH_TO_PROJECT%"
This will create the file "libs/armeabi/libfix-GLES20.so" in your project's folder. Actually, this is all you need in addition to the file "src/fix/android/opengl/GLES20.java". Now you can add "import static fix.android.opengl.GLES20.glVertexAttribPointer|glDrawElements;" to your java sources and use the missing functionality.

Note: The file "libs/armeabi/libfix-GLES20.so" should get included in the application package (.apk) automatically with most build scripts. You can print and verify the content it with: "$jar -rf %PATH_TO_APK_FILE%". If for some reason the file is not listed, simply make a copy of the .apk file and run: "$ANDROID_SDK/tools/apkbuilder %PATH_TO_APK_FILE% -z %PATH_TO_APK_COPY% -nf %PATH_TO_PROJECT%/libs".

Friday, November 27, 2009

MS Fonts in Google Documents on Ubuntu

A major reason why I love google documents is that I can access all the files from any computer regardless of the operating system. In principle this works great for me but unfortunately changing the text font (e.g. to Courier New) doesn't work on ubuntu. Although I still hope there is something goggle can do, the intermediate solution is using the 'Installer for Microsoft TrueType core fonts':

sudo apt-get install ttf-mscorefonts-installer

P.S. Don't forget to restart you browser afterwards.

Friday, September 18, 2009

MATLAB/Octave Utils: submat, grouped

Totally forgot to share these two MATLAB/Octave functions I wrote some time ago:
submat: accessing a sub-matrix of an unknown multi-dimensional matrix
grouped: creating record like data structures from arrays grouped by an index cell

Actually, I still wonder if or why this functionality is not part of the core library or even the 'language syntax '? Anyhow, I couldn't find it so I wrote the two functions myself. (please tell me if you know about a predefined equivalent or a better solution)


1. submat ( submat.m )

In Matlab everything is a (potential) N-dimensional matrix of elements such as doubles, cells or structs. Sub-indexing is easy, unless you try to program in a generic way, e.g. with high-order functions. The problem is that traditional sub-indexing requires you to know the element type (cell or not) and dimensions of the matrix. So I came up with a function submat, which basically does sub-indexing in the highest dimension of an unknown type:

A = [ 1, 2, 3; 4, 5, 6 ];
submat(A,2:3) % same as A(:,2:3)
-> [ 2, 3; 5, 6 ]

B = { 'Hello', 'submat'; 'Goodbye', '(:)' };
submat(B',1) % same as B'{:,1}
-> { 'Hello', 'submat' }



2. grouped ( grouped.m )

The other thing I missed was a simple way to group data arrays to structs. Of course this can easily be done within a for loop, but using the group function eliminates the code repetition. Also, it can be be used in two ways:

First, it allows to simply group equal indices together.

grouped( [ 1, 2, 1, 3, 1, 2, 3] )
-> { [ 1, 3, 5 ], [ 2, 6 ], [ 4, 7 ] }

So far nothing special, but its main purpose is to create a custom struct array.

%artists = { 'Beatles', 'Bob Dylan' };
%records

record_artist_ids = [1, 1, 2, 2, 1];
% or the result of group(record_artist_ids)
record_artist_ids = { [1, 2, 5], [3, 4] };
title = { 'White Album', 'Rubber Soul', 'Blood on the Tracks', 'Blonde on Blonde', 'Revolver' };
year = [ 1968, 1965, 1975, 1966, 1966 ];

artist_records = grouped ( record_artist_ids, 'title', title, 'year', year)

-> artist_records(1).title = { 'White Album', 'Rubber Soul', 'Revolver' }
artist_records(1).year = [ 1968, 1965, 1966 ]
artist_records(2).title = { 'White Album', 'Rubber Soul', 'Revolver' }
artist_records(2).year = [ 1975, 1966 ]

Saturday, April 12, 2008

Shell History Meme

Reading YC's post about the meme happening at the Fedora Planet, I also gave it a try:

history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head

113 cd
88 ls
53 hg
38 ant
28 fsc
23 ln
17 ssh
13 java
10 ../../../bin/java
9 scala

In words:
I am guy who likes to be at many places [cd] and to know the surroundings [ls] - I try to keep record of everything [hg]. Further, I don't want to bother with annoying repetitive tasks: things should be done automatically [ant] and fast [fsc]. Mental links [ln] help me not to get lost while my mind is far away [ssh]. Finally, you know I used to program in Java [java,../../../bin/java] but nothing can stop the rise of Scala [scala]. ;-)

Saturday, February 02, 2008

A *new* Scala Bundle for Textmate

No doubt, the upcoming Scala Module will make Netbeans the No.1 Scala IDE. I use the current pre-release version on my linux box at work without any difficulties. Unfortunately, I have encountered problems running the recommended daily development builds of Netbeans on my private macbook pro. I hoped that I could at least use my favorite editor for MacOS X, Textmate, but the bundle shipped with Scala 2.6.1 still seems to be a dummy/placeholder. Well, instead of falling into a deep depression, I invested half a day to create a new bundle myself.

For those interested, please find the download below:

- The Scala Bundle
- An Example Project (with some sources ripped from scala-lang.org)

The main goal was to have simple syntax highlighting, which actually looks quite good using the 'Cobalt Theme'. (I'm not 100% sure that value:Type is always displayed correctly, but it seemed to work with the samples.)

Further, there is a template for an Ant-Script which includes a compile, run & deploy task. These can be executed as bundle commands (menu or shortcuts), if the file is called 'build.xml'. Customization should be easy with a few properties at the top of the file.

Finally, there are a few snippets to write new code quickly. E.g. typing

trait ->| Adder ->| def ->| add ->| Int ->| 2 ->| left ->| Int ->| right ->| Int

results in
/**
* Adder
*/
trait Adder
{
def add ( left : Int, right : Int ) : Int
}
without pressing two keys at the same time (no SHIFT)!

But of course you can sketch your own (and share them with me =) ) !

Thursday, October 18, 2007

Streams for Incremental and Tail-Recoursive Computing

Programming in Scala is a lot of fun for me, but I really love Streams. Their laziness allows really nice implementations of incremental algorithms and tail-recursive functions. the best thing is they can be used efficiently under different requirements: no matter if you are interested in each intermediate result or only the final one, you'll only need one implementations. =)

I hope the following example computing the factorial(s) using Streams explains it well:

Wednesday, October 17, 2007

Building Scala from the Sources

As I noticed that the actual Scala sources include a great enhancement for all RandomAccessSeq.Mutables, such as Array or ArrayBuffer: Now, the methods drop, take, slice and reverse return a RandomAccessSeq.MutableProjection, which allows cool things to be done in O(1), e.g. Array(1,2,3) reverse(0) = 10.

Unable to hold mysself back waiting, I decided try the latest version. All I had to do was to follow these instruction: Building Scala 2 from the sources. Et viola, one can already taste some of the features/fixes scheduled for Scala 2.6.1. =)