切换到As工程后或多或少的遇到了一些问题,以前的时候总是解决后不记录,写这个是为了做个备份,以方便以后开发使用,如有错误的请留言指正,感谢!!!。
参见链接: https://developer.android.com/studio/releases/gradle-plugin.html#updating-gradle
Plugin version | Required Gradle version |
---|---|
1.0.0 - 1.1.3 | 2.2.1 - 2.3 |
1.2.0 - 1.3.1 | 2.2.1 - 2.9 |
1.5.0 | 2.2.1 - 2.13 |
2.0.0 - 2.1.2 | 2.10 - 2.13 |
2.1.3 - 2.2.3 | 2.14.1+ |
2.3.0+ | 3.3+ |
3.0.0+ | 4.1+ |
3.1.0+ | 4.4+ |
3.2.0 - 3.2.1 | 4.6+ |
3.3.0 - 3.3.3 | 4.10.1+ |
3.4.0 - 3.4.3 | 5.1.1+ |
3.5.0 - 3.5.4 | 5.4.1+ |
3.6.0 - 3.6.4 | 5.6.4+ |
4.0.0+ | 6.1.1+ |
4.1.0+ | 6.5+ |
gradle插件版本就是我们通常在classpath 'com.android.tools.build:gradle:3.0.0’中的版本号
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
gradle版本就是我们通常在gradle-wrapper.properties文件中的
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
执行gradlew build。第一次的时候会下载gradle相关内容。这个时候报错了代理访问报错。
报错被截断。错误信息就是加载3.5.3加载不出来,一直报错访问时代理失败
Could not resolve all artifacts for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:3.5.3.
于是我修检查了下我的代理配置。
(1)File->Settings -> HTTP Proxy -> No Proxy
(2)检查 gradle.properties 。位置在:C:\Users.gradle\ 查看里面是否包含了代码信息。删除掉。
解决方法:参照上面的gradle版本对应列表修改自己的gradle插件版本,implementation()应该是在gradle插件3.0.0+支持的,所以使用3.0.0+然后将gradle修改为自己的对应的版本就可以了
解决方法:一般出现这个是因为使用的gradle版本是4.0以下比如是3.3时导致的,这个时候要用
将
maven {
url 'https://maven.google.com'
}
替换
google()
报错原因:因为使用gradle脚本打包apk重命名的时候会使用下面代码:
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "小工具.apk"
}
}
当前报错是因为我使用的是AndroidStudio3.4.1,但是环境是在gradle 3.3环境下使用gradle插件为2.3.0导致的,当前的gradle插件不支持这么用。
上面是我的报错原因,但追根究底,所有人报这个错是因为每个gradle版本以及对应的gradle插件在以下方法的使用都是有区别的因为gradle插件版本不同,因此给出以下解决方法和示例
解决方法:
(1)使用的gradle版本和gradle插件要对应
(2)根据版本选择下面示例中对应的方法修改
区别是:
(1)variant.outputs.all修改为variant.outputs.each
(2) output.outputFile和outputFileName的使用
示例:
//gradle 4.1 plugin 3.0.0
android.applicationVariants.all { variant ->
variant.outputs.all {
//这里修改apk文件名
def fileName = "${defaultConfig.applicationId}.apk"
outputFileName = fileName
}
}
//gradle 3.3 plugin 2.3.0
android.applicationVariants.all { variant ->
variant.outputs.each {output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
File outputDirectory = new File(outputFile.parent)
//这里修改apk文件名
def fileName = "${defaultConfig.applicationId}.apk"
output.outputFile = new File(outputDirectory,fileName)
}
}
}
2种解决方式
方式1 ----修改gradle 版本为 2.14.1 修改plugin Version版本为2.2.3
方式2 -----升级到 classpath ‘com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.4’,Gradle用3.3的就能解决这个问题
android.libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith("release.aar")) {
def fileName = "XXX.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
查看详细日志:
gradlew processDebugManifest --stacktrace
然后根据日志去处理对应的问题(在mac下执行的命令是:./gradlew processDebugManifest --stacktrace(即在命令前面加上./))
修正:targetsdk设置应放到build.gradle文件中
INFO: The targetSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.
解决方法:检查alias是否存在,检查keystore密码和密码是否匹配正确
查看密钥相关内容:
keytool -list -v -keystore "keystore文件"
从RSA文件中查看密钥信息
keytool -printcert -file xxx.RSA
解决方法:
android{
packagingOptions {
exclude 'publicsuffixes.gz'
}
}
类似于这种问题,哪里报错,处理哪里
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
关于packagingOptions的属性相关,可查看:
链接: 参考文档.
正常情况下我们在androidStudio中修改包名直接修改applictaionId即可。但是这次我修改了我的工程后发现并不能修改成功,原因就是我集成了google服务,需要把google-service.json中的包名也改一下。
解决问题:
升级AndroidStudio版本至少到3.6版本,因为viewBinding功能是在3.5.6才变为AS的一部分。升级版本后注意相应的gradle插件的版本也要注意提升
有问题的可以下方留言,有好的解决方法我这里也会记录下来
暂时更新这么多,之后有问题会随时往下更新