|
41 | 41 |
|
42 | 42 | Here is an example of lint warnings produced by this check: |
43 | 43 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text |
44 | | -src/test/pkg/Utils.kt:12:Warning: This method should be annotated with |
| 44 | +src/test/pkg/test.kt:8:Warning: This method should be annotated with |
45 | 45 | @ChecksSdkIntAtLeast(api=VERSION_CODES.N) [AnnotateVersionCheck] |
46 | 46 |
|
47 | | -fun isNougat1(): Boolean = VERSION.SDK_INT >= VERSION_CODES.N // 1: Should be annotated |
48 | | - --------- |
49 | | - |
50 | | - |
51 | | -src/test/pkg/Utils.kt:14:Warning: This method should be annotated with |
52 | | -@ChecksSdkIntAtLeast(api=VERSION_CODES.N) [AnnotateVersionCheck] |
53 | | - |
54 | | -fun isNougat2(): Boolean { // 2: Should be annotated |
55 | | - --------- |
56 | | - |
57 | | - |
58 | | -src/test/pkg/Utils.kt:18:Warning: This method should be annotated with |
59 | | -@ChecksSdkIntAtLeast(parameter=0) [AnnotateVersionCheck] |
60 | | - |
61 | | -fun isAtLeast2(api: Int): Boolean { // 3: Should be annotated |
62 | | - ---------- |
| 47 | +fun isNougat(): Boolean { |
| 48 | + -------- |
63 | 49 |
|
64 | 50 |
|
65 | | -src/test/pkg/Utils.kt:22:Warning: This method should be annotated with |
| 51 | +src/test/pkg/test.kt:12:Warning: This method should be annotated with |
66 | 52 | @ChecksSdkIntAtLeast(parameter=0) [AnnotateVersionCheck] |
67 | 53 |
|
68 | | -fun isAtLeast2g(api: Int): Boolean = VERSION.SDK_INT >= api // 4: Should be annotated |
69 | | - ----------- |
70 | | - |
71 | | - |
72 | | -src/test/pkg/Utils.kt:25:Warning: This method should be annotated with |
73 | | -@ChecksSdkIntAtLeast(api=Build.VERSION_CODES.ICE_CREAM_SANDWICH) |
74 | | -[AnnotateVersionCheck] |
75 | | - |
76 | | - val isIcs: Boolean // 5: Should be annotated |
77 | | - ----- |
78 | | - |
79 | | - |
80 | | -src/test/pkg/Utils.kt:27:Warning: This method should be annotated with |
81 | | -@ChecksSdkIntAtLeast(api=Build.VERSION_CODES.GINGERBREAD) |
82 | | -[AnnotateVersionCheck] |
83 | | - |
84 | | - val isGingerbread: Boolean // 6: Should be annotated |
85 | | - ------------- |
| 54 | +fun isAtLeast(api: Int): Boolean { |
| 55 | + --------- |
86 | 56 |
|
87 | 57 |
|
88 | | -src/test/pkg/Utils.kt:31:Warning: This method should be annotated with |
| 58 | +src/test/pkg/test.kt:16:Warning: This method should be annotated with |
89 | 59 | @ChecksSdkIntAtLeast(api=Build.VERSION_CODES.O, lambda=1) |
90 | 60 | [AnnotateVersionCheck] |
91 | 61 |
|
92 | | -inline fun <T> T.applyForOreoOrAbove(block: T.() -> Unit): T { // 7: Should be annotated |
| 62 | +inline fun <T> T.applyForOreoOrAbove(block: T.() -> Unit): T { |
93 | 63 | ------------------- |
94 | 64 |
|
95 | 65 |
|
96 | | -src/test/pkg/Utils.kt:38:Warning: This method should be annotated with |
97 | | -@ChecksSdkIntAtLeast(api=Build.VERSION_CODES.O, lambda=1) |
98 | | -[AnnotateVersionCheck] |
99 | | - |
100 | | -inline fun <T> T.applyForOreoOrAbove2(block: T.() -> Unit): Unit { // 8: Should be annotated |
101 | | - -------------------- |
102 | | - |
103 | | - |
104 | | -src/test/pkg/Utils.kt:46:Warning: This method should be annotated with |
105 | | -@ChecksSdkIntAtLeast(parameter=0, lambda=1) [AnnotateVersionCheck] |
106 | | - |
107 | | -inline fun <T> sdk(level: Int, func: () -> T): T? { // 9: Should be annotated |
108 | | - --- |
109 | | - |
110 | | - |
111 | | -src/test/pkg/Utils.kt:54:Warning: This method should be annotated with |
112 | | -@ChecksSdkIntAtLeast(parameter=0, lambda=1) [AnnotateVersionCheck] |
113 | | - |
114 | | -inline fun <T> sdk2(level: Int, func: () -> T): T? = // 10: Should be annotated |
115 | | - ---- |
116 | | - |
117 | | - |
118 | | -src/test/pkg/Utils.kt:61:Warning: This method should be annotated with |
119 | | -@ChecksSdkIntAtLeast(parameter=0, lambda=1) [AnnotateVersionCheck] |
120 | | - |
121 | | -inline fun fromApi(value: Int, action: () -> Unit) { // 11: Should be annotated |
122 | | - ------- |
123 | | - |
124 | | - |
125 | | -src/test/pkg/Utils.kt:67:Warning: This method should be annotated with |
126 | | -@ChecksSdkIntAtLeast(parameter=0, lambda=1) [AnnotateVersionCheck] |
127 | | - |
128 | | -fun fromApiNonInline(value: Int, action: () -> Unit) { // 12: Should be annotated |
129 | | - ---------------- |
130 | | - |
131 | | - |
132 | | -src/test/pkg/Utils.kt:79:Warning: This method should be annotated with |
133 | | -@ChecksSdkIntAtLeast(api=android.os.Build.VERSION_CODES.N_MR1) |
134 | | -[AnnotateVersionCheck] |
135 | | - |
136 | | -fun isAfterNougat(): Boolean { // 14: Should be annotated |
137 | | - ------------- |
138 | | - |
139 | | - |
140 | 66 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
141 | 67 |
|
142 | 68 | Here is the source file referenced above: |
143 | 69 |
|
144 | | -`src/test/pkg/Utils.kt`: |
| 70 | +`src/test/pkg/test.kt`: |
145 | 71 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers |
146 | | -@file:Suppress("unused", "RemoveRedundantQualifierName") |
147 | | - |
148 | 72 | package test.pkg |
149 | 73 |
|
150 | 74 | import android.os.Build |
151 | 75 | import android.os.Build.VERSION |
152 | 76 | import android.os.Build.VERSION.SDK_INT |
153 | 77 | import android.os.Build.VERSION_CODES |
154 | | -import androidx.annotation.ChecksSdkIntAtLeast |
155 | | -import androidx.core.os.BuildCompat |
156 | | - |
157 | | -fun isNougat1(): Boolean = VERSION.SDK_INT >= VERSION_CODES.N // 1: Should be annotated |
158 | 78 |
|
159 | | -fun isNougat2(): Boolean { // 2: Should be annotated |
| 79 | +fun isNougat(): Boolean { |
160 | 80 | return VERSION.SDK_INT >= VERSION_CODES.N |
161 | 81 | } |
162 | 82 |
|
163 | | -fun isAtLeast2(api: Int): Boolean { // 3: Should be annotated |
| 83 | +fun isAtLeast(api: Int): Boolean { |
164 | 84 | return VERSION.SDK_INT >= api |
165 | 85 | } |
166 | 86 |
|
167 | | -fun isAtLeast2g(api: Int): Boolean = VERSION.SDK_INT >= api // 4: Should be annotated |
168 | | - |
169 | | -private object Utils { |
170 | | - val isIcs: Boolean // 5: Should be annotated |
171 | | - get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH |
172 | | - val isGingerbread: Boolean // 6: Should be annotated |
173 | | - get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD |
174 | | -} |
175 | | - |
176 | | -inline fun <T> T.applyForOreoOrAbove(block: T.() -> Unit): T { // 7: Should be annotated |
| 87 | +inline fun <T> T.applyForOreoOrAbove(block: T.() -> Unit): T { |
177 | 88 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
178 | 89 | block() |
179 | 90 | } |
180 | 91 | return this |
181 | 92 | } |
182 | | - |
183 | | -inline fun <T> T.applyForOreoOrAbove2(block: T.() -> Unit): Unit { // 8: Should be annotated |
184 | | - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
185 | | - block() |
186 | | - } else { |
187 | | - error("Unexpected") |
188 | | - } |
189 | | -} |
190 | | - |
191 | | -inline fun <T> sdk(level: Int, func: () -> T): T? { // 9: Should be annotated |
192 | | - return if (Build.VERSION.SDK_INT >= level) { |
193 | | - func() |
194 | | - } else { |
195 | | - null |
196 | | - } |
197 | | -} |
198 | | - |
199 | | -inline fun <T> sdk2(level: Int, func: () -> T): T? = // 10: Should be annotated |
200 | | - if (Build.VERSION.SDK_INT >= level) { |
201 | | - func() |
202 | | - } else { |
203 | | - null |
204 | | - } |
205 | | - |
206 | | -inline fun fromApi(value: Int, action: () -> Unit) { // 11: Should be annotated |
207 | | - if (Build.VERSION.SDK_INT >= value) { |
208 | | - action() |
209 | | - } |
210 | | -} |
211 | | - |
212 | | -fun fromApiNonInline(value: Int, action: () -> Unit) { // 12: Should be annotated |
213 | | - if (Build.VERSION.SDK_INT >= value) { |
214 | | - action() |
215 | | - } |
216 | | -} |
217 | | - |
218 | | -inline fun notFromApi(value: Int, action: () -> Unit) { // 13: Suggest in the future? |
219 | | - if (Build.VERSION.SDK_INT < value) { |
220 | | - action() |
221 | | - } |
222 | | -} |
223 | | - |
224 | | -fun isAfterNougat(): Boolean { // 14: Should be annotated |
225 | | - return VERSION.SDK_INT > VERSION_CODES.N |
226 | | -} |
227 | | - |
228 | | -@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N) |
229 | | -fun isNougat3(): Boolean { // Should NOT annotate (already annotated) |
230 | | - return VERSION.SDK_INT >= VERSION_CODES.N |
231 | | -} |
232 | | - |
233 | | -private var unrelated: Boolean = false |
234 | | -fun unrelated(): Boolean { |
235 | | - unrelated = SDK_INT > VERSION_CODES.N; return false; } // Should NOT annotate |
236 | | - |
237 | | -fun isAtLeastN(): Boolean { // 15: Could annotate in the future |
238 | | - return BuildCompat.isAtLeastN() |
239 | | -} |
240 | | - |
241 | | -fun isAtLeastN2(): Boolean = BuildCompat.isAtLeastN() // 16: Could annotate in the future |
242 | 93 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
243 | 94 |
|
244 | 95 | You can also visit the |
245 | 96 | [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/SdkIntDetectorTest.kt) |
246 | 97 | for the unit tests for this check to see additional scenarios. |
247 | 98 |
|
248 | | -The above example was automatically extracted from the first unit test |
249 | | -found for this lint check, `SdkIntDetector.testChecksSdkIntAtLeast`. |
250 | | -To report a problem with this extracted sample, visit |
251 | | -https://issuetracker.google.com/issues/new?component=192708. |
252 | | - |
253 | 99 | (##) Suppressing |
254 | 100 |
|
255 | 101 | You can suppress false positives using one of the following mechanisms: |
|
0 commit comments