首页 文章

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

提问于
浏览
49

我最近安装了最新的Canary版本的Android Studio,目前正在使用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

依赖

{

//这是旧方法,不再适用于本地方法
//库模块:
// debugCompile项目(路径:':foo',配置:'debug')
// releaseCompile项目(路径:':foo',配置:'发布')

//相反,只需使用以下内容即可充分利用
//变体感知依赖项解析 . 您可以了解更多信息
//关于的部分中的'implementation'配置
//新的依赖配置 .
实施项目(':foo')

//但是,您可以继续使用特定于变体的配置
//定位外部依赖项 . 以下行添加'app-magic'
//仅作为模块的'debug'版本的依赖项 .

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.

lib 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:

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'

请帮忙

6 回答

  • 0

    今天我在迁移到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
    

    为了更清楚,您可以单击Android Studio中消息日志中的“显示详细信息”链接 . 此日志将更有助于确定什么是真正的问题 .

  • 37

    使用新插件,变体感知依赖项解析

    implementation project(':MyLib')
    

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

    例如,不可能通过此机制使'debug'变体消耗'release'变体,因为 生产环境 者和消费者不匹配 . (在这种情况下,名称'debug'指的是上面在Publishing Dependencies部分中提到的已发布配置对象 . )现在我们发布了两个配置,一个用于编译,一个用于运行时,这种选择一个配置的旧方法确实没有再工作了 .

    所以旧的方法

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

    将不再工作 .

    示例

    在您的示例中,这将如下所示:

    在app 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”的新功能来定义默认构建类型 .

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

    例如,如果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')
      }
    }
    

    缺少风味尺寸

    在defaultConfig块中使用missingDimensionStrategy指定插件应从每个缺少的维度中选择的默认flavor

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

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

    它指出:

    选择缺少的构建类型的默认值如果使用者配置 生产环境 者没有的构建类型,则需要手动将使用者的构建类型与 生产环境 者的构建类型匹配 . 例如,如果您的app模块配置“staging”构建类型及其库模块依赖项“mylibrary”,则不会,Android插件会抛出以下构建错误:

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

    要解决此错误,您需要指定Android插件应与“应用程序”的“登台”构建类型匹配的“mylibrary”中的构建类型 . 您可以使用app的build.gradle文件中的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'
    }
    

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

  • 0

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

    在你的情况下,添加

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

    你的模块的build.gradle应该做的伎俩 .

    一定要将任何“编译项目”更改为“实施项目” .

    希望能帮助到你

  • 28

    这个解决方案对我有用 . 我正在使用Android Studio 3.1.2 . Android Gradle插件3.1.2 . Gradle 4.4 . 我有一个库模块,有 trialpremium 等风味 . 作为迁移到Android Gradle插件3.1.2的过程的一部分,我将 main 的味道维度添加到我的库模块's gradle build file. To correct the build error therefore in my app' s build.gradle 文件中我更改了以下内容:

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

    成为

    implementation project(':library')
    

    我将以下行添加到我的 defaultConfig 块: missingDimensionStrategy 'main', 'premium'

  • -1

    谷歌增加了更多说明如何解决它:Resolve build errors related to dependency matching

    构建错误的原因:

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

    例如,您的应用包含一个“staging”构建类型,但依赖项仅包括“debug”和“release”构建类型 . 请注意,当库依赖项包含您的应用程序不包含的构建类型时,没有问题 . 那是因为插件根本不会从依赖项中请求构建类型 .

    Resolution

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

相关问题