Gradle error: configuration declares dependency which is not declared

Gradle error: configuration declares dependency which is not declared

我正在制作我的第一个 android Wear 应用程序,但我无法让 Android Studio 正常工作。 首先我得到错误

 "Project with path ':wear' could not be found in project ':mobile'. 

已通过在 settings.gradle 中添加 "include ':wear" 解决此问题。
但是随后出现了一个新的错误:

"Error:Module version Test2:mobile:unspecified, configuration 'wearApp' declares a dependency on configuration 'default' which is not declared in the module descriptor for Test2:wear:unspecified" .

我该怎么做才能解决该错误?

以防万一:这里是 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.verbraeken.joost.test2"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    wearApp project(':wear')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.google.android.gms:play-services:8.3.0'
    compile 'com.android.support:design:23.1.1'
}

settings.gradle:

include ':mobile'
include ':wear'

Error:Module version Test2:mobile:unspecified, configuration 'wearApp' declares a dependency on configuration 'default'

这意味着模块(在您的情况下为 wearApp)没有 build.gradle 文件或 build.gradle 文件中的正确配置。

由于您在 settings.gradle 中定义了一个模块,因此您必须为每个模块提供一个 build.gradle

你的情况:

root
|-- mobile
|----build.gradle
|-- wear
|----build.gradle
|--build.gradle
|--settings.gradle

在 Android Studio 3.0 中 Migrate to the New Plugin 的文档说:

dependencies {
    // This is the old method and no longer works for local
    // library modules:
    // debugCompile project(path: ':foo', configuration: 'debug')
    // releaseCompile project(path: ':foo', configuration: 'release')

    // Instead, simply use the following to take advantage of
    // variant-aware dependency resolution. You can learn more about
    // the 'implementation' configuration in the section about
    // new dependency configurations.
    implementation project(':foo')

    // You can, however, keep using variant-specific configurations when
    // targeting external dependencies. The following line adds 'app-magic'
    // as a dependency to only the 'debug' version of your module.

    debugImplementation 'com.example.android:app-magic:12.3'
}

所以改变这个

    debugCompile project(path: ':foo', configuration: 'debug')
    releaseCompile project(path: ':foo', configuration: 'release')

至此

    implementation project(':foo')

如果您不使用 Android Studio 3.0,这对我有用,在您的 build.gradle 库中:

publishNonDefault true

像这样

android {
    compileSdkVersion maxApiLevel.toInteger()
    buildToolsVersion androidBuildToolsVersion
    publishNonDefault true

    [...]
}

并在您的 build.gradle 中包含:

dependencies {
    debugCompile project(path: ':foo', configuration: 'debug')
    releaseCompile project(path: ':foo', configuration: 'release')
}

我正在使用 ionic cordova 构建我的应用程序,在我的例子中文件 build.grade 每次都更新,我必须将文件 "app_path>node_modules\cordova-android\bin\templates\cordova\lib\builders\GradleBuilder.js" 更改为:

    console.log('Subproject Path: ' + p);
    var libName=p.replace(/[/\]/g, ':').replace(name+'-','');
    depsList += '    debugCompile(project(path: "' + libName + '", configuration: "debug"))';
    insertExclude(p);
    depsList += '    releaseCompile(project(path: "' + libName + '", configuration: "release"))';
    insertExclude(p);

至:

    console.log('Subproject Path: ' + p);
    var libName=p.replace(/[/\]/g, ':').replace(name+'-','');
    depsList += '    compile project(\'' + libName + '\')';
    insertExclude(p);

适合我

诀窍是:

dependencies {
    // If the main app and wearable modules have the same flavors,
    // the following configuration uses automatic dependency matching.
    wearApp  project(':wearable')
}

您现在没有设置 flavor 或 type build,gradle 3.0 及更高版本搜索每个 flavor 和 buildType。 更多信息:https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration#variant_dependencies