Android sdk升级爬坑记录

 入职新公司,第一份任务是将几个项目的target升级到Pie(28)。以下是在升级过程中遇到,搜集,整理的一些问题,在此纪录:

1.  报 java.io.IOException: Cleartext HTTP traffic to dict.youdao.com not permitted错误

原因分析

从Android 6.0开始引入了对Https的推荐支持,与以往不同,Android P的系统上面默认所有Http的请求都被阻止了。

<application android:usesCleartextTraffic=["true" | "false"]>

原本这个属性的默认值从true改变为false

解决办法

解决的办法简单来说可以通过在AnroidManifest.xml中的application显示设置

<application android:usesCleartextTraffic="true">

更为根本的解决办法是修改应用程序中Http的请求为Https,当然这也需要服务端的支持。

2.Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.util.ByteArrayBuffer" on path: DexPathList[[zip file "/data/app/com.

大致错误就是找不到org.apache.http.util.ByteArrayBuffer之类的,解决方法为在AndroidManifest.xml的<application>中加入以下代码:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

3.android8.0 取消大部分广播的静态注册(xml注册),要用动态注册,但是开机启动广播不受影响。对后台广播的应用进行了限制。

应对方案:

     a. 如果项目依赖Android O,无法修改编译版本的话,可以把静态广播修改为动态广播,因为此项限制的原因即是系统与后台应用自启之间的战争,动态广播在申请某项权限时会经过用户同意,因此是系统允许的,修改为动态广播后即可正常接收。

   b. 如果不是接收系统广播,只是两个应用间进行通信的话,可以在发送时为intent指定包名,这样接受者静态注册也是可以接收到广播的。

    Intent mIntent = new Intent(Broadcast_Action);

    mIntent.setPackage(Package_Name);

   sendBroadcast(mIntent);

   c.如果项目必须以一对多的方式发送广播,并且接收者无法动态注册的话,那么可以给Intent增加一个FLAG_RECEIVER_INCLUDE_BACKGROUND的Flag,不过这个标志位在源码中被hide掉了。可以在发送端添加如下语句:

intent.addFlags(0x01000000);

4.错误 java.lang.IllegalStateException: Not allowed to start service Intent {}: app is in background uid UidRecord{}

原因分析

从Android8.0开始,系统会对后台执行进行限制。当应用还处于后台时启动服务,这个时候被系统检测到,从而报出了java.lang.IllegalStateException错误。

解决办法

解决后台服务的限制,首先想到的办法是将服务变成前台服务,随即我们又遇到了另一个问题。如下

5. android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{}

原因分析

见Android8.0行为变更。新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。

解决办法

在后台服务启动执行执行之后,通过Service.startForeground()方法传入notification变成前台服务。需要注意的是从Android8.0开始,Notification必须制定Channel才可以正常弹出通知,如果创建Notification Channels详见这里

6.升级到Android Studio 3.1,重新构建项目时报错,报错:

The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?

解决办法:instrumentTest已被废弃,新的Gradle插件已经不支持。instrumentTest需要修改为androidTest。

7.报错:DSL element 'DexOptions.incremental' is obsolete and will be removed at the end of 2018.

是因为我项目用了incremental  dexOptions { //multiDex的一些相关配置,这样配置可以让你的编译速度更快

preDexLibraries = false //让它不要对Lib做preDexing

preDexing incremental true //开启incremental dexing,优化编译效率,这个功能android studio默认是关闭的。}

incremental这个将在2018年后被弃用,故直接删掉incremental就好了

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值