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:
- 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.
- Run the createdex.bat file to create the classes.dex file which includes the two jar files above, plus the base64coder.jar file.
- Double check that the Deployment Manager references the new classes.dex and not the old ones, and that the remote path is “classes\”
- Notice that the android.JNI.Base64Coder.pas file wraps and exposes the methods of the base64coder class.
- 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:
- Java2Pas by Software Union
- JavaImport by WinSoft
- Android2DelphiImport by CHUA Chee Wee aka “Chewy” (also an Embarcadero MVP) [my previous review]
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”
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]
http://en.wikipedia.org/wiki/Dalvik_%28software%29
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.
[…] Head over and read the full blog post, download the demo, and watch the slideshow for customizing yo… […]
[…] By creating a custom Classes.dex you can get access to 3rd party Java JAR APIs from your application. […]
[…] I’ve still got a lot of work to do on the components, but they work as is for now. If you want to modify the component code then take a look at my Skill Sprint and blog post on the Android JNI Bridge. […]
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
Give me a hint. What sort of error are you getting? What have you tried?
I am getting access violation error when i press the button
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 ?
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.
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 !
I don’t have an example with Azure yet. Sorry.
[…] que me ajudaram a entender melhor: Delphi.org DocWiki da Embarcadero Blog do Fernando […]
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.
A good example of the use of this script. I’ll have to try it to write something like that. The author of Respect!
[…] Download :Android jni bridge and custom classes.dex […]
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!