How does Delphi XE7 's Android program invoke Java's jar, using Java classes?

Source: Internet
Author: User

Reprint: http://blog.csdn.net/sunylat/article/details/41414785

This article uses tools and all source code download: http://download.csdn.net/detail/sunylat/8190765

Why do we want to invoke Java's Jar in the Delphi XE7 Android program, using the Java class? If we can do this, then it means that we have to expand the Delphi development of the Android program to a larger scope of development, in theory can call arbitrary Java code written, for the same time proficient in Delphi and Java programmer, simply a powerful!!

I spent almost a day, and finally with the help of the old cat, the successful implementation of the Delphi XE7 in the Android program to call the Java jar, the use of Java Class! In this expression to the old cat thanks, at the same time through the old cat allowed to provide the old cat developed Java to PAS tool, thank the old cat selfless sharing!!

The jar I have provided for you is a very simple Java class that I have written myself, providing only two properties. Why write a Java class yourself? I think I know the code I wrote most, and the easiest way to debug the program as soon as possible, so I made a simple Java class.

I conclude that Delphi XE7 's Android program calls the Java jar, going through so three steps:

One, set the project properties. For the time being, because of the Delphi tool, which causes the default properties to not run properly compiled Android programs, I think this should be the Delphi tool bug.

Second, generate "Native Bridge File". This is the Delphi and Java Communication Interface file, in Delphi by introducing this interface file to use the Java class inside the jar, this file is necessary.

Third, write the code that actually calls the Java class. This is the final work to be done.

Assuming that you are now a master of both Delphi and Java programmers, then I am now on the above three steps to explain in detail, if you are not familiar with Java also does not matter, in the last, I will write my Java class and export the process of the jar in detail. Here are the three steps above:

One, set the project properties. For the time being, because of the Delphi tool, which causes the default properties to not run properly compiled Android programs, I think this should be the Delphi tool bug.

1, after you have completed the project, click "Project", "Deployment".

2, set the properties.

After doing this, your project configuration is correct, if you have done the following two steps, then your Delphi XE7 development of the Android program should be able to call the Java class in your jar normally!

Second, generate "Native Bridge File". This is the Delphi and Java Communication Interface file, in Delphi by introducing this interface file to use the Java class inside the jar, this file is necessary.

Delphi Company provides a Java to PAS tool, called "Java2op", in my actual use, feel not good, there are two points: A, can not automatically generate interface guid;b, generated interface file name and Java class name does not match. So in the end I didn't use it either. This tool works correctly in Windows virtual machine VMware, and if you have XE7 installed in VMware, you can try it.

I successfully generated native Bridge File using two tools:

A, the old cat's tool: Javaclasstodelphiunit_flyingwang

B,java2pas:

I think the old cat tools more friendly, easier to use, but also should be more perfect, because the old cat has been in use, constantly in the improvement! So I recommend you to use the old cat tools!!

Here is the build native Bridge file process:

1, copy the jar file you want to generate native Bridge to the old Cat tool directory. My jar name is "Test.jar".

2, use the old Cat tool to generate native Bridge File.

A, select the jar file that we want to generate native Bridge file.

B, select the output path of the native Bridge file.

C, generate native Bridge File.

Copy the generated native Bridge file to the project directory and add it to the project.

Third, write the code that actually calls the Java class. This is the final work to be done.

Two personal points of view:

1, the function of the interface is to define and implement the separation, then we use the time, we should define the variable as an interface, the actual instantiation should be used to implement the class of this interface.

2, we do not need to release the called Java class, this class is managed by the Android virtual machine and should be freed automatically.

The following is the implementation of the Delphi All code:

Unit unit1;interfaceuses system.sysutils, System.types, System.uitypes, system.classes, System.variants, FMX. Types, FMX. Controls, FMX. Forms, FMX. Graphics, FMX. Dialogs, Fmx.stdctrls, FMX. Controls.presentation, FMX. Edit, FMX. Layouts, FMX. Memo, Test;    This test is the native Bridge file element we just generated, type TFORM1 = Class (Tform) Panel1:tpanel;    Label1:tlabel;    Nameedt:tedit;    Label2:tlabel;    Ageedt:tedit;    Panel2:tpanel;    Memo1:tmemo;    Button1:tbutton;    Button2:tbutton;    Button3:tbutton;    Procedure Button3click (Sender:tobject);    Procedure Button2click (Sender:tobject);    Procedure Formcreate (Sender:tobject);  Procedure Button1Click (Sender:tobject);  Private {Private declarations} public {public declarations} End;var Form1:tform1; Testclass:jtest; Java class variable to invoke implementation{$R *.fmx}uses androidapi.helpers,//jstringtostring androidapi.jni.javatypes;// Set Java class properties procedure Tform1.button1click (Sender:tobject); var name:string; Name attribute Value AGe:string;  Age attribute value begin//Get First Name: = Trim (Nameedt.text);  Get Age: = Trim (Ageedt.text); If name = "THEN begin ShowMessage (' name attribute cannot be empty!    ‘);  Nameedt.setfocus; End else if age = "THEN begin ShowMessage (' aging attribute cannot be empty!    ‘);  Ageedt.setfocus;    End ELSE begin//sets the name attribute value Testclass.setname (stringtojstring (name)) that we call in the Java class;  Set the Age attribute value Testclass.setage (TJInteger.JavaClass.init (stringtojstring (age)) in the Java class that we call.  end;end;//Gets the Java class attribute set procedure Tform1.button2click (sender:tobject); Begin//Empty the original display data memo1.text: = ";  Displays the value of the name attribute in the called Java Class Memo1.Lines.Add (Jstringtostring (testclass.getname)); Displays the value of the age attribute in the called Java Class Memo1.Lines.Add (Jstringtostring (testClass.getAge.toString)); end;//empty the contents of the display procedure Tform1.button3click (sender:tobject); begin memo1.text: = "; end;//instantiate J to invoke Ava Class procedure tform1.formcreate (Sender:  TObject); Begin//Instantiate the Java class to invoke TestClass: = TJTest.JavaClass.init;  Set the name attribute value Testclass.setname (stringtojstring (' testname ')); Set Age attributeValue Testclass.setage (TJInteger.JavaClass.init (stringtojstring ('))); End;end. 

The eclipse operation in this example is the whole process:

http://blog.csdn.net/sunylat/article/details/41422103

In addition to using Eclipse, you can also use BAT to generate jars. You can refer to the example of a JAVA-to-JAR for group sharing, example name: Jarorclass2pas_flyingwang v1.0.2014.1120.zip. Group name: ①firemonkey[Mobile Development] 165232328

How does Delphi XE7 's Android program invoke Java's jar, using Java classes?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.