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

时间:2022-12-10 20:55:18

I recently installed the latest Canary build of Android Studio which is currently using the Android Gradle plugin 3.0.0-alpha4 .

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

I now get a error:

我现在得到一个错误:

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

I has read: 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' 
}

I changed:

我改变了:

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

to:

:

implementation project(':MyLib')

but i still have this error: Error:Failed to resolve: Could not resolve project :MyLib.

但是我仍然有这个错误:error: Failed to resolve: Could not resolve project:MyLib。

lib gradle:

*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)
        }
    }
}

app gradle:

应用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'

Please help

请帮助

6 个解决方案

#1


44  

Google added more instruction how to solve it: Resolve build errors related to dependency matching

谷歌增加了更多如何解决它的指令:解决与依赖项匹配相关的构建错误

Cause of build error:

构建错误原因:

Your app includes a build type that a library dependency does not.

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

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

例如,您的应用程序包含一个“staging”构建类型,但是依赖项只包含一个“debug”和“release”构建类型。

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.

请注意,当一个库依赖项包含一个您的应用程序没有的构建类型时,没有问题。这是因为插件根本没有从依赖项请求构建类型。

Resolution

决议

Use matchingFallbacks to specify alternative matches for a given build type, as shown below:

使用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']
        }
    }
}

#2


37  

After facing the same issue, I finally declared exactly the same buildTypes in both App and Modules' build.gradle files.

在遇到同样的问题后,我最终在App和模块的构建中声明了完全相同的构建类型。gradle文件。

In your case, adding

在你的情况下,添加

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

to your module's build.gradle should do the trick.

你的模块的构建。gradle应该能做到这一点。

Be sure to change any "compile project" to "implementation project" too.

一定要将“编译项目”改为“实现项目”。

Hope it helps

希望它能帮助

#3


24  

With the new plugin, the variant-aware dependency resolution

有了新插件,可以识别变量的依赖关系解决方案

implementation project(':MyLib')

needs to have exact matching build types. 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.

例如,不可能通过这种机制让“调试”变量使用“发布”变量,因为生产者和消费者不匹配。(在本例中,名称“debug”是指在发布依赖项部分中提到的已发布的配置对象。)现在我们发布了两种配置,一种用于编译,另一种用于运行时,这种选择一种配置的老方法已经不起作用了。

So the old method of

所以旧的方法

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

will not work anymore.

将不再工作。

Example

With your example this would look like this:

以你的例子来说,这是这样的:

In app build.gradle:

在应用build.gradle:

apply plugin: 'com.android.application'

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

In module/lib 'MyLib' build.gradle:

在模块/ lib MyLib build.gradle:

apply plugin: 'com.android.library'

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

Therefore the build type must exactly match, no more no less.

因此,构建类型必须完全匹配。

Using Build-Type Fallbacks

A new feature called "matchingFallbacks" can be used to define default buildtypes if a sub-module does not define the buildtype.

如果子模块不定义构建类型,可以使用一个名为“matchingFallbacks”的新特性来定义默认的构建类型。

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

使用matchingFallbacks指定给定构建类型(…)的替代匹配。

For example if module/lib 'MyLib' gradle would look like this:

例如,如果模块/lib 'MyLib' gradle看起来是这样的:

apply plugin: 'com.android.library'

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

You could define the following in your app build.gradle:

您可以在应用程序构建中定义以下内容:

apply plugin: 'com.android.application'

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

Missing Flavor Dimensions

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

在defaultConfig块中使用missingdimensions策略来指定插件应该从每个缺失的维度中选择的默认风格

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

#4


0  

Today I also had the same problem after migrating to Android Studio 3. The problem is the gradle is not able to resolve the certain libraries due to network issue. The reasons might be various. If you work behind the proxy you need to add the proxy parameters in gradle.properties file:

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

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

In my case I had one more issue. My company uses the self signed SSL certificate so the SSL connection had some problem. If same applies also for you, you can set the parameter again in gradle.properties file as follows:

我还有一个问题。我的公司使用自签名的SSL证书,因此SSL连接有一些问题。如果同样适用于您,您可以在第二级中再次设置参数。属性文件如下:

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

To be more clear you can click on "Show details" link in messages log in Android Studio. This log will be more helpful to decide what is the real problem.

更清楚的是,你可以点击“显示详细信息”链接,在Android Studio中登录。这个日志将更有助于确定真正的问题是什么。

#5


0  

This solution worked for me. I'm using Android Studio 3.1.2. Android Gradle plugin 3.1.2. Gradle 4.4. I have a library module with flavours such as trial and premium. As part of the process of migrating to the Android Gradle plugin 3.1.2 I added a flavour dimension of main to my library module's gradle build file. To correct the build error therefore in my app's build.gradle file I changed the following:

这个办法对我起作用了。我使用的是Android Studio 3.1.2。3.1.2 Android Gradle插件。Gradle 4.4。我有一个图书馆模块的口味,如试用和高级。作为迁移到Android Gradle plugin 3.1.2的过程的一部分,我在我的库模块的Gradle构建文件中添加了一个主要的味道维度。为了纠正我的应用程序的构建错误。我更改了以下文件:

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

became

成为

implementation project(':library')

and I added the following line to my defaultConfig block: missingDimensionStrategy 'main', 'premium'

我在我的defaultConfig块中添加了如下一行:missingDimensionStrategy 'main', 'premium'

#6


-1  

I was facing the same problem, I found this migration page: Build matching types

我遇到了同样的问题,我发现了这个迁移页面:构建匹配类型

It states:

州:

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:

如果使用者配置了生产者没有配置的构建类型,则需要手动将使用者的构建类型与生产者的构建类型匹配。例如,如果您的应用程序模块配置“staging”构建类型及其库模块依赖项“mylibrary”不配置,那么Android插件会抛出以下构建错误:

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:

要解决这个错误,您需要指定来自“mylibrary”的构建类型,Android插件应该与应用程序的“staging”构建类型匹配。您可以通过应用程序构建中的buildTypeMatching属性来实现这一点。等级文件,如下图所示:

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

Adding buildTypeMatching fixed it for me without creating unecessary types in my library

添加build - typematching为我修复了它,而没有在我的库中创建不必要的类型

#1


44  

Google added more instruction how to solve it: Resolve build errors related to dependency matching

谷歌增加了更多如何解决它的指令:解决与依赖项匹配相关的构建错误

Cause of build error:

构建错误原因:

Your app includes a build type that a library dependency does not.

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

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

例如,您的应用程序包含一个“staging”构建类型,但是依赖项只包含一个“debug”和“release”构建类型。

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.

请注意,当一个库依赖项包含一个您的应用程序没有的构建类型时,没有问题。这是因为插件根本没有从依赖项请求构建类型。

Resolution

决议

Use matchingFallbacks to specify alternative matches for a given build type, as shown below:

使用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']
        }
    }
}

#2


37  

After facing the same issue, I finally declared exactly the same buildTypes in both App and Modules' build.gradle files.

在遇到同样的问题后,我最终在App和模块的构建中声明了完全相同的构建类型。gradle文件。

In your case, adding

在你的情况下,添加

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

to your module's build.gradle should do the trick.

你的模块的构建。gradle应该能做到这一点。

Be sure to change any "compile project" to "implementation project" too.

一定要将“编译项目”改为“实现项目”。

Hope it helps

希望它能帮助

#3


24  

With the new plugin, the variant-aware dependency resolution

有了新插件,可以识别变量的依赖关系解决方案

implementation project(':MyLib')

needs to have exact matching build types. 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.

例如,不可能通过这种机制让“调试”变量使用“发布”变量,因为生产者和消费者不匹配。(在本例中,名称“debug”是指在发布依赖项部分中提到的已发布的配置对象。)现在我们发布了两种配置,一种用于编译,另一种用于运行时,这种选择一种配置的老方法已经不起作用了。

So the old method of

所以旧的方法

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

will not work anymore.

将不再工作。

Example

With your example this would look like this:

以你的例子来说,这是这样的:

In app build.gradle:

在应用build.gradle:

apply plugin: 'com.android.application'

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

In module/lib 'MyLib' build.gradle:

在模块/ lib MyLib build.gradle:

apply plugin: 'com.android.library'

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

Therefore the build type must exactly match, no more no less.

因此,构建类型必须完全匹配。

Using Build-Type Fallbacks

A new feature called "matchingFallbacks" can be used to define default buildtypes if a sub-module does not define the buildtype.

如果子模块不定义构建类型,可以使用一个名为“matchingFallbacks”的新特性来定义默认的构建类型。

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

使用matchingFallbacks指定给定构建类型(…)的替代匹配。

For example if module/lib 'MyLib' gradle would look like this:

例如,如果模块/lib 'MyLib' gradle看起来是这样的:

apply plugin: 'com.android.library'

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

You could define the following in your app build.gradle:

您可以在应用程序构建中定义以下内容:

apply plugin: 'com.android.application'

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

Missing Flavor Dimensions

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

在defaultConfig块中使用missingdimensions策略来指定插件应该从每个缺失的维度中选择的默认风格

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

#4


0  

Today I also had the same problem after migrating to Android Studio 3. The problem is the gradle is not able to resolve the certain libraries due to network issue. The reasons might be various. If you work behind the proxy you need to add the proxy parameters in gradle.properties file:

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

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

In my case I had one more issue. My company uses the self signed SSL certificate so the SSL connection had some problem. If same applies also for you, you can set the parameter again in gradle.properties file as follows:

我还有一个问题。我的公司使用自签名的SSL证书,因此SSL连接有一些问题。如果同样适用于您,您可以在第二级中再次设置参数。属性文件如下:

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

To be more clear you can click on "Show details" link in messages log in Android Studio. This log will be more helpful to decide what is the real problem.

更清楚的是,你可以点击“显示详细信息”链接,在Android Studio中登录。这个日志将更有助于确定真正的问题是什么。

#5


0  

This solution worked for me. I'm using Android Studio 3.1.2. Android Gradle plugin 3.1.2. Gradle 4.4. I have a library module with flavours such as trial and premium. As part of the process of migrating to the Android Gradle plugin 3.1.2 I added a flavour dimension of main to my library module's gradle build file. To correct the build error therefore in my app's build.gradle file I changed the following:

这个办法对我起作用了。我使用的是Android Studio 3.1.2。3.1.2 Android Gradle插件。Gradle 4.4。我有一个图书馆模块的口味,如试用和高级。作为迁移到Android Gradle plugin 3.1.2的过程的一部分,我在我的库模块的Gradle构建文件中添加了一个主要的味道维度。为了纠正我的应用程序的构建错误。我更改了以下文件:

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

became

成为

implementation project(':library')

and I added the following line to my defaultConfig block: missingDimensionStrategy 'main', 'premium'

我在我的defaultConfig块中添加了如下一行:missingDimensionStrategy 'main', 'premium'

#6


-1  

I was facing the same problem, I found this migration page: Build matching types

我遇到了同样的问题,我发现了这个迁移页面:构建匹配类型

It states:

州:

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:

如果使用者配置了生产者没有配置的构建类型,则需要手动将使用者的构建类型与生产者的构建类型匹配。例如,如果您的应用程序模块配置“staging”构建类型及其库模块依赖项“mylibrary”不配置,那么Android插件会抛出以下构建错误:

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:

要解决这个错误,您需要指定来自“mylibrary”的构建类型,Android插件应该与应用程序的“staging”构建类型匹配。您可以通过应用程序构建中的buildTypeMatching属性来实现这一点。等级文件,如下图所示:

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

Adding buildTypeMatching fixed it for me without creating unecessary types in my library

添加build - typematching为我修复了它,而没有在我的库中创建不必要的类型