The Android project is packaged into a jar file, and the jar files referenced in the project are combined into the new jar file. androidjar

Source: Internet
Author: User

The Android project is packaged into a jar file, and the jar files referenced in the project are combined into the new jar file. androidjar

Preface:

About the. jar file:

Third-party. jar files are usually used in Android project development.

The. jarfile contains a compressed. ZIP file, which contains some source code. Note that. jar does not contain resource files (res, images, etc)

Bytes ---------------------------------------------------------------------------------------------------------

I. First, I learned how to compile the Android project into a. jar file in android studio.

1. Add the following red font code to the build. gradle file in the app directory:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}

task makeJar(type: Copy) {
delete 'build/libs/mysdk.jar'
from('build/intermediates/bundles/release/')
into('build/libs/')
include('classes.jar')
rename ('classes.jar', 'myjar.jar')
}

makeJar.dependsOn(build)

2. Generate the. jar File

There is the Terminal option under Android studio. You can see the command line format.

Enter "gradle makeJar" here and press Enter. Wait for a moment, the. jar file will be generated

The address of the. jar file is:

build/intermediates/bundles/release/

 

So far, we can use the generated. jar in other projects.

 

Ii. in-depth exploration

Problem:

There is such a situation:

One or more third-party. jar files have been referenced in the android project to be packaged. Then we press the above method to compress the. jar file and use it in other projects,

An error occurs:

NoClassDefFoundError

Locate the error code and you will find that the class in the third-party. jar file referenced in the original android project that generated the. jar file cannot be found.

Here we can guess that we can use the above method to compress the android project. during the jar file process, no third party referenced by the android project. jar files are also written into the new. jar file.

You can test whether to decompress the. jar file compiled by the android project. Only java code-related files are found, but no third-party. jar files referenced in the original android project are found.

 

Solution:

1. First, the cause of the problem is that the third-party. jar file originally referenced by the android project that generated the. jar file is not injected into the new. jar file.

So we thought: How can we break the third-party. jar file referenced by the android project into the new. jar file along with the. jar file in the android project?

After trying for a few days, no solution is found... This cannot be achieved

 

2. Since the android project was originally referenced. jar files cannot be generated. can we manually integrate third-party projects referenced by android projects into android projects. in the jar file?

Merge the. jar file compiled by the android project and the. jar file referenced by the android project into a. jar file.

I checked it online: You can use ANT to merge two or more. jar files into one. jar file.

Apache Ant is a Java-based generation tool. According to James Duncan David son, the original founder, the tool name is the abbreviation of another neat tool (another neat tool.

Steps:

(1) download ANT, a bunch of Baidu online

(2) Configure Environment Variables

  

(3) check whether the configuration is successful

Click Start> RUN> Enter cmd.
Open the command window,

Enter the following command: ant
If the following content appears, the installation is successful:
Buildfile: build. xml does not exist!
Build failed
Note: Because ant runs the build. xml file by default, this file needs to be created.
If you do not want to name it build. xml, you can run the ant-buildfile test. xml command to specify the build file to be run.

(4) edit the xml file and name it randomly. Here we name it build. xml.

Open File Editing:

<? Xml version = "1.0" encoding = "UTF-8"?> <Project name = "hosa" // you do not need to change the location where basedir = "H: \ soft \ jar" // generated jar is stored and all the jar files to be merged. jar files are also stored in the directory default = "makeSuperJar"> // you do not need to change <target name = "makeSuperJar" // you do not need to change description = "description"> // you do not need to change <jar destfile = "standingbanlanceFB. jar "> // The name of the merged jar file <zipfileset src =" avoscloud-sdk-v3.14.5.jar "/> // The <zipfileset> labels are all sub-jar packages to be merged <zipfileset src = "fastjson. jar "/> <zipfileset src =" okhttp-2.6.0-leancloud.jar "/> <zipfileset src =" standingbanlance. jar "/> <zipfileset src =" okio-1.6.0-leancloud.jar "/> </jar> </target> </project>

(5) execute the merge operation on the command line and enter the first command line. The red part is the address of the build. xml file we edited.

 

(6) Open the directory basedir = "H: \ soft \ jar" in the build. xml file to view the merged jar file.

 

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.