isMinifyEnabled() is deprecated. What is the alternative?
Asked Answered
E

2

8

I use the below code to automatically generate pro guard mapping file apparently according to product flavors.

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"
                            }
                        }
                    }
                }
            }

        }
    }

After upgrading android studio to 3.0 it shows a warning saying isMinifyEnabled() is Deprecated and I could not find any solution or an alternative for this isMinifyEnabled(). Any help thanks in advance?

enter image description here

Eton answered 26/10, 2017 at 4:50 Comment(4)
on android official site it is no where written that it is deprecatedVoyeurism
it is not deprecatedInfidel
Then why is the warning?Eton
may be it is deprecated in if cases but if you set like minifyEnabled=true then it should work.Voyeurism
D
3

From the sources of Android Gradle Plugin 3.0:


    /**
     * 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();

This documentation is vague and does not directly tell what to use instead. In the blame we can see, that it's Michał Bendowski that has performed those changes, from whom I have asked to help out with that question in twitter. Here's the reply:

enter image description here

Also I cannot see @Deprecated annotation in the latest commit (at the time of writing this it's android-8.0.0_r34), which means, that the API is not deprecated there.

As a fix, you can suppress that warning putting this line before if statement:


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

Disulfide answered 13/11, 2017 at 18:55 Comment(0)
M
3

My solution was to replace variant.getBuildType().isMinifyEnabled() with variant.mappingFile.exists().

Where I previously had:

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")
        }
    }
}

I replaced it with:

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)
        }
    }
}

Or in your case replace

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

with:

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"
                }
            }
        }
    }
}

Notice how I rearranged the conditions in your code so that the mapping file is searched for only after all other assembly tasks have completed.

Metralgia answered 7/11, 2017 at 18:47 Comment(0)
D
3

From the sources of Android Gradle Plugin 3.0:


    /**
     * 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();

This documentation is vague and does not directly tell what to use instead. In the blame we can see, that it's Michał Bendowski that has performed those changes, from whom I have asked to help out with that question in twitter. Here's the reply:

enter image description here

Also I cannot see @Deprecated annotation in the latest commit (at the time of writing this it's android-8.0.0_r34), which means, that the API is not deprecated there.

As a fix, you can suppress that warning putting this line before if statement:


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

Disulfide answered 13/11, 2017 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.