Developing and running D GUI app on Android

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jan 13 20:23:22 UTC 2021


On Wed, Jan 13, 2021 at 07:51:08PM +0000, aberba via Digitalmars-d-learn wrote:
[...]
> So Adam's tool setup is pretty clear (talked to him). What remains is
> calling Java classes and interacting with the Android's API. I know a
> little bit of Java but not enough Android. Just the calling
> conversation part.
> 
> Do you have a sample of how that works?

Android's APIs are mostly Java.  So what you'll need to do is to
interface with it via JNI.  The basic idea is:

1) To call D from Java, you'd define Java methods as native methods, and
   implement the native methods in D (using the JNI naming convention so
   that the JVM knows how to match them up) to make it accessible to
   Java code. In your Java class you'd have a static {} block that calls
   System.loadLibrary to load your D code into the JVM. If your entire
   class is in D, it may be possible to auto-generate the Java wrapper;
   Adam may be written a script for this, I'm not 100% sure.

2) To call Java from D, you'd use JNI. This involves a lot of
   boilerplate to marshal your arguments and/or convert them to
   Java-compatible types, and unmarshal/convert the results, and so
   lends itself very well to automation using D templates.  Thanks to
   Adam's jni.d, this is generally very painless: you just declare the
   Java class using an equivalent D class, and jni.d uses introspection
   to auto-generate the JNI boilerplate for you, then you can just call
   it as if it were a D object.

My own code predates Adam's jni.d, though, so I have my own templates
for taking care of the boilerplate.  If you want examples using jni.d
you'd best ask Adam. :-D

If you ever need to look under the hood, though, e.g. for debugging
purposes, take a look at the JNI spec and Android's NDK docs.


T

-- 
"I'm not childish; I'm just in touch with the child within!" - RL


More information about the Digitalmars-d-learn mailing list