0

I wanted to implement gson in my android project but after I included the gson implementation in Gradle an error occurred like this:

Type com.google.gson.annotations.Expose is defined multiple times

I tried to lower the version into 2.8.2 but a different error occurred like this:

error: error while writing Expose: could not create parent directories public @interface Expose {

I did not use the implementation yet in my project but the error keeps on preventing the app to run.

gradle:

 apply plugin: 'com.android.application'

    android {
       compileSdkVersion 28
       buildToolsVersion '26'
       defaultConfig {
        useLibrary 'org.apache.http.legacy'
        manifestPlaceholders = [applicationName: "Sample"]
        applicationId "com.odoo"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 7
        versionName "2.3.0"
    }
    buildTypes {
        debug {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    //implementation 'com.google.android.gms:play-services-gcm:10.2.1'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.volley:volley:1.1.0'
    implementation project(':intro-slider-lib')
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.code.gson:gson:2.8.6'
}

updated:

I tried also using a simple example using gson without implementing it:

 Gson gson = new Gson();
 Sample sample= new Sample("Sample", 20, 15, 5);
 String json = gson.toJson(sample);

the result is null, json {}

Why does the implementation showing errors like that during runtime and how do I fix it? Thanks.

plexus
  • 101
  • 1
  • 11

2 Answers2

0

Do this:

File -> Invalidate & Restart Android

This will clear all the cache data that is causing the issue.

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
0

Looks like gson is already being used in intro-slider-lib you need to exclude it from there

implementation project(':intro-slider-lib') {
    transitive = true; 
    exclude module: 'gson' 
}
karan
  • 8,637
  • 3
  • 41
  • 78
  • it throws an error 'Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method exclude() for arguments [com.google.code.gson]' – plexus Apr 22 '20 at 01:32
  • it throws also an error 'Caused by: groovy.lang.MissingPropertyException: Could not set unknown property 'transitive' for project ':intro-slider-lib' of type org.gradle.api.Project' , I decided not to implement the gson because it seems one of the implemnation that are already in build.gradle are has it – plexus Apr 23 '20 at 02:48