我正在将 Eclipse 与 ADT 结合使用,并希望使用 Gradle(2.3) 构建我的项目。项目结构是标准生成的 android eclipse 项目结构。 我正在使用 android 支持库 appcompat_7。 支持库已安装,Android SDK 所需的一切都是最新的。 这是我对 build.gradle 文件(位于根目录)的(失败)尝试:
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')
}
}
我从 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
当我省略关于 appcompat 依赖项的行时,我得到以下构建失败:
: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
我应该如何声明依赖项才能构建项目?
在我看来,当您想使用 gradle 构建 eclipse-android 项目时,这是一个非常基本的问题,但是我在网络搜索中找不到解决方案,希望您能提供帮助:)
最佳答案
主要问题是您没有明确区分 buildscript
定义(即在哪里可以找到正确的插件)与 android 特定定义(在 apply plugin< 之后应用
行)。
因此,buildscript
部分不能包含对 Android 库的依赖(它仅用于声明在哪里可以找到插件):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3' // <-- updated
}
}
接下来,Android 应用程序的正确插件是 com.android.application
:
apply plugin: 'com.android.application'
然后要添加三个组件:repositories
(在哪里可以找到 Android 库)、dependencies
(你有什么 Android 依赖项)和 android
(在其中配置 Android 构建细节):
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')
}
}
将这三个部分放在一起,您将拥有一个完整的 build.gradle
文件。
关于java - 如何使用 eclipse 项目设置在 build.gradle 中声明 appcompat v7 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29734217/