How to make a jar package and invoke the jar package in Android

Source: Internet
Author: User

Android Make jar package:

To create a new Android project, then right click, click Export, select the export type of the jar file under Java, do not select the Androidmanifest.xml and res folder in Java file specification. Otherwise, the error generating final archive:found duplicate file for Apk:androidmanifes will appear when the jar package is called.

If you call the jar package on Android, you will need to click Add External JARS (optionally the Add Library) in the Java build path of the project's properties to make a call to the function in the call jar package in the program.

Add Jar Package: (The jar is compiled under Windows and is available without cross-compiling, which also shows the feature of Java compilation across platforms)

You can also add a jar package from the Java build path by clicking on the Adding library (you can refer to the method of introducing a third-party jar package in Android, test it (http://www.havenliu.com/android/548.html))

Call the method in the jar package in the program (the hello in this article is the package name, the uppercase Hello class name, and test is the method):



--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------



Creating an executable jar package requires adding MANIFEST.MF. Do not need to execute the JAR package, do not need to MANIFEST.MF, just need to pack the class file to be able to. (Jar CVF A.jar hello.class hello2.class)

Make the executable jar package, note MANIFEST.MF, M (must uppercase) Ain-c (must be capitalized) Lass: (Must have a space) package name. Class name (must enter), must enter ,

Otherwise the "Fail to load Main-class manifest attribute from A.jar" error will appear when running the jar package (Java-jar A.jar).

--------------------------------------------------------------------------------------------------------------- --------------------



Other documents:

Summary of resource issues in jar packages (http://blog.csdn.net/yy4040/article/details/6641688)

Before a question was raised, see

Http://topic.csdn.net/u/20110630/14/ad71749c-631b-4fef-950d-92d5d6983628.html

The analysis results are as follows:

1, jar is included in other projects, the resulting APK file contains the resources in the jar (the directory structure is the same), this can be opened by the zip jar and apk view.

2. The resource in the jar should also be managed by the JVM, but because its actual ID is inconsistent with the ID generated in the original Jar project, the resource ID in the original JAR project cannot be used to access the actual resource. Access can only be done by GetClass (). getClassLoader (). getResourceAsStream ().

For component development using the JAR approach, you need to be aware of the following:

1. The jar package typically contains only code that does not contain resources.

2. If you want to include resources in the jar, the resource files should not be placed in the default res directory (because there may be conflicting names after importing other items), you should create a dedicated resource directory under the package directory, so that the pakcage does not conflict with the names of the imported resources. At the same time, the code in the jar cannot be accessed directly using the resource ID in the R class, and should be loaded by GetClass (). getClassLoader (). getResourceAsStream ().

3. String resources can be defined as constants in the way they are used.

Build your own library file jar under Android and call it in the app

(Transferred from http://hi.baidu.com/gaogaf/item/cef2285e2372bb444fff2046)

Mainly to solve the following problems:
Android's unlisted API is used in the project, and Red forks are displayed under Eclipse.
Different projects pull out the same part of the code that is shared.

Required Prerequisites:
Need to have the Android source code, the compiled library file is mainly to encapsulate the non-public API or common code.


Project 1:java library File Project

The project will eventually generate a Java library file that ends in a jar and will be installed to the following directory on the device:
/system/framework/
The documentation for the relevant library files is also installed to enable the system to locate the library files:
/system/etc/permissions/

1, establish the project
Create a new, empty Java project under Eclipse.
In the engineering
Java Build Path--Libraries
, import the Android.jar library in the Android SDK.
Create the appropriate packages and Java files for the project.
The following assumes that the package path is com.mytest.lib and the resulting library is Mylib.jar.

2. Add a registration file for the project
Create the following files in the root directory of the library file project:
<permission XML File Name>.xml
The file is used for the system registry, the name can be self-determined, under the assumption that the file is mylibxml.xml.
Add the following to the file:
<?xml version= "1.0" encoding= "UTF-8"?>
<permissions>
<library
Name= "Com.mytest.lib"
File= "/system/framework/mylib.jar"
/>
</permissions>
The library is used to associate the name and file two properties under it, and the value of name will be used behind it.
The name here is specified as the project's package name.
The file specified here is the storage path for the library files, which should be:
/system/framework/<jar file Name>.jar

3. Add makefile file for project:
Create the following files in the root directory of the library file project:
Android.mk
The file is used to compile the project, and the name is fixed.
Add the following to the file:
Local_path:= $ (call My-dir)
#MAKE_JAR
Include $ (clear_vars)
Local_src_files: = $ (call all-subdir-java-files)
Local_module: = Mylib
Include $ (build_java_library)
#MAKE_XML
Include $ (clear_vars)
Local_module: = Mylibxml.xml
Local_module_class: = ETC
Local_module_path: = $ (TARGET_OUT_ETC)/permissions
Local_src_files: = $ (Local_module)
Include $ (build_prebuilt)
Where the local_module portion of the Make_jar section is specified as the name of the library file that you want to generate,
Needs to be consistent with the file section under the Library in the. xml file.
Where the local_module portion of the Make_xml section is specified as an. XML file for the registration library.

4. Build the library files available to the application
At this point, if you use an undisclosed API, you should have a red cross under eclipse.
These errors do not need to worry about, only need to ensure that the program under the Android source code can be compiled through.
Copy the project's Java files as well as the. xml and. Mk to the following directory structure:
<android Source Folder>/packages/apps/<project name>/
Run the following command:
$ cd <android Source folder>
$ . ./build/envsetup.sh
$ mmm packages/apps/<project name>
A. jar file and an. xml file will be generated.
The above two files are required by the application at run time.
Before you debug your app, you need to copy two files to the appropriate directory on your device:
. jar Files:/system/framework/
. xml file:/system/etc/permissions/
Copy to device using the following command:
$ sudo adb push <.jar or. xml file path> <path in device>
The device needs to be restarted after the copy is finished.

5. Generate library files that can be referenced in eclipse
The. jar files compiled under the source code are not available in eclipse and need to be generated separately.
Under Eclipse, select the project root, select Export from the File menu, and then select the jar type.
When exporting to a jar file, you only need to select the appropriate Java file.
The resulting jar file is intended for use only by the application under Eclipse, except in the following cases:
The library file does not use a non-public API, and the
The application itself can be compiled under Eclipse, and it does not need to be compiled under the source code.
Name the file Mylibtemp.jar for the time being


Engineering 2:android Application Engineering

1, establish the project
Create a new Android project.
In the engineering
Java Build Path--Libraries
, import the Mylibtemp.jar library.
Write code using the classes in Mylibtemp.jar.
The following assumes that the last generated application file is myapp.apk.

2. Edit the Androidmanifest.xml file
Edit the Androidmanifest.xml file under the project root directory.
Under Application, add the following:
<uses-library
Android:name= "Com.mytest.lib" >
</uses-library>
Where: Name indicates the package name of the referenced library file.
It should be consistent with the name in the. xml file of the Library project.
This will allow you to find the appropriate. jar file when the app is running.
If you reference more than one library, you need to add multiple uses-library tags.

3. Add makefile file for project:
Create the following files in the root directory of the library file project:
Android.mk
The file is used to compile the project, and the name is fixed.
Add the following to the file:
Local_path:= $ (call My-dir)
Include $ (clear_vars)
Local_src_files: = $ (call All-java-files-under, SRC)
Local_java_libraries: = Mylib
Local_package_name: = MyApp
Local_certificate: = Platform
Include $ (build_package)
The local_java_libraries indicates the library file that the program will use.
The Local_module under the Make_jar section of the. mk File under the Library project should be consistent.
Where the local_package_name represents the last generated name of the app.

4. Compiling the application
The SRC, res, assets folders, and files for the project:
Android.mk and Androidmanifest.xml
Follow the directory structure to the following directory:
<android Source Folder>/packages/apps/<project name>/
Referenced. Jar library files are not required.
Run the following command:
$ cd <android Source folder>
$ . ./build/envsetup.sh
$ mmm packages/apps/<project name>
An. apk file will be generated.
Run the following command to install the app to the device:
$ sudo adb install [-r] <apk file path>

Here's how to make an executable jar package:

Transferred from: http://blog.chinaunix.net/uid-20640731-id-60041.html

Today I tried the method of making the executable jar file. As follows: First, create a project folder Jartest, and build the Hello package in the Jartest directory. Next, follow these steps: 1. Write The Hello.java (own class name) file under directory hello. The sample content is as follows: Package Hello;
public class Hello {
public static void Main (String args[]) {
System.out.println ("Hello world!");
}
} 2, compile and build the Hello.class file. 3, the MANIFEST.MF (name unrestricted) file is established in the sibling directory of Hello. The sample content is as follows: Main-class:hello. Hello this file is most important, the main points are: M (must uppercase) Ain-c (must be capitalized) Lass: (Must have a space) package name. Class name (must enter) 4, run the explicit Jar CVFM A.jar MANIFEST. MF hello/generate A.jar 5, test run Java-jar A.jar 6, Output: Hello world! The complete operation process is as follows: ================================================ ==d:program Filesjavasrcjartesthello>javac Hello.javaD:Program FILESJAVASRCJARTESTHELLO&GT;CD. D:program Filesjavasrcjartest>jar cvfm A.jar MANIFEST. MF hello/
Mark List (manifest)
Added: hello/(read in = 0) (write = 0) (0% stored)
Added: Hello/hello.class (read in = 422) (write = 289) (31% compressed)
Added: Hello/hello.java (read in = 136) (write = 108) (compressed 20%) D:program Filesjavasrcjartest>java-jar A.jar
Hello world! D:program filesjavasrcjartest>================================================== can be Hello.java deleted after compilation.

According to the following http://blog.chinaunix.net/uid-20640731-id-60041.html test, success.

Creating an executable jar package requires adding MANIFEST.MF. Do not need to execute the JAR package, do not need to MANIFEST.MF, just need to pack the class file to be able to. (Jar CVF A.jar hello.class hello2.class)

Make the executable jar package, note MANIFEST.MF, M (must uppercase) Ain-c (must be capitalized) Lass: (Must have a space) package name. Class name (must enter), must enter ,

Otherwise the "Fail to load Main-class manifest attribute from A.jar" error will appear when running the jar package (Java-jar A.jar).

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------



Creating an executable jar package requires adding MANIFEST.MF. Do not need to execute the JAR package, do not need to MANIFEST.MF, just need to pack the class file to be able to. (Jar CVF A.jar hello.class hello2.class)

Make the executable jar package, note MANIFEST.MF, M (must uppercase) Ain-c (must be capitalized) Lass: (Must have a space) package name. Class name (must enter), must enter ,

Otherwise the "Fail to load Main-class manifest attribute from A.jar" error will appear when running the jar package (Java-jar A.jar).

--------------------------------------------------------------------------------------------------------------- --------------------

How to make a jar package and invoke the jar package in Android

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.