简体   繁体   English

如何替换 Android Gradle 构建文件中已弃用的 packagingOptions

[英]How to replace deprecated packagingOptions in Android Gradle build files

I have migrated to gradle 8 my android build.gradle files show我已经迁移到 gradle 8 我的 android build.gradle 文件显示

plugins {
    id 'com.android.application' version '8.1.0-alpha01' apply false
    id 'com.android.library' version '8.1.0-alpha01' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
    id 'com.google.dagger.hilt.android' version '2.44.2' apply false
}

tasks.register('clean') {
    delete rootProject.buildDir
}

Now in my module gradle.build files packagingOptions is highlighted as deprecated现在在我的模块 gradle.build 文件中 packagingOptions 被突出显示为已弃用

packagingOptions {
    resources {
        excludes += '/META-INF/{AL2.0,LGPL2.1}'
    }
}

在此处输入图像描述

I thought I had found this solution我以为我找到了这个解决方案

packagingOptions {
    resources.excludes.add('/META-INF/{AL2.0,LGPL2.1}')
}

which does not work?!!哪个不起作用?! what is the correct replacement for the deprecated packagingOptions exclude?已弃用的 packagingOptions exclude 的正确替代品是什么?

The version of android studio I am using is我用的android studio版本是

Android Studio Giraffe | 2022.3.1 Canary 1
Build #AI-223.4884.69.2231.9486165, built on January 13, 2023
Runtime version: 17.0.5+0-17.0.5b653.23-9410051 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.6.1
GC: G1 Young Generation, G1 Old Generation
Memory: 4096M
Cores: 12
Metal Rendering is ON
Registry:
    external.system.auto.import.disabled=true
    ide.text.editor.with.preview.show.floating.toolbar=false
    ide.images.show.chessboard=true

Non-Bundled Plugins:
    com.android.aas (3.5.1)

This appears to fix it这似乎可以解决它

packagingOptions.resources.excludes.add('/META-INF/{AL2.0,LGPL2.1}')

It doesn't say packaginOptions is deprecated:它没有说packaginOptions已被弃用:

https://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/PackagingOptions https://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/PackagingOptions

https://developer.android.com/reference/tools/gradle-api/8.0/com/android/build/api/dsl/PackagingOptions https://developer.android.com/reference/tools/gradle-api/8.0/com/android/build/api/dsl/PackagingOptions

It says excludes is deprecated:它说excludes已被弃用:

在此处输入图像描述

It's interesting that the 7.0 docs include excludes ( https://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/PackagingOptions#excludes:kotlin.collections.MutableSet ).有趣的是,7.0 文档包括excludeshttps://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/PackagingOptions#excludes:kotlin.collections.MutableSet )。

7.2 only has exclude ( https://developer.android.com/reference/tools/gradle-api/7.2/com/android/build/api/dsl/PackagingOptions#exclude(kotlin.String) ) but explains what to swap it with: 7.2 只有excludehttps://developer.android.com/reference/tools/gradle-api/7.2/com/android/build/api/dsl/PackagingOptions#exclude(kotlin.String) )但解释了交换它的内容和:

This function is deprecated.此 function 已弃用。 This method is deprecated.此方法已弃用。 Use resources.excludes.add() or jniLibs.excludes.add() instead.请改用 resources.excludes.add() 或 jniLibs.excludes.add() 。 Use jniLibs.excludes.add() for.so file patterns, and use resources.excludes.add() for all other file patterns.对 .so 文件模式使用 jniLibs.excludes.add(),对所有其他文件模式使用 resources.excludes.add()。

Which is what you've done.这就是你所做的。

If you read the 8.0 docs and click through to resources.excludes https://developer.android.com/reference/tools/gradle-api/8.0/com/android/build/api/dsl/ResourcesPackagingOptions#excludes()如果您阅读 8.0 文档并单击resources.excludes https://developer.android.com/reference/tools/gradle-api/8.0/com/android/build/api/dsl/ResourcesPackagingOptions#excludes()

It gives an example:它举了一个例子:

The set of excluded patterns.排除的模式集。 Java resources matching any of these patterns do not get packaged in the APK. Java 匹配任何这些模式的资源不会打包在 APK 中。

Example: android.packagingOptions.resources.excludes += "**/*.exclude"示例:android.packagingOptions.resources.excludes += "**/*.exclude"

So you could try that.所以你可以试试看。 (which is looks like you have). (这看起来像你有)。

Are you sure its highlighted as deprecated, and not highlighted as incubating as written here in the 8.0 docs?您确定它突出显示为已弃用,而不是像 8.0 文档中所写的那样突出显示为正在incubating吗? https://developer.android.com/reference/tools/gradle-api/8.0/com/android/build/api/dsl/PackagingOptions https://developer.android.com/reference/tools/gradle-api/8.0/com/android/build/api/dsl/PackagingOptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM