Java and Android commons-codec use and NoSuchMethodError problem

Introduction

Commons codec is a tool package provided by the Apache open source organization to handle commonly used encoding methods, such as DES, SHA1, MD5, Base64, URL, Soundx, etc. Not only for encoding, but also for decoding.

Common classes and functions

1. Base64 encoding and decoding 2. Hex encoding and decoding 3. MD5 encryption (MD5 is an irreversible algorithm and can only be encrypted) 4. SHA encryption 5. Metaphone and Soundex 6. URLCodec

Source code

Address
https://commons.apache.org/proper/commons-codec/download_codec.cgi
or
https://github.com/apache/commons-codec

Used in Java projects

<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.15</version>
</dependency>

Android use

Unfortunately, no gradle dependency is provided. You can only copy the jar package from the source code or download the bin file to the Android project for use, but java.lang.NoSuchMethodError: No static method decodeHex(Ljava/lang/String;)
[B in class Lorg/apache/commons/codec/binary/Hex; or its super classes (declaration of'org.apache.commons.codec.binary.Hex' appears in /system/framework/org.apache.http.legacy .boot.jar)

the reason

There is also a package name and class name of the API we use inside the Android system. The key point is that there is no such method in the system! This leads to a package name conflict, and then the system finds the class with the same name in its own SDK, and then calls the method we used under the system's own class.
However, the system does not have that method at all, and it is not called at all. Then there is no more

Solution

Get the source code yourself, modify the package name, and then recompile it into a jar package, and then import it into the Android project for use; recompile and run your own Android project, you will find that the previous problems are gone;

The commons-codec version that can be used in Android is 1.15

https://pan.baidu.com/s/1zb9LWobL_Q9dHJdyZwvYjg 

Extraction code: 4v7q

Guess you like

Origin blog.csdn.net/ezconn/article/details/108782187