# 为什么要升级?
Android 手机系统近两年更新很快,特别是现在大多数的设备已经升级到了 9.0 甚至是 10.0

# 升级
- project 的 build 文件修改
只将 classpath 修改为了 3.1.4, 有需要的话可以根据需求往上升级
classpath 'com.android.tools.build:gradle:3.1.4'
- 修改 gradle-wrapper.properties 文件
修改了 classpath 需要同步修改这个,目前我只改到了 4.4
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
- module 的 build 文件修改
android 节点下修改
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig 节点下修改
minSdkVersion 21
targetSdkVersion 28
修改 apk 的输出名称,先前是这么用的,但是在升级后会出现下面的错误
Cannot set the value of read-only property 'outputFile' for XXX
applicationVariants.all { variant ->
if (variant.buildType.name.equals('release')) {
variant.outputs.each { output ->
def oldFile = output.outputFile
def buildName
def releaseApkName
variant.productFlavors.each { product ->
buildName = product.name
}
releaseApkName = 'cjprotect_' + defaultConfig.versionName + '.apk'
output.outputFile = new File(oldFile.parent, releaseApkName)
}
}else{
variant.outputs.each { output ->
def oldFile = output.outputFile
def buildName
def releaseApkName
variant.productFlavors.each { product ->
buildName = product.name
}
releaseApkName = 'cjprotect_debug_' + defaultConfig.versionName + '.apk'
output.outputFile = new File(oldFile.parent, releaseApkName)
}
}
}
现在需要改成这样
applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
variant.outputs.all {
outputFileName = "cjprotect_${defaultConfig.versionName}.apk"
}
}else{
variant.outputs.all {
outputFileName = "cjprotect_debug_${defaultConfig.versionName}.apk"
}
}
}
如果你配置了渠道分包
会出现这样的错误
Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.
官方给出的解释是需要配置 flavorDimensions
flavorDimensions "version"
productFlavors {
phone {
dimension "version"
buildConfigField('String', 'TYPE', '"Phone"')
buildConfigField('boolean', 'NEED_PUSH', 'false')
}
}
添加两行就好了
如果你使用了 HTTP client, 会出现这样的错误
Didn't find class "org.apache.http.impl.client.DefaultHttpClient" on path
需要在 manifest 的 application 标签中增加属性
<uses-library android:name="org.apache.http.legacy" android:required="false" />
为保证用户数据和设备的安全,Google 针对下一代 Android 系统 (Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代 (Transport Layer Security) 传输层安全协议,而 Android Nougat 和 Oreo 则不受影响.
默认联网会失败,解决办法一是降级 27, 这里我们不采用。第二种如下:
首先在 res 文件夹下创建一个 xml 文件夹,然后创建一个 network_security_config.xml 文件,文件内容如下:
<?xml version="1.0" encoding="utf-8"?> | |
<network-security-config> | |
<base-config cleartextTrafficPermitted="true" /> | |
</network-security-config> |
接着,在 AndroidManifest.xml 文件下的 application 标签增加以下属性:
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...
/>
最后还有最简单的办法,在 AndroidManifest.xml 配置文件的 <application> 标签中直接插入
android:usesCleartextTraffic="true"
- 最后记得动态权限控制