isMinifyEnabled()已弃用。有什么替代方案?

时间:2017-10-26 04:50:51

标签: android gradle android-gradle android-build android-gradle-3.0

我使用以下代码显然根据产品口味自动生成专业版保护映射文件。

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

            applicationVariants.all { variant ->
                if (variant.getBuildType().isMinifyEnabled()) {
                    variant.assemble.doLast {
                        copy {
                            from variant.mappingFile
                            into "${rootDir}/proguardTools"
                            rename { String fileName ->
                                "mapping-${variant.name}.txt"
                            }
                        }
                    }
                }
            }

        }
    }

将android studio升级到3.0后,会显示一条警告,提示isMinifyEnabled()已弃用,我找不到任何解决方案或替代isMinifyEnabled()。有任何帮助提前感谢吗?

enter image description here

2 个答案:

答案 0 :(得分:3)

我的解决方案是将variant.getBuildType().isMinifyEnabled()替换为variant.mappingFile.exists()

我之前的位置:

applicationVariants.all { variant ->
    if (variant.getBuildType().isMinifyEnabled()) {
        variant.assemble.doLast {
            (new File(variant.mappingFile.parent, "$archivesBaseName-$variant.baseName-mapping.txt")).delete()
            variant.mappingFile.renameTo(variant.mappingFile.parent +
                    "/$archivesBaseName-$variant.baseName-mapping.txt")
        }
    }
}

我将其替换为:

applicationVariants.all { variant ->
    variant.assemble.doLast {
        if (variant.mappingFile != null && variant.mappingFile.exists()) {
            def mappingFilename = "$archivesBaseName-$variant.baseName-mapping.txt"
            (new File(variant.mappingFile.parent, mappingFilename)).delete()
            variant.mappingFile.renameTo(variant.mappingFile.parent +
                    "/" + mappingFilename)
        }
    }
}

或者在您的情况下替换

applicationVariants.all { variant ->
    if (variant.getBuildType().isMinifyEnabled()) {
        variant.assemble.doLast {
            copy {
                from variant.mappingFile
                into "${rootDir}/proguardTools"
                rename { String fileName ->
                    "mapping-${variant.name}.txt"
                }
            }
        }
    }
}

使用:

applicationVariants.all { variant ->
    variant.assemble.doLast {
        if (variant.mappingFile != null && variant.mappingFile.exists()) {
            copy {
                from variant.mappingFile
                into "${rootDir}/proguardTools"
                rename { String fileName ->
                    "mapping-${variant.name}.txt"
                }
            }
        }
    }
}

请注意我如何重新排列代码中的条件,以便仅在所有其他装配任务完成后才搜索映射文件。

答案 1 :(得分:3)

来自Android Gradle Plugin 3.0的sources


    /**
     * Returns whether minification is enabled for this build type.
     *
     * @return true if minification is enabled.
     * @deprecated remember that this flag means that some "ProGuard-like" tool has run, it does not
     *     say if the tool was used to obfuscate and/or minify. In build system code this
     *     information is available elsewhere and should be used instead of this method.
     */
    @Deprecated
    boolean isMinifyEnabled();

此文档含糊不清,并不直接告诉您要使用的内容。 在blame我们可以看到,Michał Bendowski执行了这些更改,我已经请求帮助question in twitter。这是回复:

enter image description here

此外,我在latest commit(在撰写本文时为@Deprecated)时看不到android-8.0.0_r34注释,这意味着API 在那里弃用。

作为修复,您可以在if语句之前禁止该警告:


    //noinspection GrDeprecatedAPIUsage
    if (variant.getBuildType().isMinifyEnabled()) {
        ...
    }