gpt4 book ai didi

Android gradle 多项目依赖解析

转载 作者:行者123 更新时间:2023-12-03 05:09:07 25 4
gpt4 key购买 nike

在我的 Android 项目中,我的任务是将构建依赖项复制到外部目录,如下所示:

task copyBuildDependencies(type: Copy) {
delete "$buildDir/dependencies"
afterEvaluate {
from configurations.releaseCompileClasspath
into "$buildDir/dependencies"

doLast {
FileTree files = fileTree("$buildDir/dependencies")
files.forEach { file ->
if (file.isFile() && file.name.endsWith(".aar")) {
copy {
from zipTree(file).matching { include "*.jar" }
into "$buildDir/dependencies"
eachFile {
it.path = it.path.replace(it.name, "${file.name}-${it.name}")
}
}
}
}
}
}
}

我对项目依赖项有正常的依赖项:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
...
implementation 'com.google.guava:guava:11.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5'
// Note: These are Android Library projects
implementation project(':genericutils')
implementation project(':module2')
...
}

当我运行任务时 copyBuildDependencies我收到以下错误:
Executing task 'copyBuildDependencies'...

Executing tasks: [copyBuildDependencies]

Configuration on demand is an incubating feature.

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':BiometricInterfaceLib:copyBuildDependencies'.
> Could not resolve all task dependencies for configuration ':BiometricInterfaceLib:releaseCompileClasspath'.
> More than one variant of project :genericutils matches the consumer attributes:
- Configuration ':genericutils:releaseApiElements' variant android-aidl:
- Found artifactType 'android-aidl' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':genericutils:releaseApiElements' variant android-classes:
- Found artifactType 'android-classes' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':genericutils:releaseApiElements' variant android-manifest:
- Found artifactType 'android-manifest' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':genericutils:releaseApiElements' variant android-renderscript:
- Found artifactType 'android-renderscript' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':genericutils:releaseApiElements' variant jar:
- Found artifactType 'jar' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
> More than one variant of project :module2 matches the consumer attributes:
- Configuration ':module2:releaseApiElements' variant android-aidl:
- Found artifactType 'android-aidl' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':module2:releaseApiElements' variant android-classes:
- Found artifactType 'android-classes' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':module2:releaseApiElements' variant android-manifest:
- Found artifactType 'android-manifest' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':module2:releaseApiElements' variant android-renderscript:
- Found artifactType 'android-renderscript' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration ':module2:releaseApiElements' variant jar:
- Found artifactType 'jar' but wasn't required.
- 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 compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
16:39:07: Task execution finished 'copyBuildDependencies'.

我可以在 configurations.releaseCompileClasspath 上使用排除模块选项但随后 assembleRelease任务失败,因为它找不到依赖项。

编辑:我创建了一个示例项目来帮助显示我所追求的结果和我遇到的问题: https://gitlab.com/crunchy234/android-gradle-dependencies-export

--- 编辑 2 ---

需要库模块的依赖项而不是库的 Jar 的原因是可以创建 Xamarin 绑定(bind)库。

因此,Xamarin 绑定(bind)需要的是库的 AAR(示例代码中的主模块)和库依赖项的所有 Jars。
例如:
mastermodule/build/dependencies/
├── animated-vector-drawable-28.0.0-alpha3.aar-classes.jar
├── annotations-13.0.jar
...
├── collections-28.0.0-alpha3.jar
├── common-1.1.1.jar
├── coordinatorlayout-28.0.0-alpha3.aar-classes.jar
...
etc

最佳答案

如果我理解正确

  • 您的项目是一个 Android 库项目。
  • 您的复制任务尝试复制 AAR 库中的所有 jar。
  • 重命名classes.jar。

  • 可能下面就是你想要的。将此添加到 下的 build.gradle 中根项目。
    // Add this into the build.gradle under **root** project.
    // Add the modules that you want to exclude from having this copyXXXJar task.
    def modulesExcluded = [
    'app',
    'mastermodulewantedoutput'
    ]

    subprojects { prj ->

    // Skip excluded modules
    if (modulesExcluded.contains(prj.name)) {
    prj.afterEvaluate {
    tasks['preBuild'].dependsOn(copyModuleJars)
    }
    return
    }

    prj.afterEvaluate {

    /**
    * Dynamically create task "copyXXXJar" according to build variant of each library module.
    */
    android.libraryVariants.all { variant ->

    def capitalizedVariantName = variant.name.capitalize()
    def variantName = variant.flavorName
    if (!variantName || variantName == "") {
    variantName = variant.buildType.name
    } else {
    variantName += "-${variant.buildType.name}"
    }
    //================================================================================
    // Define build/copy variant jar and copy to dependencies
    //================================================================================
    def copyJarTask = project.tasks.create("copy${capitalizedVariantName}Jar", Copy) {
    group "Copy Jar"
    description "Rename the classes.jar to ${project.name}-${variantName}.jar and copy it into dist folder."


    def fromDirModule = "$buildDir/outputs/aar/${project.name}-${variantName}.aar"
    def intoDirModule = "$rootDir/dependencies/$variant.buildType.name"
    from(zipTree(fromDirModule))
    into(intoDirModule)
    include('*.jar')
    include('libs/*.jar')
    rename('classes.jar', "${project.name}-${variantName}.jar")

    dependsOn "assemble${capitalizedVariantName}"
    }
    copyModuleJars.dependsOn(copyJarTask)
    }
    }
    }

    // Task to copy all the modules' jars. It can be run with command [ ./gradlew copyModuleJars ]
    task copyModuleJars {
    group = 'Copy Jar'
    description = 'Copy the classes.jar from each module.'
    }

    运行以下命令进行验证
    ./gradlew copyModuleJars

    希望能帮助到你。

    --------- 已编辑 ----------

    我检查了你的 gitlab 项目。看起来您只需要 .aar 中的 jar。我想说这可能会导致一些问题,因为您删除了相关的 res 文件、aidl 文件、 Assets 和 R.txt 等,它们被认为是 .aar 库的必要部分。例如,您的一个依赖项 aar appcompat-v7-28.0.0-alpha3 , 解压后会有如下布局, 不仅是 classes.jar .
    appcompat-v7-28.0.0-alpha3 contents

    此外,您可以简单地修改 模块排除我在上面编辑的数组。并且可以去掉“ hasProperty() ”的条件检查。

    输出将位于“ $rootDir/dependencies ”下。如下所示:
  • 调试
  • genericutils-debug.jar
  • mastermodule-debug.jar
  • 释放
  • genericutils-release.jar
  • mastermodule-release.jar

  • 这些 jars 是从您自己的源代码生成的,即您的 通用工具 主模块 来源。如果库的传递依赖是“ .jar ”格式,它们将进入 的“ libs/ ” .aar ,如果它们是“ .aar ”格式,我认为你不能简单地选择它的 classes.jar 而是去掉它的相关和必要的部分,除非你有一些特殊的要求并且那些缺少的部分没有与您的项目有关。

    -------- 已编辑 2 ---------

    如果您只关心源代码而不使用代码逻辑中的任何资源(res、R.txt、aidl、jni 等),例如可能是你的 通用工具 模块就是这种情况,那么你可以简单地只拿起classes.jar。当您将它用作普通的 Java 库时,它不会引起任何问题。

    关于Android gradle 多项目依赖解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51129245/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com