繁体   English   中英

GSON / Gradle:3模块Android Studio项目中的java.lang.NoClassDefFoundError

[英]GSON/Gradle: java.lang.NoClassDefFoundError in 3-module Android Studio Project

我知道有几十个人( 像这样 )已经发布了相同的堆栈跟踪信息,但是在研究了每个人之后,我仍然缺少解决方案。 我正在处理一个学校项目,我们正在Android Studio(取决于Gson的构建工具)中进行构建。 它由三个模块构成:

  • app (由Android Studio生成的android应用)
  • server (应用程序将其定向到的服务器)
  • shared (服务器和应用程序都依赖的类)

appserver在build.gradle依赖项中都包含以下行:

implementation project(':shared')

因此,两者都取决于shared shared ,反过来又GSON-2.6.2.jar在shared/libs (虽然我试着删除它,并使用远程仓库,而不是太),和所有三个有这个作为一个依赖:

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

shared有一个叫Serializer的类,它使用Gson。 但是,当我在Android Studio中运行server模块并调用Serializer时,会得到以下堆栈跟踪:

Exception in thread "Thread-2" java.lang.NoClassDefFoundError: com/google/gson/Gson
    at my.t2r.comm.Serializer.<init>(Serializer.java:25)
...
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

Stack Overflow的解决方案着重于确保依赖项位于build.gradle文件中,以及清理和重建项目。 我尝试将对Gson的依赖关系分别包含在所有三个模块中,尝试使用远程Gson,并且清理和重建了无数次。 我总是能够在编码时使用Gson,但是它拒绝在运行时工作。 我很茫然,任何帮助将不胜感激!

app / build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "my.t2r"
        minSdkVersion 24
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '26.0.2'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation project(':shared')
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.bignerdranch.android:expandablerecyclerview:1.0.3'
}

服务器/ build.gradle:

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':shared')
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.code.gson:gson:2.8.2' // I've also tried api instead
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

shared / build.gradle:

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.code.gson:gson:2.8.2' // I've also tried api instead
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

编辑:更新以指示我正在使用指令使用远程而不是libs /

在您的app\\build.gradle添加此依赖app\\build.gradle

implementation 'com.google.code.gson:gson:2.8.2'

我找到了答案!

我假设IDE出了点问题。 当我在IDEA中而不是Android Studio中加载项目时,Gson依赖项的作用域为提供的而不是编译的。 不幸的是,在Android Studio中无法查看这些设置。 经过数小时的搜寻之后,我终于在一个SO聊天室找到了这个答案

artifacts.add("default", file('libs/gson-2.6.2.jar'))

将此行添加到shared/build.gradle (不在任何括号内,只是免费)解决了我的问题! 我不需要在server/build.gradle任何更改。

我有同样的问题,清理或缓存入侵或Gradle更改均无济于事。 最终,事实证明它是一个仿真器实例(API 19)所独有的。 其他仿真器和物理设备都可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM