Gradle project upgrade from version 4.4 to 4.6

One, background

Gralde version AGP (Android Gradle Plugin) version having a certain relationship, because the AGP Gradle substantially as a plug, depending on the Gradle as hosts. Thus, different versions of AGP needs to match the corresponding version Gralde. The corresponding relationship between their versions of the following:

 

 

Specific reference to the official document:
developer.android.com/studio/rele...

The current project is using Gradle version 4.4, AGP version is 3.1.0. To comply with progressive policies, this upgrade, the goal is to upgrade to version 4.6 Gralde, the corresponding, AGP upgrade to 3.2.1.

The main points to consider:
1, incremental upgrade, upgrade only from 4.4 to 4.6, while taking into account upgrade, to ensure the stability of the project;
2, upgrade Gralde and AGP version, is conducive to building projects faster;
3, shortly after the upgrade AndroidX to prepare.


Second, the main problems and solutions

Given past experience: each upgrade is not just a simple change it so simple version, each upgrade will also encounter some problems need to be addressed.
Similarly, the upgrade, or encountered some problems, mainly recorded as follows:

2.1 Project custom plug-in method signature issue

1, Issue:
Modify gradle-wrapper.properties in Gradle version changed from 4.4 4.6

....
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
复制代码

build.gradle modify the project file in the root directory, from the AGP 3.1 upgrade to 3.2.1.

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    ....
    ....
}
复制代码

Construction of the project, found the following errors:

App:cornProdReleaseMultidexKeep (Thread[Task worker for ':' Thread 4,5,main]) started.
:App:cornProdReleaseMultidexKeep
Caching disabled for task ':App:cornProdReleaseMultidexKeep': Caching has not been enabled for the task Task ':App:cornProdReleaseMultidexKeep' is not up-to-date because: Task has not declared any outputs. try to update manifest_keep.txt can't find getManifestKeepListFile method, exception:groovy.lang.MissingMethodException: No signature of method: com.android.build.gradle.internal.scope.VariantScopeImpl.getManifestKeepListFile() is applicable for argument types: () values: [] :App:cornProdReleaseMultidexKeep FAILED :App:cornProdReleaseMultidexKeep (Thread[Task worker for ':' Thread 4,5,main]) completed. Took 0.071 secs. 复制代码

Based on wrong information, is executed :App:cornProdReleaseMultidexKeepwhen the task, there are calls to the getManifestKeepListFilemethod, because the AGP version upgrades here and do not correspond to the historical compatible, direct error.

2, to solve:
the current sub-projects directly from Google official multidex program. The investigation, here is the project team custom plug-ins, and provides external configuration items. Tinker modeled object is written when the corresponding sub items by adding to the configuration maindex manifest_keep.txtin order to retain the corresponding maindexlist.txt, so as to correspond to the class will be packaged in maindex, 5.0 the following models not appear ClassNotFoundExceptionrelated errors. Plug the corresponding task will be to hook up multidex corresponding task links, equivalent to the completion of additional maindex configuration.

Body implement logic is as follows:

OptMainDexTask.groovy

@TaskAction
def doAction() {
    def ext = project.extensions.findByName(OptMainDexExtension.NAME) as OptMainDexExtension
    if (!ext.enable) {
        project.logger.error("optMainDex enable set false!")
        return } project.logger.error("try to update manifest_keep.txt") StringBuffer lines = new StringBuffer() lines.append("#optMainDex.loader patterns here\n") Iterable<String> loader = ext.loader for (String pattern : loader) { if (pattern.endsWith("*")) { if (!pattern.endsWith("**")) { pattern += "*" } } lines.append("-keep class " + pattern + " {\n" + " <init>(...);\n" + "}\n") .append("\n") } File multiDexKeepProguard = null try { multiDexKeepProguard = applicationVariant.getVariantData().getScope().getManifestKeepListProguardFile() } catch (Throwable ignore) { try { multiDexKeepProguard = applicationVariant.getVariantData().getScope().getManifestKeepListFile() } catch (Throwable e) { project.logger.error("can't find getManifestKeepListFile method, exception:${e}") } } if (multiDexKeepProguard == null) { project.logger.error("auto add multidex keep pattern fail, you can only copy ${file} to your own multiDex keep proguard file yourself.") return } FileWriter manifestWriter = new FileWriter(multiDexKeepProguard, true) try { for (String line : lines) { manifestWriter.write(line) } } finally { manifestWriter.close() } } 复制代码
cornPlugin.groovy

@Override
void apply(Project project) {
    ...
    initDex(project)
    ...
}

def initDex(Project project) {
    // 只支持Gradle 3.0以上
    project.extensions.create(OptMainDexExtension.NAME, OptMainDexExtension)
    def android = project.extensions.android

    project.afterEvaluate {
        OptMainDexExtension optMainDexExtension = project.extensions.getByName(OptMainDexExtension.NAME)

        if (!optMainDexExtension.enable) {
            return
        }

        android.applicationVariants.all { variant ->
            def variantOutput = variant.outputs.first()
            def variantName = variant.name.capitalize()
            def variantData = variant.variantData
            boolean multiDexEnabled = variantData.variantConfiguration.isMultiDexEnabled()

            if (multiDexEnabled) {
                OptMainDexTask dexConfigTask = project.tasks.create("corn${variantName}MultidexKeep", OptMainDexTask) dexConfigTask.applicationVariant = variant // for java.io.FileNotFoundException: app/build/intermediates/multi-dex/release/manifest_keep.txt // for gradle 3.x gen manifest_keep move to processResources task dexConfigTask.mustRunAfter variantOutput.processResources def multidexTask = TaskUtil.getMultiDexTask(project, variantName) if (multidexTask != null) { multidexTask.dependsOn dexConfigTask } def collectMultiDexComponentsTask = TaskUtil.getCollectMultiDexComponentsTask(project, variantName) if (collectMultiDexComponentsTask != null) { dexConfigTask.mustRunAfter collectMultiDexComponentsTask } } } } } 复制代码

OptMainDexTask, the wording is modeled tinker before, now the new version tinker in here has been modified since tinker AGP version will be upgraded to 3.2.1 and supports the use of later.

 

developer.android.com/studio/buil...
reference multidex official document, in fact, for maindex configuration it is not necessary from the plug-in around one directly through multiDexKeepFileor multiDexKeepProguardbe configured at the main project.

Corresponding, maindex here tombstone project team own plug-in, plug-in version upgrade, the main engineering reintroduced, remove the main project corresponding configuration and the need to retain the configuration corresponding to a configuration in maindex Fortunately, multiDexKeepProguardfile, and the introduction of .

defaultConfig {
    ....
    ....
    multiDexEnabled true
    multiDexKeepProguard file('multidex-config.pro')
    ....
    ....
}
复制代码

2.2 Resource obfuscation tool AndResGuard abnormal

1, issue:

Rebuild, the following error message appears:

parse to get the exist names in the resouces.arsc first
com.tencent.mm.androlib.AndrolibException: Could not decode arsc file
at com.tencent.mm.androlib.res.decoder.RawARSCDecoder.decode(RawARSCDecoder.java:74)
at com.tencent.mm.androlib.ApkDecoder.decode(ApkDecoder.java:190)
at com.tencent.mm.resourceproguard.Main.decodeResource(Main.java:96)
....
复制代码

Corresponding to the search, found AndResGuard GitHub there are many people feedback, such as correspondence AND DELINQUENCY: github.com/shwenzhang/...


2, to solve:

The reason is that after the upgrade, the system default upgrade buildToolsVersionto version 28.0.3. Leading to decode the resource problems. The current project buildToolsVersionversion 27.0.3, direct removal, the default AGP corresponding buildToolsVersionversion of the configuration.

Meanwhile, AndResGuard latest version has solved this problem, a direct upgrade to the current version of the latest version.

....
classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.17'
....
复制代码

Build again, successfully constructed.

Verification maindex subcontracting relevant manifest_keep.txtand maindexlist.txtin line with expectations. Installation and testing on the 5.0 phone, you can run properly.


Third, the conclusion

Gradle upgrade to 4.6, AGP corresponding upgrade to 3.2.1, updated the main points:

1, buildToolsVersionbegan to take the default version of the policy, AGP will correspond to the configuration in accordance with its own version of the buildToolsVersionrelease, even if because of special needs, people can also go to the specified buildToolsVersionversion, but still needs to correspond with the range version AGP version.

2, started to provide official support for direct migration to AndroidX. Google has been android.*replaced androidx.*, and will not be updated to maintain the original extension library, so as soon as possible to migrate the project to expand the library AndroidX form, it is also necessary.

3, D8 in Desugaring (desugared) enabled by default, providing support for further use Java8.

4, the new code obfuscation tool, R8, to replace the original ProGuard tool, the current default is not turned on, it can be turned through configuration.

android.enableR8 = true
复制代码

Andorid project development, involving various versions of the concept. While projects continue iterations, official development tools, the corresponding supporting environment, tools, libraries, extensions, etc., are constantly updated. To continue to have a better development experience, speed and new features compilation support, timely need to constantly upgrade the appropriate version.


Author: HappyCorn
link: https: //juejin.im/post/5d6510796fb9a06b2e3d02c8
Source: Nuggets
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/lwbqqyumidi/p/11991779.html