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

Source: Internet
Author: User

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:

UnitUnit1; Interface    usessystem.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 unit we just created.  typeTForm1=class(tform) Panel1:tpanel;      Label1:tlabel;      Nameedt:tedit;      Label2:tlabel;      Ageedt:tedit;      Panel2:tpanel;      Memo1:tmemo;      Button1:tbutton;      Button2:tbutton;      Button3:tbutton; procedureButton3click (Sender:tobject); procedureButton2click (Sender:tobject); procedureformcreate (Sender:tobject); procedureButton1Click (Sender:tobject); Private      {Private Declarations}     Public      {Public Declarations}    End; varForm1:tform1; Testclass:jtest; //the Java class variable to invoke  Implementation    {$R *.FMX}    usesAndroidapi.helpers,//jstringtostringAndroidapi.JNI.JavaTypes; //setting Java class PropertiesprocedureTform1.button1click (Sender:tobject); varName:string;//Name attribute valueAgestring;//Age Attribute Valuebegin      //Get the nameName: =Trim (Nameedt.text); //Get AgeAge: =Trim (Ageedt.text); ifName ="'  Then    beginShowMessage ('The name attribute cannot be empty! ');    Nameedt.setfocus; End    Else ifAge ="'  Then    beginShowMessage ('The age attribute cannot be empty! ');    Ageedt.setfocus; End    Else    begin      //set the value of the Name property that we call in the Java classTestclass.setname (stringtojstring (name)); //set the Age property value in the Java class that we calltestclass.setage (TJInteger.JavaClass.init (stringtojstring (age))); End; End; //Get the Java class properties setprocedureTform1.button2click (Sender:tobject); begin      //empty existing display dataMemo1.text: ="'; //The value of the name attribute in the resulting called Java class is displayedMemo1.Lines.Add (jstringtostring (testclass.getname)); //The value of the age attribute in the resulting called Java class is displayedMemo1.Lines.Add (jstringtostring (testClass.getAge.toString)); End; //clear the contents of the displayprocedureTform1.button3click (Sender:tobject); beginMemo1.text:="'; End; //instantiate J to invoke the Ava classproceduretform1.formcreate (Sender:tobject); begin      //Instantiate the Java class to invokeTestClass: =TJTest.JavaClass.init; //set the Name property valueTestclass.setname (Stringtojstring ('testname')); //Set the Age attribute valueTestclass.setage (TJInteger.JavaClass.init (stringtojstring (' -'))); End; End.

The eclipse operation in this example is the whole process:

After finishing the article and re-provide the link!!

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's class?

Related Article

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.