2015-11-03 103 views
4

我导入后的下一步骤公地编解码器1.10.jar:Android Studio中java.lang.NoSuchMethodError与导入库

  1. 在德的应用程序目录中创建手动复制的一个libs目录
  2. 。 libs目录内的罐子
  3. 右击Android的工作室里面的.jar和点击添加为库

加入这一行我build.grade

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

在我的课堂我进口这样的库:

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

然后我试图访问encodeBase64String静态方法中的Base64这样的:

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

+2

这可能会帮助你

+0

Omg谢谢。这似乎很有帮助。我会让你知道我做了什么 –

回答

7

android.util.Base64 

更换

org.apache.commons.codec.binary.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; 
} 
} 
0

嗯,只是告诉大家,我无法解决这个问题。我用的是原生的Android库进行编码和解码这是在android.util.Base64

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

Android框架包括在classpath上的commons-codec库的古老版本(1.3)。在运行时,它将使用它的类,而不是与应用程序一起打包的类。在1.4中引入了Base64#encodeBase64String方法,因此您收到了java.lang.NoSuchMethodError例外。

一个可能的解决方案是通过使用jarjar重新打包它来更改库的名称空间。

查看我的blogpost,更详细地解释了问题并显示如何重新打包库。