安卓注解编程 Support Annotations 详解
2015-11-04 16:43
393 查看
http://tools.android.com/tech-docs/support-annotations
Support Annotations
As of version 19.1 of the Android support library, there is a new annotations package which includes a number of useful metadata annotations you can decorate your own code with, to help catch bugs. The support library itself has also been annotated with these annotations, so as a user of the support library, Android Studio will already check your code and flag potential problems based on these annotations. As of version 22.2 of the support library, there are an additional 13 new annotations you can also use. Using the LibraryThe annotations are not included by default; they are shipped as a separate library. (The support library is now made up of a number of smaller libraries: v4-support, appcompat, gridlayout, mediarouter, and so on.)(If you're using the appcompat library, you already have access to the annotations, because appcompat itself depends on it.)The easiest way to add the annotations is to just open the Project Structure Dialog. First select your module on the left, then on the right select the Dependencies tab, click on the + button at the bottom of the panel, select Library Dependency, and assumingyou have the Android Support Repository installed in your SDK, the support annotation library will be one of the prepopulated links in the list you can just click on (it's the first element in the list) : Hit OK to finish editing the project structure. This will edit your build.gradle file as follows, which you can also do by hand: dependencies { compile 'com.android.support:support-annotations:22.2.0' }For Android application and Android library modules (e.g. when you're using apply plugin: 'com.android.application' or 'com.android.library') that's all you need to do. If you'd like to use these annotations in a Java-only module, you'll also need to include the SDK repository explicitly, since the support libraries are not available from jcenter (the Android Gradle plugins automatically include these for dependencies, but the Java plugin does not.) repositories { jcenter() maven { url '<your-SDK-path>/extras/android/m2repository' } }Enforcing Annotations When you're using Android Studio and IntelliJ, the IDE will flag calls where you are passing the wrong type of parameter to methods annotated with these annotations.As of version 1.3.0-beta1 of the Gradle plugin, and with the Android M Preview platform tools installed, these checks are also checked/enforced on the command line by the "lint" gradle task. This is useful if you want to flag problems as part of your continuous integration server. NOTE: That does not incude the nullness annotations. All the other annotations in this document are checked by lint.)Nullness Annotations The @Nullableannotation can be used to indicate that a given parameter or return value can be null. Similarly, the @NonNullannotation can be used to indicate that a given parameter (or return value) can not be null.If a local variable is known to be null (for example because some earlier code checked whether it was null), and you pass that as a parameter to a method where that parameter is marked as @NonNull, the IDE will warn you that you have a potential crash.Example from FragmentActivity in the v4 support library: import android.support.annotation.NonNull; import android.support.annotation.Nullable; ... /** * Add support for inflating the <fragment> tag. */ @Nullable @Override public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) { ...(If you use the Analyze > Infer Nullity action, or if you type in @NotNull instead of @NonNull, the IDE may offer to attach the IntelliJ annotations. See the "IntelliJ Annotations" section at the bottom for more.) NoteInitially, we annotated findViewById as @Nullable, and technically, that’s correct: findViewById can return null. But it won’t return null if you know what you’re doing (if you pass it an id that is known |
相关文章推荐
- Java编程 的动态性,第 2部分: 引入反射
- 安全参透之旅第3章 OWASP工具使用
- Java关键字final、static使用总结
- c语言学习之基础知识点介绍(十四):指针的进阶
- 人工智能之最大最小值算法+剪枝优化(算法 + C++实现)
- 8.C#循环语句练习2
- Java 编程的动态性,第 1 部分: 类和类装入
- Python调用C/C++动态链接库的方法详解
- PHP学习笔记
- java 构造函数内部的多态方法 完全剖析
- c++并发
- Python编写购物车程序
- java基础学习
- java 引用类型
- Python 多线程 threading和multiprocessing模块
- 用50行Python代码制作一个计算器
- 使用 javamail 做邮箱验证功能
- 一个asp+ACCESS省市二级联动菜单程序
- spring security过滤器
- Java魔法堂:注解用法详解——@SuppressWarnings