有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Gradle项目对AAR库的依赖在3.5中失败。x释放

我有一个gradle 安卓库模块,它依赖于另一个项目安卓库:

安卓lib-a depends on 安卓lib-b

安卓lib——一种依赖

implementation project(path: ':安卓lib-b', configuration: "default")

从3.3.0升级到安卓 build tools 3.5.0后,在配置阶段引发以下异常:

What went wrong: Could not determine the dependencies of task ':安卓lib-a:lint'. Could not resolve all artifacts for configuration ':安卓lib-a:releaseCompileClasspath'. Failed to transform artifact '安卓lib-b.aar (project :安卓lib-b)' to match attributes {artifactType=jar}. Execution failed for IdentityTransform: C:\LocalData\projects\project\安卓lib-b\build\outputs\aar\安卓lib-b-release.aar. C:\LocalData\projects\project\安卓lib-b\build\outputs\aar\安卓lib-b-release.aar.

我将安卓lib-a依赖项更改为:

implementation project(':安卓lib-b')

从命令行为安卓lib-a构建的版本现在可以运行了,但我在Android Studio Gradle Sync上遇到了以下问题:

ERROR: More than one variant of project :安卓lib-b matches the consumer attributes: - Configuration ':安卓lib-b:releaseRuntimeElements' variant 安卓-assets: - Unmatched attributes: - Found artifactType '安卓-assets' but wasn't required. - Found com.安卓.build.api.attributes.VariantAttr 'release' but wasn't required. - Compatible attributes: - Required com.安卓.build.api.attributes.BuildTypeAttr 'release' and found compatible value 'release'. - Required com.安卓.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'. - Required org.gradle.usage 'java-runtime' and found compatible value 'java-runtime'. - Configuration ':安卓lib-b:releaseRuntimeElements' variant 安卓-classes: - Unmatched attributes: - Found artifactType '安卓-classes' but wasn't required. - Found com.安卓.build.api.attributes.VariantAttr 'release' but wasn't required. - Compatible attributes: - Required com.安卓.build.api.attributes.BuildTypeAttr 'release' and found compatible value 'release'. - Required com.安卓.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'. - Required org.gradle.usage 'java-runtime' and found compatible value 'java-runtime'. - Configuration ':安卓lib-b:releaseRuntimeElements' variant 安卓-consumer-proguard-rules: ...

更改后,我在另一个依赖安卓lib-a的模块中也遇到了一个问题,并试图像这样复制依赖项:

task copyToLib() {
    afterEvaluate {
        dependsOn ":安卓lib-a:assembleRelease"
    }
    doLast {
        delete libDir
        def config = rootProject.project(":project").configurations.releaseRuntimeClasspath
        println config.toList()
        copy {
            into libDir
            from config
        }
    }
}

preBuild.dependsOn "copyToLib"

Execution failed for task ':anothermodule:copyToLib'. Could not resolve all files for configuration ':安卓lib-a:releaseRuntimeClasspath'. More than one variant of project :安卓lib-b matches the consumer attributes: - Configuration ':安卓lib-b:releaseRuntimeElements' variant 安卓-assets: - Unmatched attributes: - Found artifactType '安卓-assets' but wasn't required. - Found com.安卓.build.api.attributes.VariantAttr 'release' but wasn't required. - Compatible attributes: - Required com.安卓.build.api.attributes.BuildTypeAttr 'release' and found compatible value 'release'. - Required com.安卓.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'. - Required org.gradle.usage 'java-runtime' and found compatible value 'java-runtime'. - Configuration ':安卓lib-b:releaseRuntimeElements' variant 安卓-classes: - Unmatched attributes: - Found artifactType '安卓-classes' but wasn't required. - Found com.安卓.build.api.attributes.VariantAttr 'release' but wasn't required. - Compatible attributes: - Required com.安卓.build.api.attributes.BuildTypeAttr 'release' and found compatible value 'release'. - Required com.安卓.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'. - Required org.gradle.usage 'java-runtime' and found compatible value 'java-runtime'.

安卓 build tools 3.5.1版本也存在同样的问题

完整的安卓lib-a构建conifg:

apply plugin: 'com.安卓.library'

version = releaseVersion

安卓 {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName releaseVersion
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation project(path: ':安卓lib-b', configuration: "default")
}

完整的安卓lib-b构建conifg:

apply plugin: 'com.安卓.library'

version = releaseVersion

安卓 {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName releaseVersion
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    我不能说这是否是你的问题,但Android Studio 3.5默认启用R8收缩和缩小功能,取代了Proguard——我想说的是,即使在调试构建类型上,至少在某些部分上——也启用了它

    在这个版本中有一个issue与AARs有关。Android Studio找不到AAR中包含的JAR依赖项。这个问题应该会在安卓Studio 3.6.0-alpha05或更高版本的加那利频道上得到解决,可以在安卓Studio的网站上下载

    此外,如果AAR不包含任何JAR依赖项,当AAR库尝试使用某些第三方库时,在执行过程中仍然可能会出现NoClassDefFoundException或NoSuchMethodError。此问题尚未解决,但您可以应用以下解决方法:

    只需在你的渐变中添加下一行。根目录中的属性,同步项目并重新运行:

    android.enableD8.desugaring=false
    

    您还可以通过将AAR上传到任何respository来解决这两个问题,比如本地存储库或云存储库,比如JFrog或Nexus private server(如果您的库是私有的),或者甚至上传到Maven或类似的公共存储库(如果您的库是公共的),因为这些错误只会影响手动包含的AAR,而不会影响从存储库下载的AAR