带有导入库的Android Studio java.lang.NoSuchMethodError(Android Studio java.lang.NoSuchMethodError with an imported library)

我按照以下步骤导入了commons-codec-1.10.jar:

在de app目录下创建了一个libs目录 手动复制libs目录中的.jar 右键单击android-studio中的.jar,然后单击Add as library

在我的build.grade中添加了这一行

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

在我的课上我导入了这样的库:

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

然后我尝试在Base64中访问encodeBase64String静态方法,如下所示:

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

没有错,即使我编译,除非我运行应用程序,它会抛出以下错误,应用程序关闭:

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环境中的新手。 所以任何帮助将不胜感激

I imported the commons-codec-1.10.jar following the next steps:

Under de app directory created a libs directory Copied manually the .jar inside the libs directory Right click the .jar inside android-studio and clicked Add as library

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

最满意答案

更换

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

Replace

org.apache.commons.codec.binary.Base64

for

android.util.Base64

And update your method like this.

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; } }带有导入库的Android Studio java.lang.NoSuchMethodError(Android Studio java.lang.NoSuchMethodError with an imported library)

我按照以下步骤导入了commons-codec-1.10.jar:

在de app目录下创建了一个libs目录 手动复制libs目录中的.jar 右键单击android-studio中的.jar,然后单击Add as library

在我的build.grade中添加了这一行

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

在我的课上我导入了这样的库:

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

然后我尝试在Base64中访问encodeBase64String静态方法,如下所示:

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

没有错,即使我编译,除非我运行应用程序,它会抛出以下错误,应用程序关闭:

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环境中的新手。 所以任何帮助将不胜感激

I imported the commons-codec-1.10.jar following the next steps:

Under de app directory created a libs directory Copied manually the .jar inside the libs directory Right click the .jar inside android-studio and clicked Add as library

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

最满意答案

更换

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

Replace

org.apache.commons.codec.binary.Base64

for

android.util.Base64

And update your method like this.

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