Erlang on Android

Sett 257 ganger
Hopp til første uleste melding

Dominic Letz

ulest,
17. sep. 2021, 07:45:5017.09.2021
til erlang-q...@erlang.org
Hey there,

just wanted to share another approach to running Erlang within an Android application that might be useful for others. In the quest to bring our companies app to Android we decided to use beam.smp binary as a shared library because on Linux (and Android for that matter) ELF binaries are just the same as shared libraries.

So with only updated environment flags for compilation and linking, we compiled OTP and then renamed beam.smp into liberlang.so to use within our app

Additional OTP compilation flags:
CFLAGS="-Os -fPIC" CXXFLAGS="-Os -fPIC" LDFLAGS="-z global"
for reproducibility the runtimes are generated in a GitHub project from Dockerfiles using GitHub CI:

Now our app is Elixir based and uses Elixir Phoenix LiveView web framework to generate a UI that the Andoird app is then presenting in a local WebView. But the same approach would work of course with a pure Erlang/cowboy setup.
Full Elixir Android app source code, with Erlang runtimes ready to build: https://github.com/elixir-desktop/android-example-app

I also would like to mention Jérôme de Bretagne who worked on making Erlang run on Android 10 and whose insights there were extremely helpful.

One issue that might affect Android compatibility is the dependency on the two external binaries erl_child_setup and inet_gethost. With Android 10 Google has made it significantly harder to ship executable binaries with your app. There is the workaround that I've found in Jérôme's repository to bundle erl_child_setup as "lib__erl_child_setup.so" to trick the Android application bundler into accepting the file as a library into the package and then creating a symlink that is named erl_child_setup after installation to make it executable again.

So this brings me to my question #1. Is there a way to remove the dependency on these external binaries of erl_child_setup and inet_gethost currently that anyone is aware of? Or will this require an OTP code change?

Question #2: Has anyone tried bundling Erlang into iPad/iPhone apps with any pointers to repos?

Cheers
--
// Dominic Letz | CTO
+49 163 9198 211 (.de)

DIODE

Mikael Pettersson

ulest,
17. sep. 2021, 08:03:4917.09.2021
til Dominic Letz, erlang-questions
On Fri, Sep 17, 2021 at 1:45 PM Dominic Letz <dom...@diode.io> wrote:
> So this brings me to my question #1. Is there a way to remove the dependency on these external binaries of erl_child_setup [...]

As I recall, erl_child_setup is there to avoid needing to fork()
inside the Erlang VM (when spawning off external commands via e.g.
os:cmd/1). Forking is a problem when one runs massively large
Erlang/VM instances, as we (Klarna) do. With erl_child_setup, the
fork() is done inside that much smaller helper process. So I think it
needs to stay, but the mechanics of starting it might need adapting
for Android.

I take it you don't use heart, because that's also a separate executable.

Dominic Letz

ulest,
18. sep. 2021, 06:25:4818.09.2021
til Mikael Pettersson, erlang-questions
Thanks, I guess I'll have to dig into the code to understand better. There must be a reason why OTP doesn't just fork early in the boot process to keep around another (or multiple other) child processes for the erl_child_setup use case. Maybe there is/was a platform where that is not an option. Once I get more time or when this gets urgent I'll revisit.

Anyone knows about iPhone targets?

Jérôme de Bretagne

ulest,
19. sep. 2021, 06:52:2219.09.2021
til Dominic Letz, erlang-questions
Hey Dominic,

> just wanted to share another approach to running Erlang within an Android application that might be useful for others. In the quest to bring our companies app to Android we decided to use beam.smp binary as a shared library because on Linux (and Android for that matter) ELF binaries are just the same as shared libraries.
>
> So with only updated environment flags for compilation and linking, we compiled OTP and then renamed beam.smp into liberlang.so to use within our app

This is a very clever approach, pulling in a minimal dependency when
integrating an Erlang VM into an Android project, great work!

> Additional OTP compilation flags:
> CFLAGS="-Os -fPIC" CXXFLAGS="-Os -fPIC" LDFLAGS="-z global"

Interesting use of "-Os" to optimize for size. Your usage of global
linking is well documented at :
https://github.com/elixir-desktop/runtimes/commit/fdefb0d6f047affd3087dedb47f86ddd6f326ae7
What about "-fPIC", did you face any issues when not passing it
explicitly? I'm asking as the current cross compilation options in
erl-xcomp-arm-android.conf and erl-xcomp-arm64-android.conf don't use
it when compiling with Clang/LLVM.

> I also would like to mention Jérôme de Bretagne who worked on making Erlang run on Android 10 and whose insights there were extremely helpful.

Thanks a lot for the mention :-) For those interested, here is the
issue discussing the Android 10 new limitation and describing the
approach used to make a full Erlang installation run on Android 10+
(while still being compatible with older Android releases):
https://github.com/JeromeDeBretagne/erlanglauncher/issues/2

> Question #2: Has anyone tried bundling Erlang into iPad/iPhone apps with any pointers to repos?
Personally, I haven't looked into this at all.

Cheers,
Jérôme

Dave Cottlehuber

ulest,
20. sep. 2021, 11:26:0920.09.2021
til erlang-q...@erlang.org
On Sun, 19 Sep 2021, at 10:51, Jérôme de Bretagne wrote:
> Hey Dominic,
>
> > just wanted to share another approach to running Erlang within an Android application that might be useful for others. In the quest to bring our companies app to Android we decided to use beam.smp binary as a shared library because on Linux (and Android for that matter) ELF binaries are just the same as shared libraries.
> >
> > So with only updated environment flags for compilation and linking, we compiled OTP and then renamed beam.smp into liberlang.so to use within our app
>
> This is a very clever approach, pulling in a minimal dependency when
> integrating an Erlang VM into an Android project, great work!
>
> > Additional OTP compilation flags:
> > CFLAGS="-Os -fPIC" CXXFLAGS="-Os -fPIC" LDFLAGS="-z global"
>
> Interesting use of "-Os" to optimize for size. Your usage of global
> linking is well documented at :
> https://github.com/elixir-desktop/runtimes/commit/fdefb0d6f047affd3087dedb47f86ddd6f326ae7

Great to see this knowledge being shared. thanks!

https://github.com/nanovms/erlang has a couple of handy hacks:

one a simple permissions warning, and the other to embed epmd & erlexec directly.

A+
Dave
Svar alle
Svar til forfatter
Videresend
0 nye meldinger