Eclipse转Android Studio踩过的各种坑

背景:项目比较复杂,接入的第三方库多,转Android Studio遇到各种坑,简直是抓狂了抓狂发火。现将我遇到的坑记录下,加深印象:


一、 method ID not in [0, 0xffff]: 65536

Android规定一个Dex文件的最大方法数是:65536,包括:Android框架方法、第三方libs和自己编写的方法。因此可以想办法减少方法数,后来发现工程引入了support-v7,但实际上只用到了support-v4中的部分方法,后来将build.gradle修改为:

compile('com.android.support:support-v4:25.1.1', {
        exclude group: 'com.android.support', module: 'support-media-compat';
        exclude group: 'com.android.support', module: 'support-core-utils';
    })

二、分包时Duplicate entry

超了最大方法数,开始想到用分包处理,但运行时出现Duplicate entry错误,后来发现,有些Module引入了V13、有些地方引入了V7,造成了重复,清理重复即可。另外可以采用如下命令,分析Module的依赖关系:

gradlew app:dependencies

三、Ignoring InnerClasses attribute for an anonymous inner class (org.apache.commons.httpclient.HttpMethodBase$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class.

工程是同事导的,奇怪的是:他那边OK,而我这边运行就报错,虽能成功运行,但网络数据都获取不到。开始怀疑是Android Studio版本及设置、Gradle版本和Gradle plugin版本,后来干脆把他的直接拷过来还是不行。简直崩溃。在网上搜了下,说的最多是:混淆错误,要在proguard-rules.pro中加入“-keepattributes EnclosingMetho”,但直接运行,哪来混淆。

进一步测试发现:我用Android5.0的手机能运行成功并安装运行,虽然有上面的错误提示,网络数据能成功获取和显示。因此判断:这个错误提示不影响功能,应该是Android版本的差异。很奇怪,另外一个同事的Android6.0就可以,简直乱套了


四、java.net.UnknownServiceException: CLEARTEXT communication not supported: []

网络数据没啥办法获取,我只好单步跟代码了,发现Volley的HurlStack类的performRequest方法中的如下:

        URL parsedUrl = new URL(url);
        HttpURLConnection connection = openConnection(parsedUrl, request);
        for (String headerName : map.keySet()) {
            connection.addRequestProperty(headerName, map.get(headerName));
        }
        setConnectionParametersForRequest(connection, request);
        // Initialize HttpResponse with data from the HttpURLConnection.
        ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
        int responseCode = connection.getResponseCode();
当执行到connection.getResponseCode()直接报如上错误,根据log搜索,基本知道是Http和Https相关(项目用的是Http请求数据),说得最多是okHttp的属性设置,因此怀疑Volley的设置问题。因为我们用的源码Module导入,担心是否中间改错了什么。后来改成了“compile 'com.android.volley:volley:1.0.0'”,还是一样的。因此跟Volley没关系了,所以去掉这个搜索项,搜索“Android6.0 cleartext”,发现有一个属性: android:uses Cleartext Traffic="true", 看了下说明感觉相关,放入工程居然真的可以了。太激动了,好想大哭一场哭

五、总结

1、Android的开放性真的既爱又恨,因此可以多试试不同的手机

2、需多了解Android新版本的新特性

3、搜索关键字的选择很重要




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值