www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - arsd.jni

reply jni <my.email gmail.com> writes:
Friends, I have decided to use D lang to link with the Android 
NDK. I can make an app that can use the android platform's 
functions and the android site says they are available in C for 
linux and windows! Making apps in android studio or 2 other 
programs I have used before is easy but now I require D because 
of the advantages  quality innovations I have written inside a D 
library, second I would use the D std library for using curl, and 
it needs to be in an apk for now and maybe in the years to come I 
will switch to using a binary executable. As you can see, linking 
with the NDK from D is the one possible outcome.

I have succesfully linked with the NDK and learned valuable linux 
programming along the way, but before I get too over-enthusiastic 
and joyous and get ahead of myself to make an error in trying to 
use the optional Android Studio together with  the arsd.jni, 
please, I wish to share with you my questions and concerns for 
the best way to move forward with this massive undertaking this 
thing, and how to go about calling android platform functions 
using arsd.jni and other D lang technologies from code.Dlang.org 
that may be useful to me.

For starters I wish to call the wifi functions for checking 
internet availability. How can I achieve this most efficiently? 
Then there are the maven questions because Android Studio does 
many of these automatically, and from my experience does an 
annoyingly gross job at it, btw unless you use all of their 
official tools. How do I use the maven for using jars or an 
Apache repository for server communication? I am using gradle 
with all this and everything works fine. Any suggestions on how 
to start this thing is welcome. I will connect through SSL with a 
remote server and print a log on the server side. On the Android 
side only connect and wait for further data to be recieved and if 
the stream finishes then it should stop the app. That part will 
mean sleeping the app until when it is relaunched. The server is 
php code.

I tried to add in correct place on forums. Thank you.
Dec 10 2022
next sibling parent jni <my.email gmail.com> writes:
On Saturday, 10 December 2022 at 22:53:14 UTC, jni wrote:
 Friends, I have decided to use D lang to link with the Android 
 NDK. I can make an app that can use the android platform's 
 functions and the android site says they are available in C for 
 linux and windows! Making apps in android studio or 2 other 
 programs I have used before is easy but now I require D because 
 of the advantages  quality innovations I have written inside a 
 D library, second I would use the D std library for using curl, 
 and it needs to be in an apk for now and maybe in the years to 
 come I will switch to using a binary executable. As you can 
 see, linking with the NDK from D is the one possible outcome.

 I have succesfully linked with the NDK and learned valuable 
 linux programming along the way, but before I get too 
 over-enthusiastic and joyous and get ahead of myself to make an 
 error in trying to use the optional Android Studio together 
 with  the arsd.jni, please, I wish to share with you my 
 questions and concerns for the best way to move forward with 
 this massive undertaking this thing, and how to go about 
 calling android platform functions using arsd.jni and other D 
 lang technologies from code.Dlang.org that may be useful to me.

 For starters I wish to call the wifi functions for checking 
 internet availability. How can I achieve this most efficiently? 
 Then there are the maven questions because Android Studio does 
 many of these automatically, and from my experience does an 
 annoyingly gross job at it, btw unless you use all of their 
 official tools. How do I use the maven for using jars or an 
 Apache repository for server communication? I am using gradle 
 with all this and everything works fine. Any suggestions on how 
 to start this thing is welcome. I will connect through SSL with 
 a remote server and print a log on the server side. On the 
 Android side only connect and wait for further data to be 
 recieved and if the stream finishes then it should stop the 
 app. That part will mean sleeping the app until when it is 
 relaunched. The server is php code.

 I tried to add in correct place on forums. Thank you.
Friends I am troubling you with too simple of a message. Please allow me to not be dissapointing myself here. Friends, mainly the focus of this very simple project is I will try to use the Android NDK "framework" API, as it is called, once I have linked succesfully, as I have already tested, next all that is left is to use the Android platform's NDK API from D lang code because I am trying my best not to use Java. I was wanting to include the JNI for good measure but was already planning to use the NDK API with extern(C). I thought it would be straight-forward since I knew that the Android platform used jni in the OS, but after doing some digging all I could find was the API reference over on android.com. There isn't much complication to be done only I was catching myself guessing how to go about it most efficiently for such a simple and small project. I would try to call the NDK API functions since it is the most low-level and C-like functional programming. I know much less of Java and will try to write the minimum I can. What I need is D lang because I have a D module to include into the app, there may be code.dlang.org packages that I may want to use, I will be using the std lib curl functions, there is the possibility of having to include functions from a seperate C library in the future if all goes well, the need to use maven for an Apache project's client and adding 2 other jars for it that are personal libraries, the possibility of using Android Studio if things get rocky. As you see there is no other way. I must use D or find something else to do. A small project as this that I would like only simplicity, any recommendations or suggestions on how would be the best way to go through with this?
Dec 10 2022
prev sibling next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
I don't know how to do much of anything on Android, but if you 
can post a small Java code example, I can suggest how to use it 
from D. You can bind many classes through the jni.

The biggest limitation is you can't do callbacks or subclasses 
through android jni, so it is kinda limited for interactive 
components.
Dec 11 2022
parent reply jni <my.email gmail.com> writes:
On Sunday, 11 December 2022 at 14:24:18 UTC, Adam D Ruppe wrote:
 I don't know how to do much of anything on Android, but if you 
 can post a small Java code example, I can suggest how to use it 
 from D. You can bind many classes through the jni.

 The biggest limitation is you can't do callbacks or subclasses 
 through android jni, so it is kinda limited for interactive 
 components.
 Always prefer to use Java code instead of NDK library code.
 I'm not saying to not use D, I'm saying that using NDK libraries
 such opensl es asset manager other ones are really trickier to 
 use.
 If you're just starting to code for android ndk
 prefer doing the work inside java and using D to call
 the java method that you need. This is the main advice
 I can give you. And never use native activity.
 Too much work for nothing.
Just as I thought. I had too much of Java years ago and I am not going through that again and will have to use the documentation! I would rather use Javascript! I kid. I know the hate. I understand, but it's not so bad I think. I make apps in Javascript easily. No problem at all. This occasion? It is not possible. On the android forums they suggested I use a generator on the files I need from the NDK, but I will not do that and I will do the classes one by one by hand as I use them. I can't use Java. Like an old girlfriend once I say enough is enough I will go and I don't look back. I do like it better than Kotlin from what I have seen, so that is why I choose to do it all in D. I will look through the NDK for the classes I need then. The two things that were most important for a reply from you lot are exactly on using these header files of inside the NDK? of which I only have used extern(C) on the moments after learning the D language. Too many years ago. That and I don't mind writing the boilerplate code as you say maybe I could try binding some if I can figure out how without writting any Java. That is why I was trying to avoid to use the jni. I mostly worry about keeping things maintainable for when I decide to use the module again for some other android project that I can not do with Javascript. The boilerplate is easy but Then the other part is a problem for me is the necessary other Java classes. They are not part of the NDK so the only way to load the jar is to use jni? Is that correct? I saw a jartoD function in arsd.jni but I wasn't sure how it fit into the lib or what it was used for "translator"? I wasn't sure. For the Apache client library and the other 2 jar classes I will use with it then writting all the Java binding seems inevitable. This makes me want to use D more and more. A much better language in my opinion. If I ever decide that I should to use a C library as a replacement for the D curl library then that will be additional work. I was wanting to ask the experts to see the best way to do these but I guess there is no easy way. On github there was what I saw a partial NDK headers for D project but the classes I need were not there. I prefer NDK livrary code because it is more acceptable for me to read and understand the functional programming of C. All the classes of the framework API in the SDK give me a headache! The NDK is much nicer to menin that way. What I want to do is fetch a file from the internet! That's it! How did I get in this mess?
Dec 11 2022
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Monday, 12 December 2022 at 01:19:23 UTC, jni wrote:
     The boilerplate is easy but Then the other part is a 
 problem for me is the necessary other Java classes. They are 
 not part of the NDK so the only way to load the jar is to use 
 jni? Is that correct?
I haven't updated this for a while,but I translated the whole api back in 2020 for my d_android package https://code.dlang.org/packages/d_android see for example: https://github.com/adamdruppe/d_android/blob/master/java_bindings/android/java/android/net/ConnectivityManager_d_interface.d (but be warned if you import these it can be very slow to compile, so just bringing only the classes you need can be much faster) I also translated the ndk headers to D back then: https://github.com/adamdruppe/d_android/tree/master/translated_headers/android/ndk But I never used android beyond hello world so i just don't know THAT much about it.
Dec 11 2022
parent reply jni <my.email gmail.com> writes:
On Monday, 12 December 2022 at 01:28:42 UTC, Adam D Ruppe wrote:
 On Monday, 12 December 2022 at 01:19:23 UTC, jni wrote:
     The boilerplate is easy but Then the other part is a 
 problem for me is the necessary other Java classes. They are 
 not part of the NDK so the only way to load the jar is to use 
 jni? Is that correct?
I haven't updated this for a while,but I translated the whole api back in 2020 for my d_android package https://code.dlang.org/packages/d_android see for example: https://github.com/adamdruppe/d_android/blob/master/java_bindings/android/java/android/net/ConnectivityManager_d_interface.d (but be warned if you import these it can be very slow to compile, so just bringing only the classes you need can be much faster) I also translated the ndk headers to D back then: https://github.com/adamdruppe/d_android/tree/master/translated_headers/android/ndk But I never used android beyond hello world so i just don't know THAT much about it.
Nice. Some of those names do look different but I will give it a try. Your arsd.jni library can be used to translate the headers then that is helping the cause already. I am not sure if the names are different because of seperate APIs having different naming for classes? It's good. But you did the java bindings by hand or is there a generator in arsd.jni for that too? I am interested in using your library anyways if I decide to do some of it by hand, if only to verify that what I did was there correctly written. This is good but if it can be used on any jar then it is even better. Java interfaces are easy and the naming scheme for jni is not so bad but I was trying to write as much in D as possible forward looking in a situation where I can use it again. I haven't taken a look at the client code but I already know that it will be substantial so I hope it works and I am liking Java more already after this. The d_android Java bindings are written in D as I was aiming to do from what I can see it is basicly what I planned to do so it is perfect for me. Thank you for your time.
Dec 12 2022
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Monday, 12 December 2022 at 11:17:47 UTC, jni wrote:
 It's good. But you did the java bindings by hand or is there a 
 generator in arsd.jni for that too?
It does it automatically. You compile jni.d with `-version=WithClassLoadSupport` and then write a main function that calls `jarToD("path/to/file.jar", "whatever.d.name", "output_directory", JavaTranslatorConfig(true, true, true));` Or something like that. It has been 3 years lol. But I put some docs in there too http://arsd-official.dpldocs.info/arsd.jni.jarToD.html http://arsd-official.dpldocs.info/arsd.jni.JavaTranslationConfig.html For the C .h files, I used the `dstep` program https://github.com/jacob-carlborg/dstep/ with a bit of hand edits after.
Dec 12 2022
next sibling parent jni <my.email gmail.com> writes:
On Monday, 12 December 2022 at 15:03:13 UTC, Adam D Ruppe wrote:
 On Monday, 12 December 2022 at 11:17:47 UTC, jni wrote:
 It's good. But you did the java bindings by hand or is there a 
 generator in arsd.jni for that too?
It does it automatically. You compile jni.d with `-version=WithClassLoadSupport` and then write a main function that calls `jarToD("path/to/file.jar", "whatever.d.name", "output_directory", JavaTranslatorConfig(true, true, true));` Or something like that. It has been 3 years lol. But I put some docs in there too http://arsd-official.dpldocs.info/arsd.jni.jarToD.html http://arsd-official.dpldocs.info/arsd.jni.JavaTranslationConfig.html For the C .h files, I used the `dstep` program https://github.com/jacob-carlborg/dstep/ with a bit of hand edits after.
I had jni.d already, not so sure if it was from an earlier module that I have worked on but I am pretty sure that file was already it was there. I don't know what I could've used it for. With Dstep I heard of it before maybe 6 years ago and all this years I thought it was for Objective C, that is why I had to do those myself using extern, but it was only a few functions. But this java translator it interests me greatly how with such a small library it can do that. Does it import a VM that is different from the JVM for the translation or did you sit down and write the Java implementation for it yourself? Because it seemed like a small library when I last took a look. Very nice. Android is the only phone besides buying a $1,000 iphone and for me it doesn't really make much sense. Android is much more better for programming and I myself don't mind its quirks. It's not a perfect phone but the Kotlin and Java options are just not what I need in my line of work. Probably some day I will have no choice but to learn Java again! That is what I expect will happen in the future and it is laughable, but for now it is a much better thing to have the D language available to me for writing this simple scripts that I have to do on occasion. If I write something worthwhile I will be sure to put it on code.dlang.org and name it Triple-D.
Dec 13 2022
prev sibling parent jni <my.email gmail.com> writes:
On Monday, 12 December 2022 at 15:03:13 UTC, Adam D Ruppe wrote:
 On Monday, 12 December 2022 at 11:17:47 UTC, jni wrote:
 It's good. But you did the java bindings by hand or is there a 
 generator in arsd.jni for that too?
It does it automatically. You compile jni.d with `-version=WithClassLoadSupport` and then write a main function that calls `jarToD("path/to/file.jar", "whatever.d.name", "output_directory", JavaTranslatorConfig(true, true, true));` Or something like that. It has been 3 years lol. But I put some docs in there too http://arsd-official.dpldocs.info/arsd.jni.jarToD.html http://arsd-official.dpldocs.info/arsd.jni.JavaTranslationConfig.html For the C .h files, I used the `dstep` program https://github.com/jacob-carlborg/dstep/ with a bit of hand edits after.
Hello, friends. I am ready to make my engineering decisions. I have been up and down the d-android repos and I have a firmer grasp as to what the jni actually is, and I presume that it is used in the Android NDK. In the months ago that I opened the NDK I noticed the C files but I did not go in for closer inspection. I have only one more question for you so that I can get to a better engineering decision for the app I will build. Are the differences negligible between using the jni and writing the app in C? Does the NDK provide both the jni and C libraries then you Adam chose to use jni instead of the C or are they together binded in the NDK? eg. The C library uses jni? I know the Android OS uses jni, but I thought the native APIs were native to the OS, not written ontop with the jni.
Dec 17 2022
prev sibling parent Hipreme <msnmancini hotmail.com> writes:
On Saturday, 10 December 2022 at 22:53:14 UTC, jni wrote:
 Friends, I have decided to use D lang to link with the Android 
 NDK. I can make an app that can use the android platform's 
 functions and the android site says they are available in C for 
 linux and windows! Making apps in android studio or 2 other 
 programs I have used before is easy but now I require D because 
 of the advantages  quality innovations I have written inside a 
 D library, second I would use the D std library for using curl, 
 and it needs to be in an apk for now and maybe in the years to 
 come I will switch to using a binary executable. As you can 
 see, linking with the NDK from D is the one possible outcome.

 [...]
Always prefer to use Java code instead of NDK library code. I'm not saying to not use D, I'm saying that using NDK libraries such opensl es, asset manager, other ones are really trickier to use. If you're just starting to code for android ndk, prefer doing the work inside java and using D to call the java method that you need. This is the main advice I can give you. And `never` use native activity. Too much work for nothing.
Dec 11 2022