Android JNI Bridge and Custom Classes.dex

By creating a custom Classes.dex you can get access to 3rd party Java JAR APIs from your application. For my Integrate More Android with a JNI Call to your Android App Developer Skill Sprint I created a demo app that demonstrates creating a custom Classes.dex. This is a new feature in XE6 and Appmethod 1.14. [Download the demo] [Download the slides] The Demo app uses the Base64Coder JAR file (included). To build the demo:

  1. Examine the createdex.bat file to make sure it refers to the correct location for your dx.bat utility and the fmx.jar & android-support-v4.jar files.
  2. Run the createdex.bat file to create the classes.dex file which includes the two jar files above, plus the base64coder.jar file.
  3. Double check that the Deployment Manager references the new classes.dex and not the old ones, and that the remote path is “classes\”
  4. Notice that the android.JNI.Base64Coder.pas file wraps and exposes the methods of the base64coder class.
  5. Run the app on your Android device and verify that it works as expected.

The Base64Coder.JAR is Android specific, so it will not work on iOS or Windows. Some additional notes from the Developer Skill Sprint: Some useful units for making JNI calls

  • Androidapi.Jni – Java Native Interface type definitions
  • Androidapi.JNIBridge – The JNI Bridge
  • Androidapi.JNI.JavaTypes – JString and other common types.
  • Androidapi.Helpers – JStringToString and other useful conversions.
  • FMX.Platform.Android– Useful platform methods like GetAndroidApp, MainActivity and ConvertPointToPixel
  • Others useful units: Androidapi.AppGlue, Androidapi.JNIMarshal, Androidapi.JNI.Embarcadero
  • For more see: C:\Program Files (x86)\Embarcadero\Studio\14.0\source\rtl\android (Object Pascal) and C:\Program Files (x86)\Embarcadero\Studio\14.0\include\android\rtl (C++)

You will want to make use of Conditional Defines in Object Pascal and Predefined Macros in C++. In my blog post on Android Settings I showed how to make a JNI call with Object Pascal, but you can also look at the DeviceInfo Mobile Code Snippet in both C++ and Object Pascal. To create your own JNI Bridge wrappers, look at the source code in C:\Program Files (x86)\Embarcadero\Studio\14.0\source\rtl\android (Object Pascal) and C:\Program Files (x86)\Embarcadero\Studio\14.0\include\android\rtl (C++). You can also consider the following 3rd party utilities:

If you just want to include standard Android APIs then check out the FMXExpress (also an Embarcadero MVP) project on GitHub that includes all the Android APIs. Here is the video replay of my skill sprint

Also, check out Brian Long’s video on accessing the Android API with XE5

This entry was posted in Android, Mobile, MVP, News, Source Code, Tools, webinar. Bookmark the permalink.

22 Responses to Android JNI Bridge and Custom Classes.dex

  1. Gabi says:

    teaching on this, too?:
    Android Runtime (ART), which debuted as a technology preview in Android 4.4 “KitKat”,[3][4] as of June 2014 is announced to replace Dalvik entirely in Android L.[5]

  2. Jim McKeeth says:

    ART is cool. Basically Android is moving from JIT (Just In-Time Comple) like traditional Java to AOT (Ahead-Of-Time Compile). All of our apps are already pre-compiled, so it doesn’t affect us significantly. It does mean the classes.dex and the JAR files it contain will be AOT compiled on install, instead of on first run.

    Basically the move to ART is an acknowledgement that the sooner you compile the better.

  3. Pingback: Customize The Classes.dex File To Access Third Party Android JAR Libraries In Delphi XE6 Firemonkey | Delphi XE5 XE6 Firemonkey, Delphi Android, Delphi IOS

  4. Pingback: Custom Classes.dex | Delphi Programming | Scoo...

  5. Pingback: Skill Sprint: Android Voice – Speech Recognition and TTS | The Podcast at

  6. Mike Price says:

    I have watched the “Integrate More Android with JNI call to your Android API” Ski you say look at the createddex.bat file and make sure 3 files are in the correct directory. The problem is one of them fmx.jar is not any where on my computer and is required. I am using Delphi XE6 has it been renamed?t.

  7. It should be at: “C:\Program Files (x86)\Embarcadero\Studio\14.0\lib\android\debug\fmx.jar”and “C:\Program Files (x86)\Embarcadero\Studio\14.0\lib\android\release\fmx.jar”. I have just checked it

  8. Ates Gökdemir says:

    Hello, first of all thank you very much for your post. I am trying to control ioio-otg board with delphi xe6. I Bought winsoft java import tool and i have ioio_delphi.pas file now. I have also created a new classes.dex file which includes ioio.jar.


    I look forward for your reply

    Best Regards

  9. Jim McKeeth says:

    Give me a hint. What sort of error are you getting? What have you tried?

  10. Ates says:

    I am getting access violation error when i press the button

  11. Jim McKeeth says:

    Maybe you are not including the JAR file in your Classes.dex. Have you verified that? Does removing the JAR file change the behavior? Did you verify that you removed the old classes.dex from your deployment and the new classes.dex is in the correct location?

    What have you done to troubleshoot this?

  12. Ates Gökdemir says:

    When i remove the jar file, changes only the number of access violation error. But i saw that i have no usb host permission on my phone. I will root it and try again. Can it be the reason ?

  13. Jim McKeeth says:

    I’m not familiar with the JAR library you are using. It could be a matter of you don’t have the the JNI interface correct.

  14. Mike Price says:

    Jim you mentioned during the video that you considered using the ZBar library as the demo interface. If it is not too much work would it be possible to publish that demo as well.

  15. Dener says:

    Jim, i try the connect a push in azure mobile service, you have a sample for this ? Congat for your “show” in Embarcadero Conference Brazil !

  16. Jim McKeeth says:

    I don’t have an example with Azure yet. Sorry.

  17. Pingback: Atualizador Automático vs Google Play TDevRocks

  18. Stéphane WIERZBICKI says:

    Hello Jim,

    Thank you for your post. I still have some questions. Starting XE7 (I guess), project manager allow you to add custom .jar files (libraries). Is it safer to use this feature or should we still have to create a custom classes.dex file ?

    Additional Java packages (.java files) should be added to our custom classes.dex file or could be added to the project manager’s library ?

  19. Jim McKeeth says:

    Stephane: Adding files to the Project Manager creates custom classes.dex behind the scenes. So you are better off using the Project Manager in most cases.

  20. samsim says:

    A good example of the use of this script. I’ll have to try it to write something like that. The author of Respect!

  21. Pingback: android jni tutorial - computer tutorials

  22. den says:

    Hi! I have Delphi XE8 and I want CryptoApi from Windows on Android, but in android i find similar functions in Androidapi.JNI.Java.Security. How can i use TJKeyFactory in my app? I try to create it but its failt KeyFactoryI := TJKeyFactory.Create; second i try use Wrap KeyFactoryI := TJKeyFactory.Wrap(???) but i don’t know what object_ID must be in parametr of wrap function. Please give me little example how i can get JKeyFactory object in android. Thank you!

Leave a Reply