Categories
Android Mobile MVP News Source Code Tools webinar

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

22 replies on “Android JNI Bridge and Custom Classes.dex”

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.

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.

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

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.

[snip]

I look forward for your reply

Best Regards
Ates

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?

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 ?

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.

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 !

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 ?

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.

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!

Comments are closed.