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

Sai
  • 15,188
  • 20
  • 81
  • 121

2 Answers2

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.

Jon
  • 9,156
  • 9
  • 56
  • 73
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()) {
        ...
    }

azizbekian
  • 60,783
  • 13
  • 169
  • 249