Android Studio java.lang.NoSuchMethodError with imported library

I imported commons-codec-1.10.jar by following these steps:

  • The libs directory is created in the application directory
  • Manually copied .jar to the libs directory
  • Right click on .jar inside android studio and click "Add as library"

Added this line to my build.grade

compile fileTree(dir: 'libs', include: ['*.jar'])

In my class, I imported the library as follows:

import org.apache.commons.codec.binary.Base64;

Then I tried to access the encodeBase64String static method inside Base64 as follows:

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);
    }
}

There is nothing bad there, even when I compile, except when I launch the application, it gives the following error and the application shuts down:

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)

DoThisThing, , , . , , encodeBase64String . , , java android.

+4
3

org.apache.commons.codec.binary.Base64 

android.util.Base64

.

public static class DoThisThing {
 public String DoThisOtherThing() {
    String hashed = "hello";
    byte[] data = hashed.getBytes("UTF-8");
    String hash = Base64.encodeToString(data, Base64.DEFAULT);
    return hash;
 }
}
+7

Android framework (1.3) commons-codec . , , . Base64#encodeBase64String 1.4, java.lang.NoSuchMethodError.

jarjar.

blogpost, , .

+1

, , , . , android.util.Base64

            String hash = Base64.encodeToString(hasheado.doFinal(json.getBytes()), Base64.DEFAULT);
0
source

Source: https://habr.com/ru/post/1614294/


All Articles