注意:这是在/r/haskell上启动的线程的扩展
让我们从事实开始:
Android是一个很棒的操作系统 Haskell是世界上最好的编程语言
因此,显然,将它们结合起来将使Android开发变得更好。我想知道如何为Android OS编写Haskell程序。我的问题是:
如何让Haskell程序在Android操作系统上执行/运行?
注意:这是在/r/haskell上启动的线程的扩展
让我们从事实开始:
Android是一个很棒的操作系统 Haskell是世界上最好的编程语言
因此,显然,将它们结合起来将使Android开发变得更好。我想知道如何为Android OS编写Haskell程序。我的问题是:
如何让Haskell程序在Android操作系统上执行/运行?
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.
另一种选择是构建一个针对ndk gcc的“未注册”GHC端口,这是一个更复杂的过程,因为GHC目前还不是一个真正的交叉编译器,你需要了解构建系统需要更改哪些部分。另一个选择是NHC,它可以交叉编译到C,就像GHC一样,你需要构建针对C编译器的NHC, NHC没有像GHC那样的许多Haskell扩展。
一旦你有Haskell编译器针对NDK GCC,你将需要写绑定到android NDK JNI胶水代码框架(添加自android 2.3)或你必须写JNI胶水代码之间的Java-C-Haskell,前一个选项是更容易的解决方案,如果我没记错可能实际上向后兼容以前版本的android低于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:有关于如何做到这一点的教程吗?为 第一部分,我知道我必须下载JHC。我要做什么 写在inf文件和如何使用它?
在回答这个问题之前请注意,自从我最初写这篇文章以来,我已经有很长一段时间没有使用jhc了,而且更新的版本已经发布了,所以我不知道jhc目前在更复杂的Haskell程序的代码生成方面有多稳定。在考虑使用JHC编写大型Haskell程序之前,这是对任何人的一个警告,在完全开始之前,您应该做一些小测试。
JHC确实有一个手册http://repetae.net/computer/jhc/manual.html和一个关于设置交叉编译和.ini文件的部分,选项:http://repetae.net/computer/jhc/manual.html#crosscompilation。
第二部分是第一部分的替代方案。我不知道怎么做你说的 第三。
在开始之前,您应该对C有一定的了解,并且能够熟练使用Haskell外部函数接口(FFI)和hs2c等工具。您还应该熟悉使用Android NDK和使用共享库构建.apk。你需要知道这些,才能在C-Haskell, Java/C-Haskell之间进行交互,并为Android开发Haskell程序,以便在市场商店上正式发布/销售。
L01man:我知道它的目标是创建一个绑定 Android API。但是…第四部分说我们不能用。apk吗 Haskell吗?
.apk只是一个应用程序包文件格式,是用Android SDK(不是NDK)自带的工具构建的,这与构建二进制文件本身没有什么关系。Android包可以包含原生共享库,这就是你的Haskell程序,原生共享/静态库是通过Android NDK生成的。
我曾经在Reddit上看到过同样的帖子,但它是旧的,而且评论是关闭的。我给OP发了一条消息,但不确定是否到达了收件人。我在这里的建议(可能适用于旧的android,因为原生活动是不可能的)。
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
在“src”下面是Java代码,用于截取用户事件并将其发送到本机代码(参见CogView类)。虚拟机本身的C代码并不完全在那里(参见squeakvm.org, Cog的分支),但人们可能会明白这一点。你也可以在http://gitorious.org/~golubovsky/cogvm/dmg-blessed/trees/master/platforms/android/vm下查看,这是解释器的C前端(包括用户事件处理,一些计时等)。
希望这能有所帮助。
德米特里•
I think the general answer should come from source->source transformations, since loading specially compiled shared objects seems to be a bit of a kludge (involving ghc->c and a c->java step in the answers above). This question thus falls under the heading of Haskell on the JVM, which has been tried (with one step as a Java intermediate representation) and discussed at length. You could use frege if the libraries you need compile there. The only remaining steps would be the beginnings of the Android framework API translated into IO() actions and maybe a wrapper for building the manifest xml and apk.
最近引起我注意的一种语言是Eta。
Eta的编译器是GHC 7.10的一个分支,它有一个JVM后端。可以使用生成的JAR文件来编写Android应用程序,甚至可以使用其外部函数接口来调用原生Android Java库。
Brian McKenna写了一篇关于如何配置Android Studio项目以使用Eta库的博客文章。