I know there are a few dozen people (like this one) who've posted with the same stacktrace, but after working through each of them I'm still missing a solution. I have a school project I'm working on which we're building in Android Studio (Gradle is the build tool) that depends on Gson. It is structured with three modules:
app
(the android app generated by Android Studio)server
(the server which the app will be directing its calls to)shared
(classes that the server and app both depend on)
Both app
and server
have the following line in their build.gradle dependency:
implementation project(':shared')
So both depend on shared
. shared
, in turn, has gson-2.6.2.jar in shared/libs
(though I've tried removing it and using the remote repository instead too), and all three have this as a dependency:
implementation fileTree(dir: 'libs', include: ['*.jar'])
There's a class in shared
called Serializer
which uses Gson. But when I'm running the server
module within Android Studio and it gets to a call to Serializer, I get this stack trace:
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
Solutions on Stack Overflow focus on making sure the dependencies are in the build.gradle
files and cleaning and rebuilding the project. I've tried including the dependency on Gson in all three modules individually, tried using a remote Gson, and cleaned and rebuilt countless times. I'm always able to use Gson while coding, but it refuses to work at runtime. I'm at a loss, and any help would be appreciated!
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'
}
server/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"
EDIT: Updated to indicate that I'm using the directive to use remote instead of libs/