I imported the commons-codec-1.10.jar following the next steps:
Added this line in my build.grade
compile fileTree(dir: 'libs', include: ['*.jar'])
In my class I imported the library like this:
import org.apache.commons.codec.binary.Base64;
Then I tried to access the encodeBase64String static method inside Base64 like this:
public static class DoThisThing {
public String DoThisOtherThing() {
String hashed = "hello";
String hash = Base64.encodeBase64String(hashed.getBytes());
return hash;
}
}
public class ActivityThing extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_thing);
String hash = DoThisThing.DoThisOtherThing();
System.out.println(hash);
}
}
Nothing wrong there, even when I compile, except when I run the app, it throws the following error and the app shuts:
11-03 09:41:27.719 2390-2476/com.myproject E/AndroidRuntime: Caused by: java.lang.NoSuchMethodError: No static method encodeBase64String([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' appears in /system/framework/org.apache.http.legacy.boot.jar)
My DoThisThing class is not inside the activity by the way, it's just to make it short. I checked the library an indeed the encodeBase64String is static. So I don't know exactly what to do, I'm new in the java and android environment . So any help would be much appreciated
A java. lang. NoSuchMethodError is a runtime error in Java which occurs when a method is called that exists at compile-time, but does not exist at runtime. The Java Garbage Collector (GC) cannot free up the space required for a new object, which causes a java.
NoSuchMethodError: main Exception in thread "main" can come due to various reasons like: 1) The class which you are trying to run doesn't have the main method. 2) The signature of the main method is not correct. See here for all possible signatures of the main method in Java.
Android framework includes ancient version (1.3) of commons-codec library on a classpath. In runtime, it will use its class instead of the one that packaged with your app. Base64#encodeBase64String
method was introduced in 1.4 and therefore you're receiving the java.lang.NoSuchMethodError
exception.
One possible solution will be to change the library's namespace by repackaging it using jarjar.
See my blogpost that explains the issue in more detail and shows how to repackage the library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With