NIRAV KAKU 0 Posted February 23, 2020 I have rather written a very simple class in Java which I would like to consume in Delphi. I have followed the instructions to the tee (to the best of my knowledge)... however, in the end, when I do try to consume the class method, I get 'Java class JWhatever could not be found.' I have compiled the java to jar... jar into the classes.dex and deployed my classes.dex. I have written the wrapper correctly too and registered the types. 😕 I have attached the full source code with the dex files. Any help would be appreciated. Nirav JNI.rar Share this post Link to post
Dave Nottage 557 Posted February 23, 2020 (edited) 1. You don't need to add the classes.dex file to the project, nor the .java files 2. The UseAsJar.java file is missing the package directive which by convention is a domain in reverse order followed by a package identifier, e.g. com.mydomain.mypackage. Also, you do not need a main method, unless you're intending to run the jar separately. 3. Your import (android.JNI.UseAsJAR) is incorrect, since the signature for JUseAsJar does not match the package/class. It should be the package name followed by the class name e.g. com/mydomain/mypackage/UseAsJar. You don't need RegisterTypes, either; I believe that's a hangover from earlier versions of Delphi. 4. Once you've recompiled the .jar, add it to the project, in Project Manager under Target Platforms > Android. This saves having to manually deploy the classes.dex. You could then remove that manual addition. I'd also recommend updating to Delphi 10.3.3 Edited February 23, 2020 by Dave Nottage 1 Share this post Link to post
NIRAV KAKU 0 Posted February 25, 2020 1. You don't need to add the classes.dex file to the project, nor the .java files Yes. I know that. I just added them to the project file so that I could access them easily. That's all. 🙂 Besides, seeing all the files in the project brings a certain comfort when I am on uncharted waters. What I did not know is that you don't have to recompile into classes.dex! I didn't know you could simply add a JAR file in the libraries and wrap the header to directly use it! Thats amazing !! (unless I understood that wrong??) 2. The UseAsJar.java file is missing the package directive which by convention is a domain in reverse order followed by a package identifier, e.g. com.mydomain.mypackage. Also, you do not need a main method, unless you're intending to run the jar separately. I don't understand what 'package directive' means or how to define it. Can you give an example of it using the current case? Yes, I know main is not needed. I was trying my hands at basics first so that got left in and I didn't bother to take it out. 😉 3. Your import (android.JNI.UseAsJAR) is incorrect, since the signature for JUseAsJar does not match the package/class. It should be the package name followed by the class name e.g. com/mydomain/mypackage/UseAsJar. You don't need RegisterTypes, either; I believe that's a hangover from earlier versions of Delphi. Again, the signature has to be a copy (with '/' instead) of the fully qualified package directive? I saw Jim's video on this (pretty dated) so I thought this was a mandatory step. 😕 4. Once you've recompiled the .jar, add it to the project, in Project Manager under Target Platforms > Android. This saves having to manually deploy the classes.dex. You could then remove that manual addition. Cool! Thank you for all your advice!! Share this post Link to post
Dave Nottage 557 Posted February 25, 2020 2 hours ago, NIRAV KAKU said: I don't understand what 'package directive' means or how to define it At the very top of the Java file: package com.mydomain.mypackage (for example) There's a plethora of examples on the internet, e.g in the files here: https://github.com/DelphiWorlds/KastriFree/tree/master/Java 1 Share this post Link to post
NIRAV KAKU 0 Posted February 25, 2020 (edited) Thanks a ton Dave! I actually made decent headway! 1. Added the package directive with full path of the Java file. 2. Used the jar command and to compile into JAR file. 3. Added the JAR file to the project Project > Android (selected), it placed the JAR file in the Libraries folder. 4. I did get an error but then I figured out it was because of the location path (apparently dx requires strict path and package name mapping). It then created UseAsJAR-dexed.jar file automatically. 5. Changed the signature in android.JNI.UseAsJAR pas file. 6. Removed the RegisterType call (as you suggested). But still I get the same error. 😞 So frustrating! JNI.rar Edited February 25, 2020 by NIRAV KAKU Share this post Link to post
Dave Nottage 557 Posted February 25, 2020 JNI.rar did not include the compiled .jar file, nor did the project have .jar file added to libraries. I updated the project to Delphi 10.3.3, compiled the UseAsJar.java file into UseAsJar.jar, added it to the project, zipped it and attached it to this reply. The project now works OK here. You can "downgrade" the project to whatever version of Delphi you're using by right-clicking the "Libraries" node and click "Revert System Files to Default" prjJNITest.zip Share this post Link to post
NIRAV KAKU 0 Posted March 15, 2020 Hi Dave Sorry being incognito for so many days. Since your last response, I was travelling and now am under self quarantine. Luckily no fever but still need to be careful. I will look into the code today! Thanks a ton! Share this post Link to post