【问题标题】:java.lang.NoClassDefFoundError: Failed resolution of: Lcom/android/volley/VolleyLog;java.lang.NoClassDefFoundError:解析失败:Lcom/android/volley/VolleyLog;
【发布时间】:2020-04-23 17:25:18
【问题描述】:

我创建了新的 android 项目,并在项目内部创建了新的库模块(网络)。

现在我们有了应用(默认模块)和网络模块。

在网络模块中,我有 volley 依赖项,而 app 模块中没有。

创建 Kotlin 类并在 Network Module 中定义此方法

/ 网络模块/

build.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_8
         targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'

    // Volley Network
    implementation 'com.android.volley:volley:1.1.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

// VolleyUtils.kt

 class VolleyUtils {
     fun enableLog() {
            VolleyLog.DEBUG = true
     }
 }

从网络模块创建 AAR 并放置在应用模块中

/ 应用模块 /

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.test.base"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_8
         targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'

    implementation(name: 'volleynetwork-release', ext: 'aar')
//    implementation project(':volleynetwork')

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

// MainActivity.kt

 class MainActivity : AppCompatActivity() {
       super.onreate(saveInstanceState)
       setContentView(R.layout.activity_main)
       //access method in network module
       VolleyUtils().enableLog()
    }
}

编译成功。但是,@Runtime 导致异常

结果:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/android/volley/VolleyLog;
    at com.test.volleynetwork.VolleyUtils.enableLog(VolleyUtils.kt:8)
    at com.test.base.MainActivity.onCreate(MainActivity.kt:12)
    at android.app.Activity.performCreate(Activity.java:7144)
    at android.app.Activity.performCreate(Activity.java:7135)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2932)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3087)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6746)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.volley.VolleyLog" on path: DexPathList[[zip file "/data/app/com.test.base-LKNAponHFP3pH9lW8RU9sQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.test.base-LKNAponHFP3pH9lW8RU9sQ==/lib/arm64, /system/lib64, /product/lib64]]

所以,只有在生成 AAR 时才会出现问题,否则它工作正常 请指导我哪里出错了。

【问题讨论】:

    标签: android kotlin exception module aar


    【解决方案1】:

    在应用的 build.gradle 中添加以下内容:

    implementation(project(":Name_of_your_network_module"))
    

    注意:在开头保留“:”。

    编辑

    尝试将 Network Module 中的 Volley 依赖项从 implementation 更改为 api.

    【讨论】:

    • 嗨,Karan,我只有在 AAR 中访问该方法时才收到此错误。否则,它工作正常。
    • 您正在尝试从 :APP 模块访问 VolleyLog 类。很确定你说在网络构建 gradle 中实现,这就是它没有暴露给应用程序模块的原因。发布您的构建 gradle 文件。
    • 兄弟,我已经更新了默认应用模块和网络模块 gradle。
    【解决方案2】:

    添加你的aar,然后导入网络模块aar 只需在 build.gradle 应用模块中再次将 volley 依赖项添加到您的基础项目即可。

    依赖{

    ....

    实现'com.android.volley:volley:1.1.1'

    }

    因为aar可能是HurlStack的一些依赖文件被移除了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2020-01-07
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      相关资源
      最近更新 更多