Instructions and considerations for invoking the JNI jar, invoking the third-party jar SDK and translating the instructions for the Android JAVA code V2015.6.10

Source: Internet
Author: User


Instructions and considerations for invoking the JNI jar, invoking the third-party jar SDK and translating the instructions for the Android JAVA code V2015.6.10

Reprint please indicate the source, otherwise dead family.
Select "Copy link" to get the source.


(* ************************************************ *)
(*                         *)
(*                         *)
(* Design: love to eat pig head meat & Flying Wang 2015-04-15 *)
(* Please do not remove the above copyright notice.) *)
(*                         *)
(* ************************************************ *)

My group (①firemonkey[Mobile Development] 165232328)


For Android Systems
The Google API provides a JNI interface.
The third-party JAR SDK also provides a JNI interface.

Your phone will typically have a large number of interfaces built into the Android SDK.
However, some APIs may not be built-in.

The FMX Android project will provide you with more than one Google SDK by default.
So most of the Google JAR, you don't need to find it yourself.

For third-party SDKs.
It is basically divided into jar + so, pure jar and pure so three kinds.
Pure so is a DLL-like interface. Just be aware that DLLs are stdcall types, and all other platforms are cdecl types. It's no good not to write. Because Delphi does not write the default pascall type.
If you do not call the DLL then you will not call so.
This article does not discuss calling so.

But tell you the path to the so file file: Edit the RemotePath column. The path is (xe5) Library\lib\armeabi (Xe6 or above) library\lib\armeabi-v7a
The so of the above path is generally used for jar loading.
Self-loading can be used Loadlibaray or Dlopen. Remember to use the corresponding Freelibaray and Dlclose
You can also define a function interface for so, just as you would define a DLL. So you don't have to write code to load it.
If you use only yourself, you can publish to other paths that you can access. However, only write code is loaded, and the path is written in full.


For jars whether it has no so. We can only use the external interface of the jar.

Unless it's an Android basic API, or a JAR already provided by DELPHI. Otherwise the JAR, as long as you use it, must be added to the Android project. Specific how to add, please Baidu by yourself.

Even the basic API,EMBT not all turn into JNI interfaces.
So when you use an API that EMBT not translate, please use the tool to translate it yourself.

Translation: Can be understood as the conversion of the language, the export of interfaces, that is, the Pas file into Pascal grammar

When you get a JAR, translate the tool into a Pascal file.
2 tools are currently recommended.
1. The official Java2op.exe. Supports. jar. java. class three format files.
2. Love to eat pig's jarorclass2pas. Supports. jar. class two-format files.

All the other tools are rubbish. Please don't use it. Otherwise you deserve it.

When you get the Pas file with a tool, note:
1. Generally get a lot of useless, disordered, repetitive interfaces. Please remove them.
2. Even if the interface is not a problem, if it is not available, please delete it.
3. The conversion tool will write out some uses units that may not exist.

For a jar or class file referencing a different jar, it is easy to uses a unit that is not known. Please find these jars to continue translating.


When your jar has been added to the Android project.
The jni pas file is ready to be added to the Android project as well.
You can start calling the jar interface.


Next, the basic type changes are explained.

Int is an Integer, many basic objects can be thought of by ourselves.
String is jstring.
The Uri is to be translated into Jnet_uri.
These two types of EMBT provide functions that are converted to each other.
There are also individual other types that change the name. This is not an exhaustive list. If you find that some class name EMBT should be provided, but not found, look in the Find in Files dialog box by Signatur.
For example the search Java/lang/class can be found JCALSS also renamed.

int [] is tjavaarray<integer>, basic type with tjavaarray<>.
But string [] is Tjavaobjectarray<jstring>, and the object type is generally used tjavaobjectarray<>.

Arraylist<string> to be translated into jarraylist
And any arraylist<endingintent> that is arraylist< an object > will be translated into jarraylist.

Some types of names are more specific. For example, Java's Phone type EMBT has been translated into Jcommondatakinds_phone. This is because the Phone is the inner class of commondatakinds.


The data type is OK. Then talk about the constructor class member class method of Java class and ordinary member common method.
If you do not understand these, please self-Baidu. It is recommended to take a good look at the object Oriented Development course.

Java classes typically provide a default constructor, and in Pas, his function is named Init lowercase. The default does not take parameters.
However, many Java classes overload constructors and provide versions with parameters.


Next, we need the demo of the JNI you intend to use under Baidu.
Android development, the biggest advantage is that the online full demo, although the Java language.

Once you have found the demo code, you need to follow the logic of the code to translate the language.

Several common code translations are provided below.

1. Construction of an object.
1.1 Java code
xxx x = new xxx (parameters or no parameters);
1.2 Translation Code
Var
x:jxxx;
Begin
x: = TJxxx.JavaClass.init (parameter or no parameter);
If x = Nil then error.
End
The code above demonstrates the use of constructors. Attention:
1.3 X is jxxx
1.4 Tjxxx.javaclass is the beginning of TJ. Javaclass. can provide you with all the class methods of this class, class members, including constructors.
1.5 Not all classes provide a default constructor.

2. A non-default construct of an object, using the construction of a class method.
2.1 Java Code
xxx x = new xxx.yyy (parameter or no parameter);
2.2 Translation Code
Var
x:jxxx;
Begin
x: = TJxxx.JavaClass.yyy (parameter or no parameter);
If x = Nil then error.
End
The above code demonstrates the use of class methods to get Class XXX objects.

3. Object x is obtained by other classes of objects.
3.1 Java code
xxx x = yyy.zzz (parameter or no parameter);
3.2 Translation Code
Var
x:jxxx;
Begin
x: = yyy.zzz (parameter or no parameter);
If x = Nil then error.
End
Very simple, just add a colon. YYY is another object. ZZZ can be a class method, a class member, a normal method, or an ordinary member of a YYY object.


4. It is generally not common to get object x by forcing type conversions.
4.1 Java code
xxx x = (XXX) yyy.zzz (parameters or no parameters);
The type returned by zzz is not xxx, nor is it a derived class of XXX.
4.2 Translation Code
Var
Xlocalobject:jobject;
x:jxxx;
Begin
Xlocalobject: = yyy.zzz (parameter or no parameter);
Or
Xlocalobject: = Jobject (yyy.zzz (parameter or no parameter));

If Xlocalobject = Nil then error.
x: = Tjxxx.wrap ((Obj as Ilocalobject). Getobjectid);
If x = Nil then error.
End
Note the beginning of Wrap, also TJ
Passed the verification of [Taipei]wildsky (2590003092)
x: = Tjxxx.wrap ((yyy.zzz (parameter or no parameter) as Ilocalobject). Getobjectid);
So one less variable is written. Some machines are flashing.
With two variables to complete, there will be no flash back.

For some JNI services
Verified by [Shenzhen] Doraemon (5909386)
TJContext.JavaClass.VIBRATOR_SERVICE
TJActivity.JavaClass.VIBRATOR_SERVICE
You can use the top one, and the following will flash back.

The beginning of the J-TJ begins with the conventions. You can also start the TSB with SB.

The default TJ starts with the type of DELPHI.
J begins with the type of JAVA.

Tjxxx.javaclass returns the Jxxxclass type, which specifically represents class methods and class properties. The jxxx represents the type of the class object.

5. Do not use variables to manipulate the code of a certain type of XXX.
5.1 Java code
XXX.YYY (parameter or no parameter). ZZZ (parameter or no parameter);
5.2 Translation Code
TJXXX.JAVACLASS.YYY (parameter or no parameter). ZZZ (parameter or no parameter);
5.3 xxx is a type yyy is a class function.
5.4 can appear xxx. JAVACLASS.YYY (parameter or no parameter). ZZZ (parameter or no parameter). NNN (parameters or no parameters); This multi-level invocation.

6. There is already an object xxx calling his method to manipulate the code.
6.1 Java code
XXX.YYY (parameters or no parameters);
6.2 Translation Code
XXX.YYY (parameters or no parameters);
6.3 XXX is the object yyy is the class function or member function of the object.
6.4 can appear xxx.yyy (parameters or no parameters). zzz (parameters or no parameters); This multi-level invocation.

7. Using Constants
7.1 Constants in Java are generally members of a class. and is generally a class member of a class.
7.2 Java code
xxx = yyy.zzz;
XXX is a variable yyy is a class name zzz is a constant name
7.3 Translation Code
XXX: = TJyyy.JavaClass.zzz;
7.4 This means that zzz is translated into the jyyy's Class version of the interface.
7.4 effect equals
XXX: = jyyyclass.zzz;
7.5 It is not recommended to use the above code. Please press the 7.3 version to write.

Note that for us Pascal, there are no parameter brackets that can be omitted. C JAVA is not allowed to be omitted.


For array tjavaarray<xxxx> If you specifically want to build the object yourself.
Can write
Var
xxx:tjavaarray<xxxx>;
Begin
XXX: = Tjavaarray<xxxx>. Create (number);
Then use XXX. items[number] to access.
End
Tjavaobjectarray is also the way of the above.
There is currently no way to find the dynamic number of modifications. That is, the number of arrays is established when it is determined.


If you use a method, the occurrence of segmentation fault (11) may be that the object is nil or the function does not exist (generally the version is different and some version functions do not exist).

If you use a method that has an illegal operation, it means that there is no such method (probably a name or a parameter with an error).
If you are prompted to Java class XXX could not be found, if it is the official xxx, then your phone does not provide this interface, you can find the official jar file to join,
If it is a third-party XXX, it is more simple, this xxx corresponding jar file, you certainly did not join in your project.
If you confirm that you added (refer to the second arrow), it is recommended to do the following:
Open your Android project, à la carte item project->deployment, open the deployment subwindow, point to the Revert to Default button, that is the left-pointing curved arrow:
The Revert to Default dialog box appears:
Select the first item "Revert for all configurationsthe active platform", click OK.
Note: Regardless of the default options, you must select one and click OK here, or your Android program will see "Java Class xxx could not befound" error when calling the jar file.
The above text originates from http://blog.sina.com.cn/s/blog_648d306d0102vfgq.html


If OBJS is tjavaobjectarray<jxxxx> objs.items[x] or objs[x] An error occurs (segmentation fault (11)), then you cannot use this, instead
Tjxxxx.warp (Objs.getrawitem (x)) try it. Thanks for the test of [Shenzhen] Doraemon (5909386).


General recommendations
Uses
{$IF compilerversion >= 27.0}//>= XE6
Androidapi.helpers,
{$ENDIF}
FMX. Helpers.android,
Androidapi.JNI.JavaTypes,
androidapi.nativeactivity;


When an Activity object is needed, we can only provide tandroidhelper.activity. Because FMX only has this one Activity.
When a View object is needed, the default is TAndroidHelper.Activity.getWindow.getDecorView. The rest of you don't know.
When you need a Context object, you can try Tandroidhelper.context, which is global.
When you need a Getapplicationcontext object, you can try TAndroidHelper.Context.getApplicationContext, this is because you do not understand the source, not my fault.
{$IF compilerversion >= 30.0}//>=RAD10
Tandroidhelper.activity
Tandroidhelper.context
{$ELSE}
Sharedactivity
Sharedactivitycontext
{$ENDIF}

Anyway, when you get a Java object you must first check whether it is nil, otherwise the light will prompt the error, and then flash back.
Embt often forget to check, so the flash, for example, you put an IAP payment control in the form, many mobile phones will be on the blink, because no check nil.
If the APP you're developing is on any machine, especially an older version that's been upgraded, someone else copied it to you. It is generally confusing to publish information, resulting in. The release (deployment) information for the project requires a "reload". Deployment needs Revert to Default
If it's any app. Includes the new empty APP, which runs on a particular machine and then flashes back. Description is a BUG. Please find the XE fix APK boot in this group of not looking regret series, prompting cannot deploy, "" File not found.txt.


When you use a Jni object. If it is an object that can be displayed. Many times you need:
Callinuithread (
Procedure
Begin
JNI code.
End);
Sometimes you have to switch to callinuithreadandwaitfinishing.
Only such code will not deadlock.
That is, if you don't write like that. Your APP will appear unresponsive.
Some of the JNI objects that are not displayed also need to be written like this. But not very common.
If you receive an error calledfromwrongthreadexception, you need to callinuithread.

If you receive a
Can ' t create handler inside thread that have not called looper.prepare ()
It is also necessary to callinuithread. Thanks [Xinhui]supermay (15832782) test.

Note: Do not put large chunks of code into the code block above. Minimize the associated code. It is best to use DEBUG to find the line of code that pops up this type of error message. Do not DEBUG please read.

In the world of Android, the callback function does not exist. However, you can use interfaces to do callbacks.
Typically this interface is defined as Listener.

When you need to inherit (implement) a Java interface, you need to look at the code.
Using the DELPHI IDE, open the Find in Files dialog box in the Search menu.
Enter search keywords
= Class (Tjavalocal,
Search Scopes in directories
Select the source directory to your installation directory and select Indude subdirectories
Well, look for it.
You'll find a lot of code.
They are a good example of inheriting (implementing) Java interfaces.

The interface must be implemented according to the object-oriented argument. So the code found above is a must.
The object of this class is then defined and can be used when the parameter is in JNI.
This type, however, is the implementation of Delphi, so don't forget the free.

Personally suggest that you study "unit System.Android.Bluetooth;".

Sometimes the callback function provided by the interface may be running in the thread.
When you implement this function, be aware of it.
The UI object is either FMX or JNI. You all need thread synchronization.

The easy way to thread synchronization is.
...
Some code in a thread or callback function.
Tthread.synchronize (nil,//or use thread's own synchronization function.) Synchronize (
Procedure
Begin
Your interface interaction code.
End);
The code that resumes the thread or callback function.
...

If you receive the BITMAP size too big hint in DEBUG, it is possible to use synchronization, no synchronization caused.


With the above knowledge, you basically translate Java code, there is no problem.
Don't say, you don't order, judge, loop, function call.

If you want to know how a third-party view is displayed in the FMX.
Refer to Unit FMX. Webbrowser.android and Unit FMX. Media.android.

In addition, a lot of operations, all need the corresponding permissions, do not forget to add.

For more than 4.4 of the system, you want to access the external memory card. Need to add
<uses-permission android:name= "Android.permission.WRITE_MEDIA_STORAGE"/>
This permission.
The IDE is not currently available.
You need to join in the AndroidManifest.template.xml document, you can open it with the IDE, find the <%uses-permission%>, and add the above permission text to the line below.
However, it is not guaranteed that all machines are valid.


If you find that you need to use the following command line on Android, such as performing su sh ping.
You can refer to the code of QDAC http://blog.qdac.cc, which may be called qruntime.
You can also refer to the group to restart your phone source code.


For learning to translate Android Java code for Pascal.
It is best to look at the source code of EMBT first. Then look at Embt's Samples.
You can also take a good look at the group sharing.
There are many calls to the JNI DEMO.

Related tools
Jarorclass2pas Flyingwang V1.0.2015.517 with Java turn jar.zip
Http://www.2pascal.com/forum.php?mod=viewthread&tid=891&fromuid=4
(Source: 2pascal-, Pascal, new era)


Rad10rtm loading jar has a BUG
https://quality.embarcadero.com/browse/RSP-12335
There are solutions in QC.
We recommend that you register your EDN account with EMB website to log in.
The new version of the BUG above has been fixed.

Http://www.2pascal.com/forum.php?mod=viewthread&tid=1384&fromuid=4

Instructions and considerations for invoking the JNI jar, invoking the third-party jar SDK and translating the instructions for the Android JAVA code V2015.6.10

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.