Gradle can't find dependency (Android support library)

半腔热情 提交于 2020-01-13 07:46:17

问题


I have a problem that Gradle can't find my dependency (Android support library).

My build.gradle looks like this:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/FlurryAgent.jar')
    compile group: 'com.google.android', name: 'support-v4', version: 'r7'
    compile files('libs/YouTubeAndroidPlayerApi.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 17
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

When I build (on commandline, no IDE) I get the following message:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'AndroidCalculator'.
> Failed to notify project evaluation listener.
   > Could not resolve all dependencies for configuration ':compile'.
      > Could not find com.google.android:support-v4:r7.
        Required by:
            :AndroidCalculator:unspecified

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Why am I not allowed to add the Android Support library like this?


回答1:


You have declared a repository dependency, but haven't declared a repository. Hence the dependency cannot be resolved. (Repositories/dependencies in the buildscript block are strictly separated from repositories/dependencies in the main build script.)




回答2:


http://pastebin.com/FmcCZwA5

This paste is elaborate project with AndroidAnnotations, Dagger, Jackson and Robolectric.

all you need is add

repositories {
    mavenCentral()
}

replace

 compile group: 'com.google.android', name: 'support-v4', version: 'r7'

with (line 44 of the code linked above)

 compile 'com.android.support:support-v4:18.0.+'

Gotchas: Last bit will works on Android Studio 0.2+ only if you had a fresh install. Since 0.2 Studio is shipped with its internal m2 repo to provide support and google api libraries so if you upgraded from previous versions your SDK doesn't have it.

also make sure local.properties file is present in root folder and sdk.dir points to SDK




回答3:


You need to add additional dependency in dependencies tag. If you have android-support-v4.jar library in your libs folder, try to add code listed below:

dependencies {
    compile files('libs/android-support-v4.jar')
}


来源:https://stackoverflow.com/questions/16673579/gradle-cant-find-dependency-android-support-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!