【问题标题】:Cannot Import GSON/JSON in Android Studio Using Maven无法使用 Maven 在 Android Studio 中导入 GSON/JSON
【发布时间】:2016-04-13 18:45:11
【问题描述】:

我在使用 Maven 导入 com.google.gson 时遇到问题。现在,我收到以下错误:

Error:(24, 17) Failed to resolve: junit:junit:4.12

Error:(22, 13) Failed to resolve: com.google.code.gson:gson:2.5

这是项目的 build.gradle:

buildscript {
    repositories {
        jcenter()
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

repositories {
    jcenter()
    mavenCentral()
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这里是“模块:应用程序”的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.name.testversion"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.google.code.gson:gson:2.5'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.google.android.gms:play-services:8.3.0'
}

请告诉我如何解决这些问题并成功将“com.google.code.gson:gson:2.5”导入 Android Studio 以用于此项目。

另外,你知道,我是 Android Studio 的新手,只是为了把它放在透视图中,这是我第一次在 Android Studio 中使用 Maven,但不是第一次使用 Maven。

【问题讨论】:

    标签: java android maven android-studio import


    【解决方案1】:

    把你的项目gradle改成这个

    //顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

    buildscript {
        repositories {
            jcenter()
            maven { url 'http://repo1.maven.org/maven2' }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.3.0'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    【讨论】:

      【解决方案2】:

      在您的项目build.grade 上尝试使用:

      allprojects {
      
          repositories {
              jcenter()
              mavenCentral()
          }
      }
      

      而不仅仅是repositories {...}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-08
        • 2016-01-19
        • 2016-06-09
        • 1970-01-01
        • 1970-01-01
        • 2015-04-14
        • 1970-01-01
        • 2018-10-26
        相关资源
        最近更新 更多