最近在做旧工程的升级,原来的Gradle版本是4.6的,需要升级到7.6,JDK从8升级到17,一路趟了很多坑,逐个记录下吧
1、Maven仓库需要升级到https
你会遇到这个报错
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
如果不能升级到https,也是有办法兼容的,改成下边的方式
maven {url "http://xxxxxx.com/artifactory/libs-release"allowInsecureProtocol = truecredentials {username ""password ""}}
2、gradle语法上的升级,会遇到如下报错
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
改起来也简单
provided--->compileOnly
compile--->implementation
3、找不到proguard
Could not get unknown property 'proguard' for project ':XXXXX' of type org.gradle.api.Project.
这个应该是高版本的gradle默认配置了R8,需要自己引入proguard,GPT也回答的似是而非的,这个问题让我蛋疼了很久才解决,方法如下:
在你工程的build.gradle文件中引入proguard
dependencies {classpath 'com.android.tools.build:gradle:7.4.2'classpath 'com.guardsquare:proguard-gradle:7.1.0'}
4、BuildConfig文件找不到
老版本的gradle文件你在buildTypes里边之配置一个release选项即可,也会生成debug的BuildConfig文件,工程中能引用到。升级了之后如果buildTypes里边没有配置debug的选项,会导致不生成debug的BuildConfig文件,导致工程引用不到报错
修改之前文件
buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'frame {minifyEnabled trueproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
修改之后
buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}debug {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}frame {minifyEnabled trueproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
加个debug就好了
5、BuildConfig文件中有属性找不到
这个也很蛋疼,我的BuildConfig文件中,有一个VERSION_NAME属性死活找不到,生成的BuildConfig文件中没有,我在build.gradle文件中也没有明确找到类似于
buildConfigField "String", "VERSION_NAME", "11.2"
这种的语句,所以非常好奇之前的代码是怎么编译过的
最终发现,老版本的gradle会把defaultConfig中的versionCode和versionName属性自动变成VERSION_CODE和VERSION_NAME属性加在BuildConfig文件中,而新版本的gradle文件不会管这个
所以自己加一下就好了
defaultConfig {minSdkVersion rootProject.ext.minSdkVersiontargetSdkVersion rootProject.ext.targetSdkVersionversionCode 1versionName rootProject.ext.MAPCOM_VERSIONbuildConfigField "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找不到
我在工程中需要把有些工程编译成jar,自己写了gradle函数实现,遇到这么个报错
Cannot expand ZIP '/home/GitDownload/mappreset/mapcom/android/ComBase/build/intermediates/packaged-classes/release/classes.jar' as it does not exist
原来的代码是这么写的
def zipFile = file('build/intermediates/packaged-classes/release/classes.jar')
还是新版本的gradle生成的class.jar的路径变了,改成下边的即可
def zipFile = file('build/intermediates/aar_main_jar/release/classes.jar')
7、Duplicate Strategy问题
报错如下
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.
我是编译jar包的函数里边报错的,修改起来不难,加上第一句的冲突处理策略就行了,如下
task makeJar(type:Jar){duplicatesStrategy = DuplicatesStrategy.EXCLUDEarchiveName = SDK_BASENAME + "_" + SDK_VERSION + '.jar'// 复制资源文件from fileTree(dir: 'src/main', includes: ['assets/**'])***********
}
以上就是趟坑之旅,希望对大家有帮助!