Load the jar plug-in Android app

Source: Internet
Author: User

 

Load the jar plug-in Android app

 

Introduction of plug-ins

1. Some custom general control code in the UI is not limited to a project. When shared use is required, you can integrate the source code into a new project, however, this original method increases the complexity of the Code layout and increases the security of general controls.

2. For some functional modules, third-party development, or phased development, we also consider encapsulating the functional code into a plug-in package to facilitate functional expansion of the program.

Does Android support plug-ins? That is, does it support libraries such as lib and DLL on Windows and Symbian platforms? The answer is yes. The plug-in Library supported in android can be in the JNI format developed by C/C ++, it can also be a jar file developed by Java code (or an APK file completed by Android package ). Because JNI needs to involve Android ndk, we will not introduce it here. This article only applies to the jar plug-in.

How to load the jar plug-in

Although we should consider creating the jar plug-in before loading the jar plug-in, the different jar plug-in creation forms are determined by the different jar loading methods. Currently, there are two ways to load the jar plug-in: one is the same Static Loading Method as loading the Android. Jar sdk api, and the other is the dynamic loading method at runtime. The former requires the existence of jar files during the compilation process, and the latter is dynamically called through the reflection mechanism at runtime. Next we will explain it separately.

Static Loading jar plug-in creation of static jar plug-in

The creation of static jar plug-ins can only be generated by compilation of a simple Java class file, or exported and generated from a complete project.

Suppose there is a test. Java file that does not use Android APIs placed in the C root directory, then on the PC where JDK is installed, we can generate a jar file through the CMD command line.

// Compile test. Java to generate test. Class

C: \> javac test. Java

// Compress test. Class to generate a JAR File

C: \> jar CVF test. jar test. Class

Manifest)

Added: Test. Class (read = 267) (write = 213) (compressed by 20%)

// View the specific content in the generated JAR File

C: \> jar tvf test. Jar

0 Fri Sep 23 11:21:34 CST 2011 META-INF/

75 Fri Sep 23 11:21:34 CST 2011meta-inf/manifest. MF

267 Thu Sep 22 17:56:42 CST 2011 TEST. Class

A simplest jar plug-in is generated through the above steps. The following describes how to generate a jar file from a complete project. Of course, you can also use the CMD command line method. However, for a project, there are many code files, with simple operations provided in eclipse, we naturally choose eclipse to generate jar files graphically. I Wanted To summarize it by myself, but I found that many blogs on the Internet are very detailed. Here I will reference ghost.

1. Open the project in eclipse, right-click the project, and select "Export ";

2. Select Java/jarfile and next;

3. In select the resources to export, you can select the project folder you want to include. Some unnecessary folders do not need to be put in, so as not to increase the space;

There are several options:

* Export generated class files and Resources indicates that only generated. class files and other resource files are exported.

* Export all output folders for checked projects: export all folders of the selected project

* Export Java source file and Resouces indicates that the exported jar package will contain your source code *. java. If you do not want to disclose the source code, do not select this option.

* Export refactorings for checked projects include some reconstructed information files.

In select the export destination, select the path of the exported jar, next

4. On the next page, you can choose whether to export the *. class files containing warning warnings or error errors. Ignore him, next

5. Configure the project on the next page.

* Generate the manifest file is used by the system to automatically generate the manifest. MF file. If your project does not reference other class-path files, you can select this item;

* Use existing mainfest from workspace. This is a custom. MF file. The format is written above;

* Seal content. To encapsulate the entire jar or the specified packet;

* Main class. Here you can select your program entry. The jar package in the future will be the execution result of your entry class;

After configuration, click Finish to generate the JAR file of the project.

The blog post only targets Java projects. For Android projects, we can also generate static jar files using the above methods.

Use of static jar plug-ins

After creating the static jar plug-in above, we generate a jar plug-in named drawcolor. Jar. The following shows how to load the file to the Android app for use.

First, copy the jar plug-in to the project directory. Create a lib folder in the root directory of the android project and copy the drawcolor. jar file to the Lib folder;

Second, add the jar plug-in to the project. Right-click the project, open the Java build path dialog box through the properties menu, select libraries, add jars, and select lib/drawcolor in the pop-up dialog box. JAR file (you can also right-click build path, build path-> libraries-> Add jars, and select a third-party jar package). The result is as follows:

Third, call the operations in jar. After completing the preceding steps, you can see the classes and members in the jar package under eclipse's project explorer. It is used like using the APIs in Android. Jar. The specific examples are not provided.

Fourth, package the added jar into the APK. Because the sdk api already has a plug-in the running environment (simulator and mobile phone), we don't need to package it into the APK, but the jar we added ourselves, you must package the package to the APK. Otherwise, the error "no package found" is reported during running. To package the package to APK, you need to do the following:

Find the. classpath file in the project directory, because the jar we add here is in lib/drawcolor. jar, so check whether the file contains the following line

<Classpathentrykind = "lib" Path = "lib/drawcolor. Jar"/>

If there is one, you can. If it is another, you have to change it. For example.

I suspect this is related to the eclipse version. In my compiling environment, the. classpath file of the project has been changed to the desired version when the jar plug-in step is added to the project.

The above implements Static Loading jar plug-in.

Dynamically load the jar plug-in. Create a dynamic jar plug-in

The creation of the dynamic jar plug-in is one more step than the static jar, because the android virtual machine is based on Dex, so our class cannot simply call the jar command to compress it, instead, you need to use the DX tool under the SDK \ platform-tools directory to perform type conversion. The following shows how to use the command line to generate the jar plug-in of a single class file addfunc. java.

1. compile and generate the addfunc. Class file using the javac command.

2. Because the package directory of the addfunc class is com. Demo. jar, copy the addfunc. Class file to the SDK \ platform-tools \ com \ demo \ jar folder.

3. Use the DX command to generate the jar plug-in file. The command for the preceding steps is as follows:

C: \> javac addfunc. Java

 

C: \> DX -- Dex -- output = addfunc. jarcom/demo/JAR/addfunc. Class

 

C: \> jar tvfd: \ Android \ SDK \ platform-tools \ addfunc. Jar

72 Fri Sep 23 14:28:48 CST 2011meta-inf/manifest. MF

964 Fri Sep 23 14:28:50 CST 2011 classes. Dex

Through the above steps, there is an addfunc. jar file on the SDK \ platform-tools. Here, it must be noted that addfunc. class must be placed according to the package name; otherwise, an error will be reported when the jar is generated.

In fact, if it is under the android project, you can compile the program under eclipse first, and then under the bin directory, the class file is placed by the package name, copy the required class file directory to the SDK \ platform-tools directory. If we need to package two classes, we can use the following command to implement

C: \> DX -- Dex -- output = addfunc. Jar COM/demo/JAR/addfunc. Class COM/demo/JAR/gameview. Class

After query, it is much larger than the classes. Dex File above. The details are as follows:

C: \> jar tvfd: \ Android \ SDK \ platform-tools \ addfunc. Jar

72 Fri Sep 23 14:38:06 CST 2011meta-inf/manifest. MF

1752 Fri Sep 2314: 38: 06 CST 2011 classes. Dex

All right, we need dynamic jar plug-ins.

Use of dynamic jar plug-ins

As mentioned above, we have generated a dynamic jar plug-in that requires dynamic loading, just like using the classloader class for dynamic loading in Java. In Android, we need to use dexclassloader for dynamic loading through the reflection mechanism.

If the dynamic jar plug-in is correctly created, there is no difficulty in this step. Let's explain it to the code.

The source code of addfunc is

Package com. Demo. jar;

 

Import Android. util. log;

 

Public class addfunc

{

Public addfunc ()

{

Log. I ("addfunc", "addfuncclass init ");

}

Public int add (int A, int B)

{

Int c = A + B;

Log. I ("addfunc", "addresult is" + C );

Return C;

}

}

After the above Code generates a dynamic jar plug-in, run the following command in the simulator to place the jar plug-in the running environment. This article is to place it in the SDK root directory of the simulator, as shown below:

C: \> ADB push addfunc. Jar sdcard/

6 kb/s (1149 bytes in 0.187 S)

In this way, you can use the reflection mechanism in the code to call the method in jar. The code for dynamically calling the jar plug-in the specific Android project is as follows:

Package com. Demo. Jar. runloadjardemo;

 

Importjava. Io. file;

Importjava. Lang. Reflect. method;

Import Dalvik. system. dexclassloader;

Import Android. App. activity;

Import Android. OS. Bundle;

 

Public class runloadjardemoactivityextends Activity

{

/** Called when the activity is first created .*/

Classmyclass = NULL;

@ Override

Public void oncreate (bundle savedinstancestate)

{

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Try

{

Filefile = new file ("/sdcard/addfunc. Jar ");

// File = newfile ("/sdcard/drawview.apk ");

If (file. exists ())

{

Dexclassloader Cl = new dexclassloader (file. tostring (), getfilesdir (). getabsolutepath (), null, classloader. getsystemclassloader (). getparent ());

Myclass = Cl. loadclass ("com. Demo. Jar. addfunc ");

Objectobj = myclass. newinstance ();

Class [] Params = newclass [2];

Params [0] = integer. type;

Params [1] = integer. type;

Method action = myclass. getmethod ("add", Params );

Int ret = (integer) action. Invoke (OBJ, 15, 20 );

}

}

Catch (exception ex)

{

Ex. printstacktrace ();

}

}

}

In principle, the dynamic jar plug-in generated above is the same as the APK generation. Therefore, we do not want to use the tedious DX tool, in this case, you can directly generate an APK from eclipse and use the classes and methods in the APK using the dynamic loading method. It is also feasible for me to test it.

Now, we will introduce the problem of loading the jar plug-in the Android app here. If you think that the above Code call reflection is too cumbersome, we can simplify reflection by designing interfaces. This topic is not described in this article.

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.