Running a Haskell program on the Android OS

32,889

Solution 1

A language that has recently come to my attention is Eta.

Eta's compiler is a fork of GHC 7.10 which has a JVM backend. It is possible to use the generated JAR files to write Android apps and even use its Foreign Function Interface to call native Android Java libraries.

Brian McKenna has written a blog post about how to configure an Android Studio project to use an Eta library.

Solution 2

How you do it is by first getting a Haskell compiler which can target C with the android NDK which comes with a GCC port for ARM architectures. JHC can trivially do this with a very small inf style file which describes the platform (word size, c-compiler, etc) I've done this with the Wii homebrew dev kit and it was quite easy. However jhc still has some stability issues with complex code such as using a monad transformer stack with IO but jhc has been improving a lot over the last 6 months. There is only one person working on JHC I just wished more people could help him.

The other option is to build an "unregistered" port of GHC targeting the ndk gcc, this is a lot more involved process because GHC is not a true cross-compiler at the moment and you need to understand the build system what parts you need to change. Another option is NHC which can cross-compile to C, like GHC you need to build nhc targeting a C compiler, NHC does not have many Haskell extensions like GHC.

Once you have Haskell compiler targeting NDK GCC, you will need to write bindings to either the android NDK JNI glue code framework (added since android 2.3) or you must write JNI glue code between Java-C-Haskell, the former option is the easier solution and if I remember correctly might actually be backwards compatible with previous versions of Android below 2.3.

Once you have this you must build Haskell code as shared library or static library which gets linked into the NDK java glue code (which itself is a shared library). As far as I'm aware you can not officially run native executables on android. You could probably do it with a rooted phone, thus I assume this means you can not distribute native executables on the app store even when the NDK gcc port can generate native executables just fine. This also probably kills the option for using LLVM unless you can get the NDK JNI working with LLVM.

The biggest hurdle isn't so much of getting a Haskell compiler for android (which is still a big hurdle) the biggest problem is that some one needs to write binding APIs for NDK libraries which is a huge task and the situation is worse if you need to write android UI code because there are no NDK APIs for this part of the android SDK. If you want to do android UI code in Haskell somebody will have to write Haskell bindings to Java through JNI/C. Unless there is a more automated process to writing binding libraries (I know there are some, they are just not automated enough for me) then chances of some one doing it are quite low.

L01man: Is there a tutorial about how to do this? For the first part, I understand I have to download JHC. What do I have to write in the inf file and how to use it?

Please note before I answer this question I haven't used jhc for quite sometime since I originally wrote this and newer versions have been released since so I do not know how stable jhc currently is when it comes to code generation of more complex Haskell programs. This is a warning to anyone before you consider making a large Haskell program with JHC, you should do some small tests before you go full on.

jhc does have a manual http://repetae.net/computer/jhc/manual.html and a section on setting-up cross-compilation and .ini file with options: http://repetae.net/computer/jhc/manual.html#crosscompilation.

L01man: The second part is an alternative to the first. I don't know how to do what you said in the third.

Before you begin you should have some knowledge of C and be comfortable with using the Haskell foreign function interface (FFI) and tools such as hs2c. You should also be familiar with using the Android NDK and building .apk with shared libraries. You will need to know these to interface between C-Haskell, Java/C-Haskell and develop Haskell programs for Android that you can officially distribute/sell on the market store.

L01man: I understand that its goal is to create a binding for the Android API. But... does the 4th part says we can't make .apk with Haskell?

.apk is just an app package file format and is built with the tools that come with the Android SDK (not NDK), this has very little to do building the binaries itself. Android packages can contain native shared libraries, this what your Haskell program will be and the native shared/static libraries are generated via the Android NDK.

Solution 3

There is https://github.com/neurocyte/android-haskell-activity demonstrating Haskell code running.

Solution 4

There is https://github.com/conscell/hugs-android a port of HUGS Haskell interpreter to Android.

Solution 5

I once came across the same Reddit thread, but it was old, and comments were closed. I sent a message to the OP, but am not sure whether it reached the recipient. My suggestion here (may work for older Androids where native activities were not possible).

I (developed in Haskell some time ago, but currently switched to Smalltalk) am currently developing a port of Squeak VM to Android. The way I am doing this is similar to what might be dealt with in a haskell-on-android project: a lump of C code which needs to be called from Java part of the application (basically all that can be done in Android is to handle various events; an application cannot poll for events itself and does not have any event loop). In my case the code is generated by the Squeak VM building tools, in the case of haskell on android this will be output from GHC of JHC or whatever front end used. This repo may be worth looking at:

http://gitorious.org/~golubovsky/cogvm/dmg-blessed/trees/master/platforms/android/project

Under "src" there is the Java code which provides for user events interception and sending them to the native code (see the CogView class). The C code of the VM itself is not completely there (see squeakvm.org, the Cog branch for that), but one may get the idea. One also might look under http://gitorious.org/~golubovsky/cogvm/dmg-blessed/trees/master/platforms/android/vm which is the C frontend to the interpreter (including user events handling, some timekeeping, etc.)

Hope this helps.

Dmitry

Share:
32,889

Related videos on Youtube

Robert Massaioli
Author by

Robert Massaioli

I have been programming for over 8 years in a variety of languages including: C C++ Java Haskell Ruby Perl Javascript And well versed in tools and markup languages such as: Bash / Zsh Shell Scripting Web Programming (CSS, HTML, Networking and Sysadmin Stuff) And a whole host of other knowledge gained over time. I like building tools that help people.

Updated on February 28, 2020

Comments

  • Robert Massaioli
    Robert Massaioli about 4 years

    Forenote: This is an extension of the thread started on /r/haskell

    Lets start with the facts:

    • Android is one awesome Operating System
    • Haskell is the best programming language on the planet

    Therefore, clearly, combining them would make Android development that much better. So essentially I would just like to know how I can write Haskell programs for the Android OS. My question is:

    How can I get a Haskell program to execute/run on the Android OS?

  • Phil
    Phil about 13 years
    I am by no mean an Android expert. But today I came across this new class called NativeACtivity since API level 9 developer.android.com/reference/android/app/NativeActivity.h‌​tml. They say it can be used for implementing activities purely in native code. I wonder how relevant/useful this is for our purpose? Does this imply no need for interaction between Haskell and Java?
  • snk_kid
    snk_kid about 13 years
    @Po The NativeActivity is part of the android NDK glue code framework (android 2.3) of which I've been writing about. It will allow you to write all your code in C/C++ but you will not have a native executable, you will have a shared library which gets called into from Java. If you wrote Haskell bindings to NativeActivity you would not need to write bindings between Java & Haskell but as I've mentioned the NDK APIs are subset of the full Java APIs, there are no native APIs for the standard android UI for example you would have to write your own in OpenGL (ES) or write JNI-Haskell bindings.
  • L01man
    L01man almost 12 years
    Is there a tutorial about how to do this?
  • L01man
    L01man almost 12 years
    For the first part, I understand I have to download JHC. What do I have to write in the inf file and how to use it? The second part is an alternative to the first. I don't know how to do what you said in the third. I understand that its goal is to create a binding for the Android API. But... does the 4th part says we can't make .apk with Haskell?
  • snk_kid
    snk_kid almost 12 years
    @L01man I've answered your question in the main answer because of the character limit in the comments.
  • L01man
    L01man almost 12 years
    Thank you very much. Unfortunately my goal is to make .apk so I think I'll use Java :(.
  • Ingo
    Ingo over 11 years
    In fact, there exists a showcase Android app written in Java and Frege, details are here groups.google.com/forum/#!topic/frege-programming-language/…
  • Robert Massaioli
    Robert Massaioli almost 11 years
    Son of a ... somebody actually did it! Kudos.
  • Robert Massaioli
    Robert Massaioli almost 11 years
    I'm going to take a closer look at this soon. If it seems legit then I am going to change the marked answer to this one.
  • gliptak
    gliptak almost 11 years
    Robert, it does seem legit. But neurocyte doesn't seem to offer detailed instructions on the build. Read github.com/neurocyte/android-haskell-activity/issues/1