Android SDK开发报错NoClassDefFoundError: Failed resolution of: Lcom/android/volley/toolbox/StringRequest

Android SDK开发报错NoClassDefFoundError: Failed resolution of: Lcom/android/volley/toolbox/StringRequest

  • 前言
  • 解决方法
  • 完事

前言

最近在封装sdk,里面的http请求使用了Google的Volley网络框架,在混淆打包以后,在其他工程里面运行报错,错误如下:

     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/android/volley/toolbox/StringRequest;
        at com.rdwl.rdsigmeshlib.http.RDHttp.<init>(Unknown Source:8)
        at com.rdwl.rdsigmeshlib.http.RDHttp$Companion.getInstance(Unknown Source:15)
        at com.rdwl.sigmeshdemo.activity.RegisterActivity.getCode(RegisterActivity.kt:44)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
        at android.view.View.performClick(View.java:6608) 
        at android.view.View.performClickInternal(View.java:6585) 
        at android.view.View.access$3100(View.java:785) 
        at android.view.View$PerformClick.run(View.java:25921) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6861) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.volley.toolbox.StringRequest" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.rdwl.sigmeshdemo-sxexhkCXCIuH9KPUmyFfDg==/base.apk"],nativeLibraryDirectories=[/data/app/com.rdwl.sigmeshdemo-sxexhkCXCIuH9KPUmyFfDg==/lib/arm, /data/app/com.rdwl.sigmeshdemo-sxexhkCXCIuH9KPUmyFfDg==/base.apk!/lib/armeabi-v7a, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.rdwl.rdsigmeshlib.http.RDHttp.<init>(Unknown Source:8) 
        at com.rdwl.rdsigmeshlib.http.RDHttp$Companion.getInstance(Unknown Source:15) 
        at com.rdwl.sigmeshdemo.activity.RegisterActivity.getCode(RegisterActivity.kt:44) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
        at android.view.View.performClick(View.java:6608) 
        at android.view.View.performClickInternal(View.java:6585) 
        at android.view.View.access$3100(View.java:785) 
        at android.view.View$PerformClick.run(View.java:25921) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6861) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

我的混淆规则比较直接,如下:

-keep class com.android.volley.** {*;}

我一直以为是我的混淆规则有问题,导致找不到这个相关类。搞了好久。。。

解决方法

比较蠢的解决方法,就是反正这个网络框架都是开源的,直接在项目里依赖完我的sdk后再依赖volley就好了。保证sdk中的volley依赖和项目中的版本号相同,这样就没问题了。

    // 我的sdk包
    implementation files('libs/xxxx.aar')
    // 依赖volley
    implementation 'com.android.volley:volley:1.1.1'

完事

你可能感兴趣的:(错误)