Android project combat (24): The project is packaged into a jar file, and the jars referenced in the project are entered into a new jar file together

Original: Android project combat (24): The project is packaged into a jar file, and the jars referenced in the project are put into a new jar file together

Foreword:

About .jar files:

Usually we often use third-party .jar files in the development of Android projects.

In fact, a .jar file is a compressed package similar to a .zip file, which contains some source code. Note that .jar does not contain resource files (res, pictures, etc.)

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

1. First learn how to type an 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 .jar file

There is a Terminal option under Android studio, you can see the form of this command line

We type "gradle makeJar" here and hit enter. Wait for a while, the .jar file will be generated

The address of the .jar file is:

build/intermediates/bundles/release/

 

At this point, we can use the generated .jar in other projects.

 

2. In-depth exploration

question:

There is such a situation:

One or more third-party .jar files have been referenced in the android project we want to package . Then we make a .jar file according to the above method and use it in other projects.

An error will be found:

NoClassDefFoundError

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

It can be guessed here that in the process of using the above method to convert the android project into a .jar file, the third-party .jar file originally referenced by the android project is not also entered into the new .jar file .

You can test it, decompress the .jar file made by the android project, and you will find that there are only java code related files, but no third-party .jar files referenced in the original android project.

 

solution:

1. The first reason for the problem is that the third-party .jar file originally referenced by the android project that generated the .jar file was not inserted into the new .jar file

  Then we thought: how to enter the third-party .jar culture referenced by the android project into the new .jar file when the android project is converted into a .jar file?

Tried for a few days and have not found a solution so far. . . No way

 

2. Since the .jar file originally referenced by the android project cannot be typed into the .jar file to be generated, can we manually integrate the third-party projects referenced by the android project into the .jar file created by the android project?

  Combining the .jar file created by the android project and the .jar file referenced by the android project itself into a .jar file

Query on the Internet: You can use the ANT tool to combine two or more .jar files into one .jar file

Apache Ant is a Java-based build tool. According to original founder James Duncan Davidson, the name of the tool is an acronym for another neat tool.

Implementation steps:

(1), download ANT, this online Baidu bunch

(2), configure environment variables

  

(3), check whether the configuration is successful

Click Start -> Run -> Enter cmd 
to 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, we need to create this file. 
If you don't want to name it build.xml, you can use the ant -buildfile test.xml command to specify the build file to run at runtime.

(4), edit the xml file, name it at will, here name build.xml

Open the file for editing:

<?xml version="1.0" encoding="utf-8"?>
<project
    name = " hosa " //No need to change, note: all the comments here should not be included in the build.xml file. I wrote 
    basedir = " H:\soft\jar " //The generated jar is stored in order to show you the explanation. The location of, and all the .jar files to be merged are also placed in this directory 
    default = " makeSuperJar " > //No need to change

    <target
        name = " makeSuperJar " // do not need to change   
        description = " description " > // do not need to change

        <jar destfile= " standingbanlanceFB.jar " > //The name of the merged jar file
         <zipfileset src= " avoscloud-sdk-v3.14.5.jar " /> // <zipfileset > tags 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) Perform the merge operation on the command line, enter the first line of the command, 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 , and you can see the merged jar file.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325034682&siteId=291194637