简体   繁体   中英

How to declare appcompat_v7 dependency in build.gradle within eclipse project setup?

I am using Eclipse with ADT and want to build my project with Gradle(2.3). The project structure is the standard generated android eclipse project structure. I am using the android support library appcompat_7. The support libraries are installed and everything I need from the Android SDK is up-to-date. Here is my (failing) attempt to my build.gradle file (positioned at the root):

buildscript {
repositories {
    mavenCentral()
}

dependencies {
    classpath 'com.android.tools.build:gradle:1.0.0'

    compile 'com.android.support:appcompat-v7:21.0.0'
}
}

apply plugin: 'android'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

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

    androidTest.setRoot('tests')
}
}

I get the following BUILD FAILURE from gradle:

C:\Workspaces\EclipseAndroid\training2>gradle build

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Workspaces\EclipseAndroid\training2\build.gradle' line: 9

* What went wrong:
A problem occurred evaluating root project 'training2'.
> Could not find method compile() for arguments [com.android.support:appcompat-v7:21.0.0] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@3e7545e8.

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

BUILD FAILED

Total time: 5.663 secs

When I am leaving out the line about the appcompat dependency I get the following BUILD FAILURE:

:processDebugManifest UP-TO-DATE
:processDebugResources
C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\debug\values\values.xml:9: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.

C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\debug\values-v11\values.xml:5: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.

C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\debug\values-v14\values.xml:5: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.

:processDebugResources FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':processDebugResources'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Program Files (x86)\Android\android-sdk\build-tools\22.0.1\aapt.exe package -f --no-crunch -I C:\Program Files (x86)\Android\android-sdk\platforms\android-22\android.jar -M C:\Workspaces\EclipseAndroid\training2\build\intermediates\manifests\full\debug\AndroidManifest.xml -S C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\debug -A C:\Workspaces\EclipseAndroid\training2\build\intermediates\assets\debug -m -J C:\Workspaces\EclipseAndroid\training2\build\generated\source\r\debug -F C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\resources-debug.ap_ --debug-mode --custom-package com.example.training2 -0 apk
Error Code:
1
Output:
C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\debug\values\values.xml:9: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.

C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\debug\values-v11\values.xml:5: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.

C:\Workspaces\EclipseAndroid\training2\build\intermediates\res\debug\values-v14\values.xml:5: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.



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

BUILD FAILED

Total time: 11.567 secs

How should I declare the dependencies in order to get the project build?

In my opinion, this is a pretty basic question when you want to build an eclipse-android project with gradle, however I could not find a solution during my websearch, I hope you can help :)

The main issue is that you aren't clearly separating the buildscript definition (ie, where to find the correct plugin) vs the android specific definitions (which apply after the apply plugin line).

Therefore, the buildscript section cannot include dependencies on Android libraries (it is only for declaring where to find the plugin):

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.3' // <-- updated
    }
}

Next, the correct plugin for an Android application is com.android.application :

apply plugin: 'com.android.application'

Then there's three components to add after that: repositories (where to find Android libraries), dependencies (what Android dependencies you have), and android (where you configure Android build specifics):

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.android.support:appcompat-v7:22.0.0' // <-- updated
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

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

        androidTest.setRoot('tests')
    }
}

Put those three sections together and you'll have a complete build.gradle file that will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM