为Gradle构建的Android包含另一个依赖项目时,出现错误“无法解析项目”

时间:2019-04-17 21:18:08

标签: android gradle

我似乎无法让Gradle识别Android项目的依赖项。我设法将其减少到 裸机最低限额 ,但仍然出现错误。

如何在另一个项目中包含简单的库依赖项?

这是文件夹结构,非常简单:

 - build.gradle
 - settings.gradle
 + myproject
   - build.gradle
 + mylibrary
   - build.gradle

错误是“无法解析项目” /“无法找到匹配的项目变体”

command: gradle build

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':mylibrary:preReleaseBuild'.
> Could not resolve all task dependencies for configuration ':mylibrary:releaseRuntimeClasspath'.
> Could not resolve project :mylibrary.
    Required by:
        project :mylibrary
    > Unable to find a matching variant of project :mylibrary:
        - Variant 'debugApiElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' and found incompatible value 'debug'.
            - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
            - Required org.gradle.usage 'java-runtime' and found incompatible value 'java-api'.
        - Variant 'debugBundleElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' but no value provided.
            - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but no value provided.
            - Required org.gradle.usage 'java-runtime' and found incompatible value 'android-bundle'.
        - Variant 'debugMetadataElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' and found incompatible value 'debug'.
            - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Metadata'.
            - Required org.gradle.usage 'java-runtime' but no value provided.
        - Variant 'debugRuntimeElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' and found incompatible value 'debug'.
            - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
            - Required org.gradle.usage 'java-runtime' and found compatible value 'java-runtime'.
        - Variant 'releaseApiElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' and found compatible value 'release'.
            - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
            - Required org.gradle.usage 'java-runtime' and found incompatible value 'java-api'.
        - Variant 'releaseBundleElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' but no value provided.
            - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but no value provided.
            - Required org.gradle.usage 'java-runtime' and found incompatible value 'android-bundle'.
        - Variant 'releaseMetadataElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' and found compatible value 'release'.
            - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Metadata'.
            - Required org.gradle.usage 'java-runtime' but no value provided.
        - Variant 'releaseRuntimeElements' capability gradle-test2:mylibrary:unspecified:
            - Required com.android.build.api.attributes.BuildTypeAttr 'release' and found compatible value 'release'.
            - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
            - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
            - Required org.gradle.usage 'java-runtime' and found compatible value 'java-runtime'.

我的文件内容如下:

./ build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

ext {
    compileSdkVersion = 28
    buildToolsVersion = '28.0.3'
    supportLibVersion = '28.0.0'
    applicationId = 'com.example'
    minSdkVersion = 16
    targetSdkVersion = 26
    versionCode = 1
    versionName = '1.0'
}

./ settings.gradle

include ':myproject', ':mylibrary'

./ myproject / build.gradle

apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    implementation project(':mylibrary')
}

android {

    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId rootProject.ext.applicationId
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
    }

    buildTypes {
        release {}
        debug {}
    }
}

./ mylibrary / build.gradle

apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
}

android {

    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId rootProject.ext.applicationId + ".mylibrary"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
    }

    buildTypes {
        release {}
        debug {}
    }
}

1 个答案:

答案 0 :(得分:1)

对于Android库,应用的插件应为com.android.library而不是android,请尝试替换

apply plugin: 'android'

在您图书馆的build.gradle

apply plugin: 'com.android.library'