簡體   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