Working Free Pascal android JNI example

classic Classic list List threaded Threaded
43 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Working Free Pascal android JNI example

Benjamin Jan Alexander Rosseaux-2

Hello,

I've done a working Free Pascal android JNI example (with working .apk
for current ARM-based Android device of the second device generation
(because with VFPv3, so Motorola Droid/Milestone, HTC Desire, Nexus One,
Samsung Galaxy S, and so on) inside)

http://vserver.rosseaux.net/stuff/FPCAndroidJNINativeTest.zip

It contains androidprt0.as as replacement for the empty arm/linux
dllprt0.as of the FreePascal RTL from the SVN, because otherwise the
Android Dalvik VM refuses to load the .so library because of
missed/absent global/import/export symbols (_haltproc_eabi, __strptr,
__operatingsystem_parameter_argv and so on).

It contains also a own JNI header port from the Android NDK, and own ARM
Toolchain hook binaries to inject some command line parameters to the
original "as" and "ld" binary calls. For "as": "--march=armv7-a
-mfloat-abi=softfp -mfpu=vfp3" and for "ld": "--fix-cortex-a8"

And important FPC parameters are -CpARMv6 -CfVFPV3

I've done it, because i'm needing my own ECMAScript 5th edition engine
called BESEN in a Android project of me.

Someone can make maybe a true patch from this for FreePascal for a
offical android target support in FreePascal. And my OpenGL ES 2.0
headers (what I did originally for Nokia N900 for Maemo 5) are already
in the FPC bugtracker, which should also under Android after a
single-line-change of the OpenGL ES library path.

Benjamin 'BeRo' Rosseaux


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Florian Klämpfl
Benjamin Jan Alexander Rosseaux schrieb:

>
> Hello,
>
> I've done a working Free Pascal android JNI example (with working .apk
> for current ARM-based Android device of the second device generation
> (because with VFPv3, so Motorola Droid/Milestone, HTC Desire, Nexus One,
> Samsung Galaxy S, and so on) inside)
>
> http://vserver.rosseaux.net/stuff/FPCAndroidJNINativeTest.zip
>
> It contains androidprt0.as as replacement for the empty arm/linux
> dllprt0.as of the FreePascal RTL from the SVN, because otherwise the
> Android Dalvik VM refuses to load the .so library because of
> missed/absent global/import/export symbols (_haltproc_eabi, __strptr,
> __operatingsystem_parameter_argv and so on).
>
> It contains also a own JNI header port from the Android NDK, and own ARM
> Toolchain hook binaries to inject some command line parameters to the
> original "as" and "ld" binary calls. For "as": "--march=armv7-a
> -mfloat-abi=softfp -mfpu=vfp3" and for "ld": "--fix-cortex-a8"
>
> And important FPC parameters are -CpARMv6 -CfVFPV3
>
> I've done it, because i'm needing my own ECMAScript 5th edition engine
> called BESEN in a Android project of me.
>
> Someone can make maybe a true patch from this for FreePascal for a
> offical android target support in FreePascal.

I'am not sure how this should be done: the fpc target is arm-linux so
android is some kind of sub architecture. However, FPC has no
infrastruture to support such sub targets/architectures. Maybe we should
add some -W switch which allows to load settings for a certain sub
architecture together with MACHINE_TARGET or something like this in the
makefiles.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Reimar Grabowski
In reply to this post by Benjamin Jan Alexander Rosseaux-2
On Sun, 27 Jun 2010 09:50:35 +0200
Benjamin Jan Alexander Rosseaux <[hidden email]> wrote:

>
> Hello,
>
> I've done a working Free Pascal android JNI example (with working .apk
> for current ARM-based Android device of the second device generation
> (because with VFPv3, so Motorola Droid/Milestone, HTC Desire, Nexus One,
> Samsung Galaxy S, and so on) inside)
This sounds very interesting. I only played a little with FPC and Android, meaning I build the crosscompiler (which was quite easy) and tested some simple command line stuff. Of course I could not build .apks so my programs had to be uploaded and started via adb, but at least they worked fine.
Unfortunately I currently have no time to play around with your code, but I will as soon as I can.
From your mail it is not clear which ld and as you used. I just took them from the Android NDK, you too?
Did you try if gdb and gprof are working?

All in all great work and perhaps you can claim the bounty:
http://wiki.lazarus.freepascal.org/Bounties#Cross-Compile_to_Android_on_Ubuntu_X64

My compiler runs on Ubuntu64, so yours should too and with JNI you are able to build real .apks which I think is good enough to claim the bounty.

Thanks for showing us
R.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Benjamin Jan Alexander Rosseaux-2
  Am 02.07.2010 02:11, schrieb Reimar Grabowski:
>  From your mail it is not clear which ld and as you used. I just took them from the Android NDK, you too?
> Unfortunately I currently have no time to play around with your code,
> but I will as soon as I can.

Under linux I'm using self-built arm binutils, and under windows I'm
using the YAGARTO "Yet another GNU ARM toolchain", because the original
android NDK ARM binutils binaries are very buggy, at least the ld linker
from it.

> Did you try if gdb and gprof are working?
>

So far when I had time for this :-)

> All in all great work and perhaps you can claim the bounty:
> http://wiki.lazarus.freepascal.org/Bounties#Cross-Compile_to_Android_on_Ubuntu_X64
>
> My compiler runs on Ubuntu64, so yours should too and with JNI you are able to build real .apks which I think is good enough to claim the bounty.
>

A lazarus android UI widget set is so far very difficult to make for
now, because android's UI concept is very different from another UI
concepts and therefore very difficult to adapt this concept for the LCL
"in the current state". But a alternative way would be the way over a
own OpenGL ES based GUI engine for android lazarus apps, but that would
break the android UI design concept philosophy.

Short: My android JNI freepascal stuff is rather only for to speedup
some things, for example DSP audio stuff, game rendering/complexlogic
code, etc. all what will be as Java code a lot of slower than freepascal
native code, but not for android UI things, sorry.

But Lazarus extending with a plain android JNI features (with automatic
injecting into a "host Java JNI caller stub" apk and apk resigning) will
be perharps posssible (for example for freepascal-based Android OpenGL
ES games and so on, where no native android UI stuff is needed).

Benjamin 'BeRo' Rosseaux
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Felipe Monteiro de Carvalho
Hello,

Did you try making executables for Android? It is possible to run
native executables from Java.

That would be somewhat better then running the code from a .so

Also a tutorial about how to create the cross-compiler and set
everything up would be excelent ...

>From your zip file it seams that you based your work on Windows, correct?

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Reimar Grabowski
On Sat, 6 Nov 2010 18:39:53 +0100
Felipe Monteiro de Carvalho <[hidden email]> wrote:

> Did you try making executables for Android? It is possible to run
> native executables from Java.
It is not my code but I know a bit about Android.
I don't know of any way running native executables from Java on this platform. It is definitly not officially supported.
You can run native FPC progs on Android devices via the development tools circumventing the official way to install software. You can start this progs from your pc but not from the device itself. Since you cannot distribute this kind of software via official channels (market and the like) it is not of much use. Also I don't really know how to interact with these kinds of programs or how to run a GUI.
 
> That would be somewhat better then running the code from a .so
True, but as it is not supported there is no better way than to run your code from a lib. At least it is native then and you get the performance improvements.
To my knowledge it is currently the best way to use FPC on Android. You must remember that Android apps are not really complete programs (as you know them) but some special Java classes that are run from an 'app manager'. You don't have the kind of control you know from conventional programs.

hih
R.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Felipe Monteiro de Carvalho
On Sat, Nov 6, 2010 at 8:46 PM, Reimar Grabowski <[hidden email]> wrote:
> I don't know of any way running native executables from Java on this platform. It is definitly not officially supported.

Here is a tutorial:

http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App

It just uses standard Android Java API calls AFAIK

> Since you cannot distribute this kind of software via official channels (market and the like) it is not of much use.

Google does not impose this kind of limit on market applications. As
long as you have the standard installer packages and has a stub Java
program calling our Pascal app it should be fine.

> Also I don't really know how to interact with these kinds of programs

The most workable that I found is through pipes between the Java and
the FPC app.

Sockets are also a possibility, but I think (not sure) they will
probably add more overhead then pipes.

> or how to run a GUI.

This is the tricky part. The obvious answer is OpenGL ES.

I would like to use the Surface Manager C API instead, but I haven't
found any documentation about it so OpenGL ES seams to be the workable
solution.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Graeme Geldenhuys-2
In reply to this post by Felipe Monteiro de Carvalho
On 6 November 2010 19:39, Felipe Monteiro de Carvalho wrote:
>
> Did you try making executables for Android? It is possible to run
> native executables from Java.

I'm very interested in this too. My next toy is going to be a Android
based tablet (mostly for ebook reading), but I would like to develop
for it too.

Any info would be much appreciated.


--
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net:8080/fpgui/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Reimar Grabowski
In reply to this post by Felipe Monteiro de Carvalho
On Sat, 6 Nov 2010 23:43:58 +0100
Felipe Monteiro de Carvalho <[hidden email]> wrote:

> Here is a tutorial:
>
> http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App
>
> It just uses standard Android Java API calls AFAIK
Thanks for the tutorial, but as you surely have read (see comments) there are problems using this approach. And I won't hardly call it officially supported as it is a little hackish.
 
> > Since you cannot distribute this kind of software via official channels (market and the like) it is not of much use.
>
> Google does not impose this kind of limit on market applications. As
> long as you have the standard installer packages and has a stub Java
> program calling our Pascal app it should be fine.
That is true but I was talking about running native FPC apps directly without calling them from Java.

> > Also I don't really know how to interact with these kinds of programs
>
> The most workable that I found is through pipes between the Java and
> the FPC app.
And this is fast enough for realtime interaction?
If this is the case then it is a really nice solution.
I am mostly interested in touch input and the sensors.

> > or how to run a GUI.
>
> This is the tricky part. The obvious answer is OpenGL ES.
When it comes to graphics OpenGL is always the answer. ;)

Do you have any working example code?

R.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Benjamin Jan Alexander Rosseaux-2
In reply to this post by Graeme Geldenhuys-2

Because maybe requested of few, here is the complete source code of the
android version of my BeRoXM mod/xm/wow player (which is also in the
android market), where the mod/xm/wow playerengine is implemented in
object pascal and compiled with freepascal, and the UI part stuff is in
Java.

The url is: http://rootserver.rosseaux.net/stuff/BeRoXMAndroidSrc.zip

I hope it's a good start example now for these, who do want to mix
object pascal code over the JNI bridge in their android app projects.

And I'm working on OpenGL ES 2.0 android game in he moment, which
contains 99% object pascal code and only 1% java code for "native objpas
code JNI library loading"+"Android input event hooking and redirect to
native code"+"OpenGL ES 2.0 context
creation"+"Realtime-calculated-Audiobuffer-from-native-objpas-code-to-Android-Java-Audio-API-redirecting",
but no more, so the remain of the code of my current android game
project is pure object pascal, gne logic, opengl es 2.0 renderer,
resource loading, threading (it has four threads, logic thread, render
thread, audio thread and android UI event thread), networking, and so on.

Regards,
Benjamin 'BeRo' Rosseaux

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Max Vlasov


On Thu, Nov 25, 2010 at 6:35 AM, Benjamin Jan Alexander Rosseaux <[hidden email]> wrote:

Because maybe requested of few, here is the complete source code of the android version of my BeRoXM mod/xm/wow player (which is also in the android market), where the mod/xm/wow playerengine is implemented in object pascal and compiled with freepascal, and the UI part stuff is in Java.

The url is: http://rootserver.rosseaux.net/stuff/BeRoXMAndroidSrc.zip

I hope it's a good start example now for these, who do want to mix object pascal code over the JNI bridge in their android app projects.



Benjamin,
great to here, as long as I see there's no dedicated page about your experience with writing in Pascal for Android (only found your post in another forum: http://www.pascalgamedevelopment.com/showthread.php?5818-BeRoXM-The-first-android-app-with-object-pascal-code), I hope the admins will allow and you probably will have time to write some info at fpc wiki.

Also I think that you as the developer who really did this, might add something useful to this sub-discussion: http://www.mail-archive.com/fpc-pascal@.../msg22483.html. Can you consider Android as the best mobile platform currently for a pascal developer?

Max Vlasov

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Felipe Monteiro de Carvalho
Hello,

How do you compile BeRoXM? Using ant?

Also, installing BeRoXM.apk failed in HTC Wildfire Android 2.1

Also, I couldn't find the source code for the Pascal part.

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Felipe Monteiro de Carvalho
On Tue, Nov 30, 2010 at 11:32 AM, Felipe Monteiro de Carvalho
<[hidden email]> wrote:
> Also, installing BeRoXM.apk failed in HTC Wildfire Android 2.1

I didn't check the log since I don't plan on using JNI anyway, but
probably it was built for release but lacks the certificate. This
issue confused me a bit.

ant debug

Should be used for building most of the time so that it adds the debug
certificate.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Felipe Monteiro de Carvalho
In reply to this post by Reimar Grabowski
On Wed, Nov 10, 2010 at 5:08 PM, Reimar Grabowski <[hidden email]> wrote:
> Thanks for the tutorial, but as you surely have read (see comments) there are problems using this approach. And I won't hardly call it officially supported as it is a little hackish.

I don't care about being officially supported or being hackish, I am
only interrested if it works.

I just tryed and the tutorial doesn't work anymore, it works for
Android up to 1.5 only

But I found an alternative very easy solution which works:

http://p-tools.svn.sourceforge.net/viewvc/p-tools/PascalNotes4Android/src/com/pascalnotes/PascalNotes4Android.java?revision=187&view=markup

I succeded at running ls /sdcard and showing the results in a message dialog =)

Next step is running a deployed Pascal application =D

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Benjamin Jan Alexander Rosseaux-2
In reply to this post by Felipe Monteiro de Carvalho

You do need for BeRoXM a current midclass/highend android device with a
ARM CPU, which supports the ARM v7a instruction set or better said the
VFPv3 floatingpoint instruction set. The SoC in the Wildfire, which is a
lowend android device,  is still ARMv6, like the HTC Dream and Magic.
Because BeRoXM depends on floating point calculations for the DSP audio
lowpass/highpass coef calculations.  My current FreePascal android
projects are targeting only more for midrange&highend android devices
(because i'm more the realtime multimedia audio/realtime 3D opengl es
graphics guy), not for lowend still-ARMv6 devices, which will hopely die
out by new lowend (today midrangeclass. then ex-midrangeclass lowend)
devices with VFPv3 support in this next year.

Am 30.11.2010 13:07, schrieb Felipe Monteiro de Carvalho:

> On Tue, Nov 30, 2010 at 11:32 AM, Felipe Monteiro de Carvalho
> <[hidden email]>  wrote:
>> Also, installing BeRoXM.apk failed in HTC Wildfire Android 2.1
> I didn't check the log since I don't plan on using JNI anyway, but
> probably it was built for release but lacks the certificate. This
> issue confused me a bit.
>
> ant debug
>
> Should be used for building most of the time so that it adds the debug
> certificate.
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Benjamin Jan Alexander Rosseaux-2
In reply to this post by Felipe Monteiro de Carvalho
The pascal part is under the jni/ folder in the ZIP.

Am 30.11.2010 11:32, schrieb Felipe Monteiro de Carvalho:
> Hello,
>
> How do you compile BeRoXM? Using ant?
>
> Also, installing BeRoXM.apk failed in HTC Wildfire Android 2.1
>
> Also, I couldn't find the source code for the Pascal part.
>
> thanks,

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Felipe Monteiro de Carvalho
In Android 2.3 you can write apps without any Java code, but you still
need to build it as a library:

http://developer.android.com/reference/android/app/NativeActivity.html

It seams to support OpenGL and user input without Java.

Still not ideal, however.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Matt Emson-2
On 07/12/2010 10:46, Felipe Monteiro de Carvalho wrote:
> Still not ideal, however.

Well, no. As Android targets any processor - not just ARM. Indeed, there
are Intel based versions. Native is bad, and only come in to existence
to compete with other platforms with purely native compilation - and
then purely to counteract criticisms on the performance of games and
multimedia apps. Davlik was created for a reason - compile once, run on
multiple targets. Would it not be a better solution to create a Dalvik
back-end? As I understand it, Dalvik is very, far from a traditional
stack based Java VM and closer to a traditional register based processor
in design. I know little about it, but the benefits of Android, as a
platform, are completely lost when one focuses on native compilation.

M
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Felipe Monteiro de Carvalho
On Tue, Dec 7, 2010 at 12:12 PM, Matt Emson <[hidden email]> wrote:
> Well, no. As Android targets any processor - not just ARM. Indeed, there are
> Intel based versions.

I've never seen one and I've already worked with maybe 50 different
Android smartphones / tablets.

x86-Android is negletible. I bet that it has less then 0.1% of the
Android devices market share.

> Native is bad

your opinion. I disagree.

> Would it not be a better solution to create a Dalvik back-end?

1> This would be a huge, very hard task. Who will do it? I don't have
the resources (time / money) to do it. If you have, I'm all for it
that you do it! Good luck!

2> There are no guarantees that Dalvik's bytecode will remain
compatible in the future. Google is being sued over Dalvik and might
even decide to drop it completely as far as we know. If that happens,
you just lost all your work.

3> Existing FPC applications would need to be modified to work in this
FPC port. Think about pointers for example.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Working Free Pascal android JNI example

Sven Barth-2
In reply to this post by Matt Emson-2
Am 07.12.2010 12:12, schrieb Matt Emson:

> On 07/12/2010 10:46, Felipe Monteiro de Carvalho wrote:
>> Still not ideal, however.
>
> Well, no. As Android targets any processor - not just ARM. Indeed, there
> are Intel based versions. Native is bad, and only come in to existence
> to compete with other platforms with purely native compilation - and
> then purely to counteract criticisms on the performance of games and
> multimedia apps. Davlik was created for a reason - compile once, run on
> multiple targets. Would it not be a better solution to create a Dalvik
> back-end? As I understand it, Dalvik is very, far from a traditional
> stack based Java VM and closer to a traditional register based processor
> in design. I know little about it, but the benefits of Android, as a
> platform, are completely lost when one focuses on native compilation.

I don't see it that problematic to create a Dalvik code generator (or a
normal Java or CIL one). The biggest problem is to create a suitable RTL
and to be able to use the classes that are provided by the VM.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
123