1
+ < meta charset ="utf-8 ">
2
+ (#) Using a method deprecated in earlier SDK
3
+
4
+ !!! WARNING: Using a method deprecated in earlier SDK
5
+ This is a warning.
6
+
7
+ Id
8
+ : `DeprecatedSinceApi`
9
+ Summary
10
+ : Using a method deprecated in earlier SDK
11
+ Severity
12
+ : Warning
13
+ Category
14
+ : Correctness
15
+ Platform
16
+ : Android
17
+ Vendor
18
+ : Android Open Source Project
19
+ Feedback
20
+ : https://issuetracker.google.com/issues/new?component=192708
21
+ Affects
22
+ : Kotlin and Java files
23
+ Editing
24
+ : This check runs on the fly in the IDE editor
25
+ Implementation
26
+ : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/DeprecatedSinceApiDetector.kt)
27
+ Tests
28
+ : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/DeprecatedSinceApiDetectorTest.kt)
29
+ Copyright Year
30
+ : 2022
31
+
32
+ Some backport methods are only necessary until a specific version of
33
+ Android. These have been annotated with `@DeprecatedSinceApi`,
34
+ specifying the relevant API level and replacement suggestions. Calling
35
+ these methods when the `minSdkVersion` is already at the deprecated API
36
+ level or above is unnecessary.
37
+
38
+ (##) Example
39
+
40
+ Here is an example of lint warnings produced by this check:
41
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
42
+ src/test/pkg/Api.kt:26:Warning: This method is deprecated as of API
43
+ level 25 [DeprecatedSinceApi]
44
+
45
+ api.someMethod2(0) // WARN 1
46
+ ------------------
47
+
48
+
49
+ src/test/pkg/Api.kt:27:Warning: This method is deprecated as of API
50
+ level 30; Use AlarmManager.notify instead [DeprecatedSinceApi]
51
+
52
+ api.someMethod3(0) // WARN 2
53
+ ------------------
54
+
55
+
56
+ src/test/pkg/Api.kt:28:Warning: This field is deprecated as of API level
57
+ 30 [DeprecatedSinceApi]
58
+
59
+ val c = MY_CONSTANT // WARN 3
60
+ -----------
61
+
62
+
63
+ src/test/pkg/Api.kt:29:Warning: This class is deprecated as of API level
64
+ 28 [DeprecatedSinceApi]
65
+
66
+ api2.someMethod1(0) // WARN 4
67
+ -------------------
68
+
69
+
70
+ src/test/pkg/Api.kt:30:Warning: This method is deprecated as of API
71
+ level 30 [DeprecatedSinceApi]
72
+
73
+ api2.someMethod2(0) // WARN 5
74
+ -------------------
75
+
76
+
77
+ src/test/pkg/Api.kt:31:Warning: This class is deprecated as of API level
78
+ 28 [DeprecatedSinceApi]
79
+
80
+ val clz = Api2::class.java // WARN 6
81
+ -----------
82
+
83
+
84
+ src/test/pkg/Api.kt:32:Warning: This method is deprecated as of API
85
+ level 25 [DeprecatedSinceApi]
86
+
87
+ println(api::someMethod2) // WARN 7
88
+ ----------------
89
+
90
+
91
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92
+
93
+ Here is the source file referenced above:
94
+
95
+ `src/test/pkg/Api.kt`:
96
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
97
+ package test.pkg
98
+ import androidx.annotation.DeprecatedSinceApi
99
+
100
+ class Api {
101
+ @DeprecatedSinceApi(api = 21)
102
+ fun someMethod1(arg: Int) { }
103
+ @DeprecatedSinceApi(api = 25)
104
+ fun someMethod2(arg: Int) { }
105
+ @DeprecatedSinceApi(api = 30, message = "Use AlarmManager.notify instead")
106
+ fun someMethod3(arg: Int) { }
107
+ }
108
+
109
+ @DeprecatedSinceApi(api = 28)
110
+ class Api2 {
111
+ fun someMethod1(arg: Int) { }
112
+ @DeprecatedSinceApi(api = 30)
113
+ fun someMethod2(arg: Int) { }
114
+ }
115
+
116
+ @DeprecatedSinceApi(30)
117
+ const val MY_CONSTANT = "test"
118
+
119
+ class Test {
120
+ fun test(api: Api, api2: Api2) {
121
+ api.someMethod1(0) // OK
122
+ api.someMethod2(0) // WARN 1
123
+ api.someMethod3(0) // WARN 2
124
+ val c = MY_CONSTANT // WARN 3
125
+ api2.someMethod1(0) // WARN 4
126
+ api2.someMethod2(0) // WARN 5
127
+ val clz = Api2::class.java // WARN 6
128
+ println(api::someMethod2) // WARN 7
129
+ }
130
+ }
131
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132
+
133
+ You can also visit the
134
+ [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/DeprecatedSinceApiDetectorTest.kt)
135
+ for the unit tests for this check to see additional scenarios.
136
+
137
+ (##) Suppressing
138
+
139
+ You can suppress false positives using one of the following mechanisms:
140
+
141
+ * Using a suppression annotation like this on the enclosing
142
+ element:
143
+
144
+ ```kt
145
+ // Kotlin
146
+ @Suppress("DeprecatedSinceApi")
147
+ fun method() {
148
+ problematicStatement()
149
+ }
150
+ ```
151
+
152
+ or
153
+
154
+ ```java
155
+ // Java
156
+ @SuppressWarnings("DeprecatedSinceApi")
157
+ void method() {
158
+ problematicStatement();
159
+ }
160
+ ```
161
+
162
+ * Using a suppression comment like this on the line above:
163
+
164
+ ```kt
165
+ //noinspection DeprecatedSinceApi
166
+ problematicStatement()
167
+ ```
168
+
169
+ * Using a special `lint.xml` file in the source tree which turns off
170
+ the check in that folder and any sub folder. A simple file might look
171
+ like this:
172
+ ```xml
173
+ <?xml version="1.0" encoding="UTF-8"?>
174
+ <lint>
175
+ <issue id="DeprecatedSinceApi" severity="ignore" />
176
+ </lint>
177
+ ```
178
+ Instead of `ignore` you can also change the severity here, for
179
+ example from `error` to `warning`. You can find additional
180
+ documentation on how to filter issues by path, regular expression and
181
+ so on
182
+ [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).
183
+
184
+ * In Gradle projects, using the DSL syntax to configure lint. For
185
+ example, you can use something like
186
+ ```gradle
187
+ lintOptions {
188
+ disable 'DeprecatedSinceApi'
189
+ }
190
+ ```
191
+ In Android projects this should be nested inside an `android { }`
192
+ block.
193
+
194
+ * For manual invocations of `lint`, using the `--ignore` flag:
195
+ ```
196
+ $ lint --ignore DeprecatedSinceApi ...`
197
+ ```
198
+
199
+ * Last, but not least, using baselines, as discussed
200
+ [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).
201
+
202
+ <!-- Markdeep: --> < style class ="fallback "> body {visibility : hidden;white-space : pre;font-family : monospace}</ style > < script src ="markdeep.min.js " charset ="utf-8 "> </ script > < script src ="https://morgan3d.github.io/markdeep/latest/markdeep.min.js " charset ="utf-8 "> </ script > < script > window . alreadyProcessedMarkdeep || ( document . body . style . visibility = "visible" ) </ script >
0 commit comments