build.gradle和项目库

时间:2013-06-21 11:50:56

标签: android gradle android-studio build.gradle

我在android studio中有一个使用项目库的项目。 我的问题是gradle文件的配置。

我真的尝试了很多在这里发布的解决方案(define modul in project lib in settings as modulsherlock examplesetting right sourcesets ...),但我总是得到同样的错误(我一添加我的应用程序build.gradle文件的lib debendency:

A problem occurred configuring project ':MyApp'.
> Failed to notify project evaluation listener.
   > Configuration with name 'default' not found.

我已经从命令行进行了清理和构建,但这没有任何效果。 这可能只是早期版本的Android Studio(like the comment in this post)中的一个错误吗?或者我会错过什么?

我目前的Android Studio版本:0.1.6和我的操作系统:Windows 7,64位

请帮助我,我是新手,我以前从未使用过maven或ant。

编辑1:

gardelw compileDebug --stacktrace

* Exception is:

org.gradle.api.ProjectConfigurationException: A problem occurred configuring pro
ject ':MyApp'.
        at org.gradle.configuration.LifecycleProjectEvaluator.addConfigurationFa
ilure(LifecycleProjectEvaluator.java:79)
        at org.gradle.configuration.LifecycleProjectEvaluator.notifyAfterEvaluat
e(LifecycleProjectEvaluator.java:74)
        at org.gradle.configuration.LifecycleProjectEvaluator.evaluate(Lifecycle
ProjectEvaluator.java:61)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProj
ect.java:465)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProj
ect.java:76)
        at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuil
dConfigurer.java:31)
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(Default
GradleLauncher.java:142)
        at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradle
Launcher.java:113)
        at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLaun
cher.java:81)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildCon
troller.run(InProcessBuildActionExecuter.java:64)
        at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.jav
a:33)
        at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.jav
a:24)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProce
ssBuildActionExecuter.java:35)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProce
ssBuildActionExecuter.java:26)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)
        at org.gradle.api.internal.Actions$RunnableActionAdapter.execute(Actions
.java:171)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.
execute(CommandLineActionFactory.java:201)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.
execute(CommandLineActionFactory.java:174)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
CommandLineActionFactory.java:170)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
CommandLineActionFactory.java:139)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionRep
ortingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionRep
ortingAction.java:22)
        at org.gradle.launcher.Main.doAction(Main.java:48)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.Main.main(Main.java:39)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBoots
trap.java:50)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.j
ava:32)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:26)
        at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.ja
va:33)
        at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:130)
        at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:48)
Caused by: org.gradle.listener.ListenerNotificationException: Failed to notify p
roject evaluation listener.
        at org.gradle.listener.BroadcastDispatch.dispatch(BroadcastDispatch.java
:90)
        at org.gradle.listener.BroadcastDispatch.dispatch(BroadcastDispatch.java
:30)
        at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocat
ionHandler.invoke(ProxyDispatchAdapter.java:93)
        at com.sun.proxy.$Proxy8.afterEvaluate(Unknown Source)
        at org.gradle.configuration.LifecycleProjectEvaluator.notifyAfterEvaluat
e(LifecycleProjectEvaluator.java:67)
        ... 29 more
Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration
 with name 'default' not found.
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration
Container.createNotFoundException(DefaultConfigurationContainer.java:94)
        at org.gradle.api.internal.DefaultNamedDomainObjectCollection.getByName(
DefaultNamedDomainObjectCollection.java:184)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration
Container.getByName(DefaultConfigurationContainer.java:84)
        at org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependen
cy.getProjectConfiguration(DefaultProjectDependency.java:72)
        at org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependen
cy_Decorated.getProjectConfiguration(Unknown Source)
        at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.getPropert
y(BeanDynamicObject.java:146)
        at org.gradle.api.internal.BeanDynamicObject.getProperty(BeanDynamicObje
ct.java:102)
        at org.gradle.api.internal.CompositeDynamicObject.getProperty(CompositeD
ynamicObject.java:78)
        at org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependen
cy_Decorated.getProperty(Unknown Source)
        at com.android.build.gradle.BasePlugin$_ensureConfigured_closure92.doCal
l(BasePlugin.groovy:1252)
        at com.android.build.gradle.BasePlugin.ensureConfigured(BasePlugin.groov
y:1250)
        at com.android.build.gradle.BasePlugin$ensureConfigured.callCurrent(Unkn
own Source)
        at com.android.build.gradle.BasePlugin.resolveDependencyForConfig(BasePl
ugin.groovy:1214)
        at com.android.build.gradle.BasePlugin$resolveDependencyForConfig.callCu
rrent(Unknown Source)
        at com.android.build.gradle.BasePlugin.resolveDependencies(BasePlugin.gr
oovy:1164)
        at com.android.build.gradle.BasePlugin$resolveDependencies.callCurrent(U
nknown Source)
        at com.android.build.gradle.AppPlugin.doCreateAndroidTasks(AppPlugin.gro
ovy:196)
        at com.android.build.gradle.BasePlugin.createAndroidTasks(BasePlugin.gro
ovy:242)
        at com.android.build.gradle.BasePlugin$_apply_closure1.doCall(BasePlugin
.groovy:190)
        at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(Cl
osureBackedMethodInvocationDispatch.java:40)
        at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(Cl
osureBackedMethodInvocationDispatch.java:25)
        at org.gradle.listener.BroadcastDispatch.dispatch(BroadcastDispatch.java
:81)
        ... 33 more


BUILD FAILED

Total time: 2.947 secs

编辑2:

项目结构:

MyProject
- MyApp
  -- build.gradle
  -- libraries
     --- projectLib
         --- build.gradle
- build.gradle
- settings

settings.gradle

include ':MyApp'
include ':libraries:projectLib'
// also tried to define projectLib as modul:
// project(':projectLib').projectDir = new File('MyApp/libraries/projectLib')

MyApp的build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'
dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(':libraries:projectLib') // adding this line 
                                             // --> build failed
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

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

projectLib的build.gradle:

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

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

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

编辑3:

settings.gradle

include ':MyApp'
include ':MyApp:libraries:projectLib'

MyApp的build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}
apply plugin: 'android'
dependencies {
    compile 'com.android.support:support-v4:13.0.0'
    compile project(':MyApp:libraries:projectLib')
}

projectLib的build.gradle:

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

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
}

2 个答案:

答案 0 :(得分:19)

我认为您正在错误地指定库项目的路径。如果我正确地解释您的项目布局,那么该行应该如下:

compile project(':MyApp:libraries:projectLib')

当您使用':'启动项目路径时,您将从根项目创建绝对路径,然后基本上只使用':'而不是'/'编写路径。在这种情况下,projectLib模块位于MyProject / MyApp / libraries / projectLib目录中,MyProject是settings.gradle所在的位置,使其成为您的根项目。所以用冒号替换斜线可以得到我上面写的那条线。

您需要修改settings.gradle以包含完整路径:

include 'MyApp:libraries:projectLib'

最后,如果您想为存储库配置等内容保存一些类型,可以将其放在根项目的allproject块中。

allprojects {
    repositories {
        mavenCentral()
    }
}

multi-module docs.

中提到的许多不错的小技巧

答案 1 :(得分:4)

经过长时间的研究,几个设置和Josh的帮助,我解决了上述问题中描述的问题。

现在我的解决方案:

  • 将build.gradle文件中的gradle版本设置为0.4.2
  • 用于支持库(例如:v4)“com.android.support:support-v4:13.0.0”(like its described here
  • 就我而言,我必须使用SDK Manager安装“Android Support Repository”和“Google Repository”
  • 在我的问题中使用最后编辑的gradle文件(编辑3 ) - >我的构建成功了