AndroidStudio upgrade Gradle pit

I'm upgrading an old project recently. The original Gradle version is 4.6 and needs to be upgraded to 7.6. The JDK is upgraded from 8 to 17. There are many pitfalls along the way. Let's record them one by one.

1. The Maven warehouse needs to be upgraded to https

You will encounter this error

Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(http://mapmo.baidu.com/artifactory/libs-release)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.6.1/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details

If you cannot upgrade to https, there is a way to make it compatible. Change it to the following method.

maven {
            url "http://xxxxxx.com/artifactory/libs-release"
            allowInsecureProtocol = true
            credentials {
                username ""
                password ""
            }
        }

2. When upgrading gradle syntax, you will encounter the following error

Could not find method provided() for arguments [com.android.support:appcompat-v7:26.+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

Could not find method compile() for arguments [directory 'libs'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

It’s easy to change

provided--->compileOnly

compile--->implementation

3. Proguard cannot be found

Could not get unknown property 'proguard' for project ':XXXXX' of type org.gradle.api.Project.

This should be because the higher version of gradle is configured with R8 by default. You need to introduce proguard yourself. GPT also gave a specious answer. This problem caused me pain for a long time before I solved it. The method is as follows:

Introduce proguard into your project's build.gradle file

dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'
        classpath 'com.guardsquare:proguard-gradle:7.1.0'
    }

4. The BuildConfig file cannot be found

For old versions of gradle files, you can configure a release option in buildTypes, and a debug BuildConfig file will also be generated, which can be referenced in the project. After upgrading, if the debug option is not configured in buildTypes, the debug BuildConfig file will not be generated, resulting in an error being reported when the project is not referenced.

Modify previous file

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

after modification

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        frame {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

Just add a debug

5. There are attributes in the BuildConfig file that cannot be found.

This is also very painful. In my BuildConfig file, there is a VERSION_NAME attribute that cannot be found. It is not found in the generated BuildConfig file. I also did not find anything similar to it in the build.gradle file.

buildConfigField "String", "VERSION_NAME", "11.2"

This kind of statement, so I am very curious about how the previous code was compiled.

Finally, it was discovered that the old version of gradle would automatically change the versionCode and versionName attributes in defaultConfig into VERSION_CODE and VERSION_NAME attributes and add them to the BuildConfig file, while the new version of gradle file would not care about this.

 So just add it yourself

defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName rootProject.ext.MAPCOM_VERSION
        buildConfigField "String", "VERSION_NAME", "\"${rootProject.ext.MAPCOM_VERSION}\""
        buildConfigField "String", "MECP_VERSION", "\"${rootProject.ext.MECP_VERSION}\""
        buildConfigField "String", "MAPCOM_VERSION", "\"${rootProject.ext.MAPCOM_VERSION}\""
        buildConfigField "String", "ENGINE_VERSION", "\"${rootProject.ext.ENGINE_VERSION}\""
        buildConfigField "String", "MANUFACTURER", "\"${rootProject.ext.MANUFACTURER}\""

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

6. class.jar cannot be found

In my project, I need to compile some projects into jars. I wrote the gradle function implementation myself, but I encountered this error.

Cannot expand ZIP '/home/GitDownload/mappreset/mapcom/android/ComBase/build/intermediates/packaged-classes/release/classes.jar' as it does not exist

The original code is written like this

def zipFile = file('build/intermediates/packaged-classes/release/classes.jar')

Or the path of class.jar generated by the new version of gradle has changed, just change it to the one below

def zipFile = file('build/intermediates/aar_main_jar/release/classes.jar')

7. Duplicate Strategy problem

The error is reported as follows

Entry assets/CircleDashTexture.png is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.6.1/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.

I reported an error in the function of compiling the jar package. It is not difficult to modify it. Just add the conflict handling strategy in the first sentence, as follows

task makeJar(type:Jar){
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveName = SDK_BASENAME + "_" + SDK_VERSION + '.jar'
    // 复制资源文件
    from fileTree(dir: 'src/main', includes: ['assets/**'])
    ***********
}

8. AIDL file compilation error

If you encounter this error, org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:compileDebugAidl'

At the same time there are

* Exception is:
[2023-08-24 11:28:29] java.lang.OutOfMemoryError: Metaspace

It is usually caused by the metadata used during compilation exceeding Metaspacethe limit. It can be solved by adding it to the gradle.properties file.

org.gradle.jvmargs=-XX:MaxMetaspaceSize=512m

The increased Metaspacesize is 512MB. I compiled it with 512MB. Of course, you can also adjust this value appropriately according to the actual needs of the project.

The above is a journey through the pits, I hope it will be helpful to everyone!

Guess you like

Origin blog.csdn.net/guo_zhen_qian/article/details/132347483