13

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

我真的尝试了很多在这里发布的解决方案(在设置中将模块定义为模块夏洛克示例设置正确的源集......),但我总是得到同样的错误(只要我将 lib debendency 添加到我的应用程序 build.gradle 文件):

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

我已经从命令行进行了清理和构建,但这没有效果。这是否可能只是 Android Studio 早期版本中的一个错误(如本文中的评论)?还是我错过了什么?

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

请帮助我,我是 gradle 新手,我以前从未使用过 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

设置.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:

设置.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'
}
4

2 回答 2

19

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

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

当您使用“:”开始项目路径时,您正在从根项目创建一个绝对路径,然后基本上只是使用“:”而不是“/”编写路径。在这种情况下,您的 projectLib 模块位于 MyProject/MyApp/libraries/projectLib 目录中,而 MyProject 是您的 settings.gradle 所在的位置,使其成为您的根项目。因此,将冒号换成斜杠可以得到我上面写的那一行。

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

include 'MyApp:libraries:projectLib'

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

allprojects {
    repositories {
        mavenCentral()
    }
}

多模块文档中涵盖了许多不错的小技巧。

于 2013-06-23T20:59:57.913 回答
4

After long researches, several settings and the help of Josh, I solved my problem that is describes in the question above.

Now here my solution:

  • set gradle version in your build.gradle files to 0.4.2
  • use for support-library (e.g.: v4) "com.android.support:support-v4:13.0.0" (like its described here)
  • in my case I had to install the "Android Support Repository" and "Google Repository" with the SDK Manager
  • with the last edited gradle files (Edit 3) in my question --> my build was successful
于 2013-06-24T08:43:24.097 回答