Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Shall not be reproduced without permission.

One, foreword

When developing an App, there are often situations where you need to use a third-party SDK. However, sometimes multiple SDKS may introduce the same library, or have the same class name and package name. In this case, if both SDKS are imported at the same time, the duplicate entry error will appear.


Second, analyze the problem

First, analyze the problem. If the SDK is imported using Gradle compile, and if the SDK integrates other libraries in the same way, it is ideal to exclude the imported libraries in compile. The use of exclude will be covered at a later opportunity, but it is not the focus of this article.

Using exclude is an ideal situation. In most cases, the conflicts of duplicate Entry are classes in the other side’s code (maybe the other side introduced the open source library by importing the source code). In this case, using exclude does not work.

If we can change the package name of a class in one of the SDKS, we can solve this problem.

Next comes the tool to change the package name of the jar class: jarjar.jar.

Second, jarjar. Jar

1, What is Jar Jar

Jar Jar Link is a useful tool that makes it easy to repackage Java libraries into a separate Jar package with no external dependencies that can be easily embedded in our published projects. In this case, jarjar.jar is mainly used to solve the problem of duplicate entry file conflicts.

Jarjar provides a very convenient *.jar tool for us to use. This is an open source project and provides a way to work with Gradle. Usually, we don’t use this kind of operation very often, so when we need to use it, we just need to make a change, there is no need to integrate into the project.

JarJar address: code.google.com/archive/p/j…

With Gradle, the readme of the project is already written clearly. If you are interested, you can check it out.

Github.com/shevek/jarj…

2. Use Jarjar

Since most of the time we don’t need to change jars very often, this is just how to use the jarjar.jar tool to change jars for us.

Use the latest version available for download: jarjar-1.4.jar

Download address: code.google.com/archive/p/j…

Before starting to use jarjar.jar, it is necessary to read the help documentation. In addition to reading the usage documentation on Github, you can also view the usage documentation of jarjar.jar by command.

java -jar jarjar.jar


The core commands of jarjar.jar are as follows:

  • View help:jarjar.jar
  • To view all package names:jarjar.jar strings <xxx.jar>
  • Replacement package name:jarjar.jar process <rulesFile> <inJar> <outJar>

Jarjar provides a method to view the package name, but it is not usually used. For example, if you provide a package named cxmylib.jar, use the strings command to check its contents.



cxmylib.jar

The most important method of using Jarjar is the command used to modify the Jar:

java -jar process <rulesFile> <inJar> <outJar>

InJar outJar inJar outJar inJar outJar inJar outJar

However, for such a change, how jarjar knows which packages need to be modified requires the use of rulesFile to configure the rules.

3. RulesFile Configure and modify rules

The rulesFile is fine as long as it is a text file, and it mainly contains three commands.

1. Rule specifies the replacement Package.

rule pattern result

2. Zap removes packages that comply with the rule

zap pattern

3, keep the Package that meets the requirements

keep pattern

Where pattern is used to specify a package with an operation. To facilitate operation, “” and” ** “wildcards can be used for matching.” “represents a package name, and” ** “will match any string with a valid class name. Result can specify substrings matching wildcards in pattern by @1 or @2.

These are all standard uses of wildcards, nothing to go into detail about. Let’s look at an example.

rule com.cxmydev.** com.cxmylibdev.@1

Such a rule would replace com.cxmydev.a.java with com.cxmylibdev.a.jar.

And since there are three rules, they must have priority. First, zAP specifies all classes to be deleted, and then executes the rule to replace the classes that meet the requirements. Finally, if the KEEP rule is configured, zAP executes the KEEP rule to remove all classes that do not meet the rules, and only keep the packages specified by KEEP.

In summary, the execution priority of the three commands is zap > rule > keep.

4. Take an example

First, edit the rule-txt file to specify the change rules.

rule com.cxmydev.** com.cxmylibdev.@1

Then use the process command to make the changes.

java -jar jarjar.jar process rule.txt cxmylib.jar cxmylib_new.jar

You can see the modified cxmylib_new.jar file in the current directory.

Then use JADX to look at the source code and verify the changes.


Modify the AAR

In Android projects, libraries can be introduced in jar format as well as AAR format. Aar and JAR are simply additional resource files, such as layouts, images, colors, so libraries, etc.

So what do we do when we have a situation where we need to modify an AAR? The AAR is also a standard zip package, so all you need to do is unpack it to get the classes.jar file, modify it, and package it back.

Here is the same library, packaged out of the AAR file, to modify.

1. Run the unzip command to decompress the package


2. Modify the classes.jar file

As in the previous example, modify the classes.jar file and then replace it.

3. Use jar command to package back to AAR


Four, shortcomings

As you can see, it is very convenient to use jarjar.jar if you just need to change the package name of an existing JAR and repackage it.

But it also has drawbacks:

  • Reflection cannot be supported. If a reflection call is used in a JAR package, it cannot be modified altogether.
  • Aar resource files cannot be modified either (jarjar.jar can only modify *.jar files).

However, it is always risky to modify the SDK of a third party and may cause unexpected problems. It is best to try to communicate with the third party and have the third party provide a modified package for integration.

The sample files in this article can be obtained by following cxmydev and replying with the keyword “jarjar”.

Qr code. JPG