任务 ':app:transformClassesWithAndroidGradleClassShrinkerForDebug' 执行失败

Posted

技术标签:

【中文标题】任务 \':app:transformClassesWithAndroidGradleClassShrinkerForDebug\' 执行失败【英文标题】:Execution failed for task ':app:transformClassesWithAndroidGradleClassShrinkerForDebug'任务 ':app:transformClassesWithAndroidGradleClassShrinkerForDebug' 执行失败 【发布时间】:2018-11-17 07:21:00 【问题描述】:

我收到此错误是因为我试图通过引用此document 来减小我的 apk 大小

任务“:app:transformClassesWithandroidGradleClassShrinkerForDebug”执行失败。 收缩过程中发现的警告,请使用 -dontwarn 或 -ignorewarnings 来抑制它们。

buildTypes 
        debug 
            minifyEnabled true
            shrinkResources true
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        
        release 
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    

如何使用-dontwarn or -ignorewarnings to suppress them?我必须在哪里添加这个键。

错误堆栈跟踪:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithAndroidGradleClassShrinkerForDebug'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
        at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
        at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
        at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
        at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: org.gradle.tooling.BuildException: Warnings found during shrinking, please use -dontwarn or -ignorewarnings to suppress them.
        at com.android.build.gradle.internal.transforms.BuiltInShrinkerTransform.checkForWarnings(BuiltInShrinkerTransform.java:200)
        at com.android.build.gradle.internal.transforms.BuiltInShrinkerTransform.fullRun(BuiltInShrinkerTransform.java:178)
        at com.android.build.gradle.internal.transforms.BuiltInShrinkerTransform.transform(BuiltInShrinkerTransform.java:130)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:221)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:217)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
        at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:212)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
        ... 29 more

编辑

proguard-rules.pro

    # Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the javascript interface
# class:
#-dontwarnclassmembers class fqcn.of.javascript.interface.for.webview 
#   public *;
#

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-dontwarnattributes SourceFile,LineNumberTable

# If you dontwarn the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-dontwarn <RoundedImageView>
-dontwarn <DOMSerializer>
-dontwarn retrofit2.**
-dontwarn java.nio.file.*
-dontwarn okio.**
-dontwarn okhttp3.**
-dontwarn org.w3c.dom.bootstrap.DOMImplementationRegistry
-dontwarn  com.androidquery.**
-dontwarn com.google.**
-dontwarn com.vimeo.networking.**
-dontwarn com.squareup.retrofit2.**
-dontwarn io.supercharge.**
-dontwarn com.github..**
-dontwarn com.android.**
-dontwarn com.airbnb.**
-dontwarn org.**
-dontwarn <google-services.json>

现在出现以下错误,如果我从 build.gradle 中删除 shrinkResources true 则不会出现任何错误。但是我需要使用shrinkResources来减小apk文件的大小,如何解决以下错误。

16:29:34.995 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: org.gradle.api.GradleException: Removing unused resources requires unused code shrinking to be turned on. See http://d.android.com/r/tools/shrink-resources.html for more information.
16:29:34.995 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.errors.SyncIssueHandlerImpl.reportIssue(SyncIssueHandlerImpl.kt:53)
16:29:34.996 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.builder.errors.EvalIssueReporter$DefaultImpls.reportError(EvalIssueReporter.kt:137)
16:29:34.996 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.errors.SyncIssueHandler$DefaultImpls.reportError(SyncIssueHandler.kt)
16:29:34.996 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.errors.SyncIssueHandlerImpl.reportError(SyncIssueHandlerImpl.kt:30)
16:29:34.996 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.scope.VariantScopeImpl.useResourceShrinker(VariantScopeImpl.java:457)
16:29:34.996 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.TaskManager.createMergeResourcesTask(TaskManager.java:924)
16:29:35.003 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.TaskManager.createAndroidTestVariantTasks(TaskManager.java:1907)
16:29:35.003 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.VariantManager.createTasksForVariantData(VariantManager.java:521)

【问题讨论】:

【参考方案1】:

使用以下配置

debug 
        minifyEnabled true
        shrinkResources true
        useProguard false
        ...
    

您尝试缩减禁用了ProGuard 的资源。而且由于资源缩减works only in conjunction with ProGuard 你得到

Removing unused resources requires unused code shrinking to be turned on.

以下配置应消除构建错误:

debug 
    minifyEnabled true
    shrinkResources true
    ...

如果之后你得到Warnings found during shrinking, please use -dontwarn or -ignorewarnings to suppress them,你需要编辑proguard-android.txt添加相应的-dontwarn &lt;package_name_with_wildcard_or_class_name&gt;

【讨论】:

感谢您的回答,它现在已修复,我还添加了 -dontnote 用于一些抛出一些错误说明的包和类。

以上是关于任务 ':app:transformClassesWithAndroidGradleClassShrinkerForDebug' 执行失败的主要内容,如果未能解决你的问题,请参考以下文章

任务计划程序打开软件的原理

CELERY 定时任务

“异步任务然后等待任务”与“任务然后返回任务”[重复]

宏任务和微任务的执行顺序

宏任务和微任务

20230515学习笔记——js中的同步任务与异步任务,宏任务与微任务