Backward Compatibility for Applications(向后兼容)--摘自--android.doc.resources.articles
2012-04-19 10:39
351 查看
1.Setting the minSdkVersion
设置最小支持的sdk<manifest> ... <uses-sdkandroid:minSdkVersion="3"/> ... </manifest>
2.Using reflection
使用反射检测要 使用的类 在当前运行环境下 是否有需要调用的属性或方法。like
android.os.Debug.dumpHprofData(String filename). The
Debugclass has existed since Android 1.0, but the method is new in Anroid 1.5 (API Level 3). If you try to call it directly, your app will fail to run on devices running Android 1.1 or earlier.
public class Reflect{ private static Method mDebug_dumpHprofData; static{ initCompatibility(); }; private static void initCompatibility(){ try{ mDebug_dumpHprofData =Debug.class.getMethod( "dumpHprofData",newClass[]{String.class}); /* success, this is a newer device */ }catch(NoSuchMethodException nsme){ /* failure, must be older device */ } } private static void dumpHprofData(String fileName)throws IOException{ try{ mDebug_dumpHprofData.invoke(null, fileName); }catch(InvocationTargetException ite){ /* unpack original exception when possible */ Throwable cause = ite.getCause(); if(cause instanceof IOException){ throw(IOException) cause; }else if(cause instanceofRuntimeException){ throw(RuntimeException) cause; }else if(cause instanceofError){ throw(Error) cause; }else{ /* unexpected checked exception; wrap and re-throw */ thrownewRuntimeException(ite); } }catch(IllegalAccessException ie){ System.err.println("unexpected "+ ie); } } public void fiddle(){ if(mDebug_dumpHprofData !=null){ /* feature is supported */ try{ dumpHprofData("/sdcard/dump.hprof"); }catch(IOException ie){ System.err.println("dump failed!"); } }else{ /* feature not supported, do something else */ System.out.println("dump not supported"); } } }
3.Using a wrapper class
使用一个包裹类。public class NewClass{ private static int mDiv =1; private int mMult; public static void setGlobalDiv(int div){ mDiv = div; } public NewClass(int mult){ mMult = mult; } public int doStuff(int val){ return(val * mMult)/ mDiv; } }
包裹类:
class WrapNew Class{ private NewClass mInstance; /* class initialization fails when this throws an exception */ static{ try{ Class.forName("NewClass"); }catch(Exception ex){ throw new RuntimeException(ex); } } /* calling here forces class initialization */ public static void checkAvailable(){} public static void setGlobalDiv(int div){ NewClass.setGlobalDiv(div); } public WrapNewClass(int mult){ mInstance =new NewClass(mult); } public int doStuff(int val){ return mInstance.doStuff(val); } }
4.Testing is key
进行完整的sdk测试相关文章推荐
- 【Backward Compatibility for Applications】向后兼容程序
- Backward Compatibility for Applications(向后兼容)
- Drawable Mutations(Drawable 变异)--摘自--android.doc.resources.articles
- Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.ProcessExce
- Steps for creating android applications using NDK and ARM assembly language
- 关于矢量图片资源向后兼容:CompatVectorFromResourcesEnabled标志的使用
- Error:Execution failed for task ':app:processDebugResources'com.android.ide.common.process.ProcessEx
- android源码编译出现No private recovery resources for TARGET_DEVICE解决方法
- 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
- Memory Analysis for Android Applications
- 【Android】Error:Execution failed for task ':app:mergeDebugResources'. > String index out of range: 0
- android-Storing and Searching for Data,Remaining Backward Compatible
- android 签名打包和出现的问题transformClassesAndResourcesWithProguardForRelease
- Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.Proce
- Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.bui
- Android错误之:Error:Execution failed for task ':app:mergeDebugResources'.
- 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
- Android Studio Error:Execution failed for task ':xxxxx:mergeDebugResources'
- Robotium_Automated UI testing for Android applications with Robotium
- Memory Analysis for Android Applications