对于Android开发来讲,android:targetSdkVersion应该并不陌生,但在真正遇到问题时才发现,自己并不是完全了解他的作用,所以在这里记录一下自己学习到的相关内容。
对于Android系统来讲,向前兼容一直是比较重要的问题。当应用使用低版本的SDK实现时,需要保证在更高的系统版本中仍能正常运行,且不出现问题。这就需要compileSdkVersion、miniSdkVersion、targetSdkVersion。
targetSdkVersion is the main way Android provides forward compatibility by not applying behavior changes unless the targetSdkVersion is updated. This allows you to use new APIs (as you did update your compileSdkVersion right?) prior to working through the behavior changes.翻译过来:targetSdkVersion是Android提供的向前兼容的主要方式,除非更新targetSdkVersion,否则之前的行为不会发生改变。翻译不出来 。总得来讲,当targetSdkVersion比当前运行系统版本低时,将以低版本api运行,否则按照当前系统版本的api运行。
在之前的文章里,写过loadUrl加载“javascript:”在api19+与api18-时所出现的不同处理,具体的源码如下:
![](http://grepcode.com/static/app/images/1x1.gif)
// TODO: We may actually want to do some sanity checks here (like filter about://chrome).
// For backwards compatibility, apps targeting less than K will have JS URLs evaluated
![](http://grepcode.com/static/app/images/1x1.gif)
// directly and any result of the evaluation will not replace the current page content.
final String JAVASCRIPT_SCHEME = "javascript:";
if (mAppTargetSdkVersion < Build.VERSION_CODES.KITKAT &&
url != null && url.startsWith(JAVASCRIPT_SCHEME)) {
![](http://grepcode.com/static/app/images/1x1.gif)
mFactory.startYourEngines(true);
![](http://grepcode.com/static/app/images/1x1.gif)
if (checkNeedsPost()) {
![](http://grepcode.com/static/app/images/1x1.gif)
public void run() {
![](http://grepcode.com/static/app/images/1x1.gif)
mAwContents.evaluateJavaScriptEvenIfNotYetNavigated(
![](http://grepcode.com/static/app/images/1x1.gif)
}
![](http://grepcode.com/static/app/images/1x1.gif)
});
![](http://grepcode.com/static/app/images/1x1.gif)
} else {
![](http://grepcode.com/static/app/images/1x1.gif)
mAwContents.evaluateJavaScriptEvenIfNotYetNavigated(
![](http://grepcode.com/static/app/images/1x1.gif)
}
![](http://grepcode.com/static/app/images/1x1.gif)
return;
![](http://grepcode.com/static/app/images/1x1.gif)
}
![](http://grepcode.com/static/app/images/1x1.gif)
LoadUrlParams params = new LoadUrlParams(url);
![](http://grepcode.com/static/app/images/1x1.gif)
if (additionalHttpHeaders != null) params.setExtraHeaders(additionalHttpHeaders);
![](http://grepcode.com/static/app/images/1x1.gif)
loadUrlOnUiThread(params);
![](http://grepcode.com/static/app/images/1x1.gif)
}
参考相关文章:
- http://www.race604.com/android-targetsdkversion/
- http://https://medium.com/google-developers/picking-your-compilesdkversion-minsdkversion-targetsdkversion-a098a0341ebd#.fy7ix2y4m