Android Studio 3.0 错误。迁移本地模块的依赖配置

Android Studio 3.0 Error. Migrate dependency configurations for local modules

我最近安装了 Android Studio 的最新 Canary 版本,目前正在使用 Android Gradle 插件 3.0.0-alpha4。

我现在得到一个错误:

Error:Failed to resolve: Could not resolve project :MyLib.
Required by:
project :app

我已阅读:Migrate dependency configurations for local modules

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' 
}

我改变了:

releaseCompile project(path: ':MyLib', configuration: 'appReleaseApp')
debugCompile project(path: ':MyLib', configuration: 'appDebug')

至:

implementation project(':MyLib')

但我仍然有这个错误:Error:Failed to resolve: Could not resolve project :MyLib.

库gradle:

apply plugin: 'com.android.library'

android {
    publishNonDefault true
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 25
    }
    buildTypes {
        debug {
            ...
        }
        releaseApp {
            ...
        }
        releaseSdk {
            ...'
        }
    }
    flavorDimensions "default"

    productFlavors {
        flavor1{
            ...
        flavor2{
            ...
        }
        flavor3{
            ...
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.google.android.gms:play-services-maps:10.2.6'
    compile 'com.google.android.gms:play-services-gcm:10.2.6'
    compile 'com.google.android.gms:play-services-location:10.2.6'
}

apply plugin: 'maven'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: mavenLocal().url)
        }
    }
}

应用gradle:

apply plugin: 'com.android.application'

android {

    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 12
        versionName "5.0.2"
    }

    buildTypes {
        release {
            ...
        }
        debug {
            ...
        }
    }
    flavorDimensions "default"

    productFlavors {
        flavor1 {
            ...
        }

        flavor2 {
            ...
        }
    }

    testOptions {
        unitTests {
            all {
                jvmArgs '-noverify'
                systemProperty 'robolectric.logging.enable', true
            }
        }
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies {
    //    releaseCompile project(path: ':MyLib', configuration: 'appRelease')
    //    debugCompile project(path: ':MyLib', configuration: 'appDebug')
    implementation project(':MyLib')

    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.google.android.gms:play-services-maps:10.2.6'
    compile 'com.google.android.gms:play-services-location:10.2.6'
    compile 'com.google.android.gms:play-services-analytics:10.2.6'
    compile 'com.google.android.gms:play-services-gcm:10.2.6'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:gridlayout-v7:25.3.1'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.facebook.stetho:stetho:1.4.1'
    compile 'com.facebook.stetho:stetho-okhttp3:1.4.1'
    compile 'com.android.support:percent:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:2.1.0'
    testCompile 'org.robolectric:robolectric:3.1.4'
    testCompile 'org.assertj:assertj-core:1.7.1'

    compile 'com.flipboard:bottomsheet-core:1.5.0'
    compile 'com.flipboard:bottomsheet-commons:1.5.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
}

apply plugin: 'com.google.gms.google-services'

请帮忙

遇到同样的问题后,我终于在 App 和 Modules 的 build.gradle 文件中声明了完全相同的 buildTypes。

对于你的情况,添加

buildTypes {
    debug {}
    releaseApp {}
    releaseSdk {}
}

您模块的 build.gradle 应该可以解决问题。

请务必将任何 "compile project" 也更改为 "implementation project"。

希望对您有所帮助

有了新插件,变体感知依赖解析

implementation project(':MyLib')

需要具有完全匹配的构建类型。 The migration guide describes this

For instance, it is not possible to make a 'debug' variant consume a 'release' variant through this mechanism because the producer and consumer would not match. (In this case, the name 'debug' refers to the published configuration object mentioned above in the Publishing Dependencies section.) Now that we publish two configurations, one for compiling and one for runtime, this old way of selecting one configuration really doesn't work anymore.

所以

的旧方法
releaseCompile project(path: ':foo', configuration: 'debug')

将不再有效。

例子

以您的示例为例:

在应用中 build.gradle:

apply plugin: 'com.android.application'

android {
  buildTypes {
    debug {}
    releaseApp {}
    releaseSdk {}
  }
  ...
  dependencies {
    implementation project(':MyLib')
  }
}

在module/lib 'MyLib' build.gradle:

apply plugin: 'com.android.library'

android {
  buildTypes {
    debug {}
    releaseApp {}
    releaseSdk {}
  }
}

因此构建类型必须完全匹配,不多也不少。

使用构建类型回退

如果子模块未定义构建类型,则可以使用称为 "matchingFallbacks" 的新功能来定义默认构建类型。

Use matchingFallbacks to specify alternative matches for a given build type (...)

例如,如果 module/lib 'MyLib' gradle 将如下所示:

apply plugin: 'com.android.library'

android {
  buildTypes {
    debug {}
    releaseLib {}
  }
}

您可以在您的应用程序中定义以下内容 build.gradle:

apply plugin: 'com.android.application'

android {
  buildTypes {
    debug {}
    releaseApp {
        ...
        matchingFallbacks = ['releaseLib']
    }
    releaseSdk {
        ...
        matchingFallbacks = ['releaseLib']
    }
  }
  ...
  dependencies {
    implementation project(':MyLib')
  }
}

缺少风味维度

Use missingDimensionStrategy in the defaultConfig block to specify the default flavor the plugin should select from each missing dimension

android {
    defaultConfig {
        missingDimensionStrategy 'minApi', 'minApi18', 'minApi23'
        ...
    }
}

我遇到了同样的问题,我找到了这个迁移页面: Build matching types

它指出:

Select defaults for missing build types
If a consumer configures a build type that a producer does not, you need to manually match the consumer's build type to one from the producer. For example, if your app module configures a "staging" build type and its library module dependency, "mylibrary", does not, the Android plugin throws the following build error:

Error:Failed to resolve: Could not resolve project :mylibrary.
Required by: project :app

To resolve this error, you need to specify which build type from "mylibrary" the Android plugin should match to the app's "staging" build type. You can do this with the buildTypeMatching property in the app's build.gradle file, as shown below:

// Add the following to the consumer's build.gradle file.
android {
    ...
    // Tells the Android plugin to use a library's 'debug' build type
    // when a 'staging' build type is not available. You can include
    // additional build types, and the plugin matches 'staging' to the
    // first build type it finds from the one's you specify. That is,
    // if 'mylibrary' doesn't include a 'debug' build type either, the
    // plugin matches 'staging' with the producer's 'release' build type.
    buildTypeMatching 'staging', 'debug', 'release'
}

添加 buildTypeMatching 为我修复了它,而无需在我的库中创建不必要的类型

Google 添加了更多解决方法的说明:Resolve build errors related to dependency matching

构建错误原因:

您的应用包含库依赖项不包含的构建类型。

For example, your app includes a "staging" build type, but a dependency includes only a "debug" and "release" build type.

Note that there is no issue when a library dependency includes a build type that your app does not. That's because the plugin simply never requests that build type from the dependency.

分辨率

使用 matchingFallbacks 为给定的构建类型指定替代匹配项,如下所示:

// In the app's build.gradle file.
android {
    buildTypes {
        debug {}
        release {}
        staging {
            // Specifies a sorted list of fallback build types that the
            // plugin should try to use when a dependency does not include a
            // "staging" build type. You may specify as many fallbacks as you
            // like, and the plugin selects the first build type that's
            // available in the dependency.
            matchingFallbacks = ['debug', 'qa', 'release']
        }
    }
}

今天我在迁移到 Android Studio 3 后也遇到了同样的问题。 问题是 gradle 由于网络问题无法解析某些库。原因可能多种多样。 如果您在代理后面工作,您需要在 gradle.properties 文件中添加代理参数:

systemProp.http.proxyHost=<proxy_host>
systemProp.http.proxyPort=<proxy_port
systemProp.https.proxyHost=<proxy_host>
systemProp.https.proxyPort=<proxy_port>

就我而言,我还有一个问题。我的公司使用自签名 SSL 证书,因此 SSL 连接出现了一些问题。如果同样适用于您,您可以在 gradle.properties 文件中再次设置参数,如下所示:

org.gradle.jvmargs=-Djavax.net.ssl.trustStore="/usr/lib/jvm/java-8-oracle/jre/lib/security/cacerts" -Djavax.net.ssl.trustStoreType=JKS -Djavax.net.ssl.keyStorePassword=changeit

为了更清楚,您可以点击 "Show details" link 消息登录 Android Studio。此日志将有助于确定真正的问题是什么。

这个解决方案对我有用。我正在使用 Android Studio 3.1.2。 Android Gradle 插件 3.1.2。 Gradle 4.4。我有一个带有 trialpremium 等风格的库模块。作为迁移到 Android Gradle 插件 3.1.2 的过程的一部分,我在我的库模块的 gradle 构建文件中添加了 main 的风格维度。因此,为了更正我应用的 build.gradle 文件中的构建错误,我更改了以下内容:

debugImplementation project(path: ':library', configuration: 'premiumDebug')
releaseImplementation project(path: ':library', configuration: 'premiumRelease')

成为

implementation project(':library')

并且我在 defaultConfig 块中添加了以下行:missingDimensionStrategy 'main', 'premium'