我知道有几十个人( like this one )发布了相同的堆栈跟踪,但是在处理完每个人之后,我仍然缺少解决方案。我有一个正在处理的学校项目,我们正在依赖 Gson 的 Android Studio(Gradle 是构建工具)中构建它。它由三个模块组成:
app
(Android Studio 生成的安卓应用)server
(应用程序将调用它的服务器)shared
(服务器和应用程序都依赖的类)两个
app
和 server
在其 build.gradle 依赖项中有以下行:implementation project(':shared')
所以两者都依赖于
shared
. shared
反过来,在 shared/libs
中有 gson-2.6.2.jar (尽管我也尝试将其删除并使用远程存储库),并且所有三个都将其作为依赖项:implementation fileTree(dir: 'libs', include: ['*.jar'])
shared
中有一个类调用Serializer
它使用 Gson。但是当我运行 server
Android Studio 中的模块并且它调用了 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,但它拒绝在运行时工作。我很茫然,任何帮助将不胜感激!应用程序/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"
共享/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
中添加此依赖项
implementation 'com.google.code.gson:gson:2.8.2'
关于java - 3 模块 Android Studio 项目中的 GSON/Gradle : java. lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48716829/