Android工程compileSdkVersion改为6.0引入的问题

在开发中因获取上文获取mac地址的需要,将compileSdkVersion改为6.0(23).本文记录compileSdkVersion更改后引入的编译问题。

1、无法识别org.apache.http.Header

解决方法:引入org.apache.http.legacy。
一、In Android Studio,在build.gradle中添加useLibrary

android {
    useLibrary 'org.apache.http.legacy'
}

二、In Eclipse copy,将 AndroidSDK/platforms/android-23/optional 文件夹中的org.apache.http.legacy.jar拷贝至libs目录

2、不能识别Notification.setLatestEventInfo

这个版本移除了 Notification.setLatestEventInfo() 方法。
网上一般建议使用 Notification.Builder 类来代替,但在使用过程中,会出现如下编译错误。

Call requires API level 16 (current min is 14): android.app.Notification.Builder#build

但目前工程兼容4.0版本,minSdkVersion还是14。这时候需要做兼容处理。
这个时候针对API level低于16的版本,可以使用Notification.Builder来构造函数,但需要 使用getNotification()来获取Notification。在API level为16及以上的版本,则使用Builder.build来处理。

网上也看到别的方案,在API level低于16时,用反射来调用setLatestEventInfo方法。具体未验证,可参考

//反射代码
Class clazz = mNotification.getClass();  
try {  
    Method m2 = clazz.getDeclaredMethod("setLatestEventInfo", Context.class,CharSequence.class,CharSequence.class,PendingIntent.class);  
    m2.invoke(mNotification, mContext, mContentTitle,  
                                contentText, mContentIntent);   
} catch (Exception e) {  
    // TODO: handle exception  
    e.printStackTrace();  
}  

3、FloatMath找不到方法

在 Android6.0 之后 FloatMath 类就被弃用了,使用 Math 类来替代。
如:FloatMath.floor () 改为 Math.floor();

参考文档:
http://blog.csdn.net/s13488941815/article/details/53464869
http://blog.csdn.net/solo_two/article/details/51457149
http://blog.csdn.net/iblue007/article/details/51917115

你可能感兴趣的:(Android工程compileSdkVersion改为6.0引入的问题)