Skip to content

Commit 3c4e7af

Browse files
authored
Merge pull request #81 from googlesamples/update-docs
Update docs
2 parents 6b9ba23 + 9787860 commit 3c4e7af

File tree

734 files changed

+43202
-2291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

734 files changed

+43202
-2291
lines changed

Diff for: docs/api-guide/basics.md.html

+5-4
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,11 @@
714714
example layouts are processed before value files like translations)
715715
3. Kotlin and Java files
716716
4. Bytecode (local `.class` files and library `.jar` files)
717-
5. Gradle files
718-
6. Other files
719-
7. ProGuard files
720-
8. Property Files
717+
5. TOML files
718+
6. Gradle files
719+
7. Other files
720+
8. ProGuard files
721+
9. Property Files
721722

722723
Similarly, lint will always process libraries before the modules
723724
that depend on them.

Diff for: docs/api-guide/changes.md.html

+52
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,58 @@
55
information about user visible changes to lint, see the User
66
Guide.
77

8+
**8.2**
9+
10+
* For unit tests, you can now specify the language level to be used
11+
for Kotlin and Java. For example, if your unit test is using Java
12+
records, add `.javaLanguageLevel("17")` to your `lint()` test
13+
configuration.
14+
15+
**8.1**
16+
17+
* The [data flow analyzer](dataflow-analyzer.md.html) has been
18+
improved; in addition to fixing a few bugs, there are a couple of
19+
new convenience sub classes which makes common tasks easier to
20+
accomplish; see the documentation for `TargetMethodDataFlowAnalyzer`
21+
for example.
22+
23+
* The new `mavenLibrary` (and `binaryStub`) test files make it simple
24+
to create binary stub files in your tests, without having to perform
25+
compilation and check in base64 and gzip encoded test files. When
26+
your detector resolves references, the PSI elements you get back
27+
differ whether you're calling into source or into binary (jar/.class
28+
file) elements, so testing both (which the new test files automate
29+
using test modes) is helpful. More information about this is
30+
available in [](unit-testing.md.html).
31+
32+
* Lint now supports analyzing TOML files. There is a new
33+
Scope.TOML_FILE detectors can register an interest in, a new
34+
TomlScanner interface to implement for visitTomlDocument callbacks,
35+
etc. From a GradleScanner, you can directly resolve version catalog
36+
libraries via lookup methods on the GradleContext.
37+
38+
* Lint's “diff” output for unit test verification has been improved;
39+
it's now smarter about combining nearby chunks. (This should not
40+
break existing tests; the test infrastructure will try the older
41+
format as a fallback if the diffs aren't matching for the new
42+
format.)
43+
44+
* Lint contains JVM 17 bytecode. You will now need to use JDK 17+
45+
when compiling custom Lint checks. You should also configure
46+
the Kotlin compiler to target JVM 17, otherwise you may see errors
47+
when calling inline functions declared in Lint, UAST, or PSI.
48+
49+
* Lint's testing infrastructure now looks not just for test/
50+
but also androidTest/ and testFixtures/ to set the corresponding
51+
source set type on each test context.
52+
53+
**8.0**
54+
55+
* A new testmode which makes sure lint checks are all suppressible.
56+
It analyzes the reported error locations from the expected test
57+
output, and inserts suppress annotations in XML, Kotlin and Java
58+
files and makes sure that the corresponding warnings disappear.
59+
860
**7.4**
961

1062
* Annotation detectors can now specify just an annotation name instead

Diff for: docs/api-guide/dataflow-analyzer.md.html

+49
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
`DataFlowAnalyzer` class, and override one or more of its callbacks,
4545
and then tell it to analyze a method scope.
4646

47+
!!! Tip
48+
In recent versions of lint, there is a new special subclass of the
49+
`DataFlowAnalyzer`, `TargetMethodDataFlowAnalyzer`, which makes it
50+
easier to write flow analyzers where you are looking for a specific
51+
“cleanup” or close function invoked on an instance. See the separate
52+
section on `TargetMethodDataFlowAnalyzer` below for more information.
53+
4754
For the above transaction scenario, it might look like this:
4855

4956
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
@@ -303,6 +310,12 @@
303310
case you want to perform additional analysis to track field values; see
304311
the next section.
305312

313+
!!! Tip
314+
There is a special subclass of the `DataFlowAnalyzer`, called
315+
`EscapeCheckingDataFlowAnalyzer`, which you can extend instead. This
316+
handles recording all the scenarios where the instance escapes from
317+
the method, and at the end you can just check its `escaped` property.
318+
306319
## Non Local Analysis
307320

308321
In the above examples, if we found that the value escaped via a return
@@ -347,4 +360,40 @@
347360
[Source](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/SliceDetector.kt)
348361
[Test](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/SliceDetectorTest.kt)
349362

363+
## TargetMethodDataFlowAnalyzer
364+
365+
The `TargetMethodDataFlowAnalyzer` is a special subclass of the
366+
`DataFlowAnalyzer` which makes it simple to see if you eventually wind up
367+
calling a target method on a particular instance. For example, calling
368+
`close` on a file that was opened, or calling `start` on an animation you
369+
created.
370+
371+
In addition, there is an extension function on `UMethod` which visits
372+
this analyzer, and then checks for various conditions, e.g. whether the
373+
instance “escaped” (for example by being stored in a field or passed to
374+
another method), in which case you probably don't want to conclude (and
375+
report) that the close method is never called. It also handles failures
376+
to resolve, where it remembers whether there was a resolve failure, and
377+
if so it looks to see if it finds a likely match (with the same name as
378+
the target function), and if so also makes sure you don't report a false
379+
positive.
380+
381+
A simple way to do this is as follows:
382+
383+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
384+
val targets = mapOf("show" to listOf("android.widget.Toast",
385+
"com.google.android.material.snackbar.Snackbar")
386+
val analyzer = TargetMethodDataFlowAnalyzer.create(node, targets)
387+
if (method.isMissingTarget(analyzer)) {
388+
context.report(...)
389+
}
390+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
391+
392+
You can subclass `TargetMethodDataFlowAnalyzer` directly and override the
393+
`getTargetMethod` methods and any other UAST visitor methods if you want
394+
to customize the behavior further.
395+
396+
One advantage of using the `TargetMethodDataFlowAnalyzer` is that it also
397+
correctly handles method references.
398+
350399
<!-- 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>

Diff for: docs/api-guide/messages.md.html

+12
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@
8989
”Hello.“ to ”Hello, world!“ is compatible.
9090
* Adding a prefix
9191

92+
## Plurals
93+
94+
Avoid trying to make sentences gramatically correct and flexible by
95+
using constructs like ”(s)“ to quantity strings. In other words,
96+
instead of for example saying
97+
98+
*”register your receiver(s) in the manifest“*
99+
100+
just use the plural form,
101+
102+
*”register your receivers in the manifest“*
103+
92104
## Examples
93105

94106
Here are some examples from lint's built-in checks. Note that these are not

Diff for: docs/api-guide/test-modes.md.html

+14
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,18 @@
517517
string resources, and will convert regular text into `CDATA` and makes
518518
sure the results continue to be the same.
519519

520+
### Suppressible Mode
521+
522+
Users should be able to ignore lint warnings by inserting suppress annotations
523+
(in Kotlin and Java), and via `tools:ignore` attributes in XML files.
524+
525+
This normally works for simple checks, but if you are combining results from
526+
different parts of the code, or for example caching locations and reporting
527+
them later, this is sometimes broken.
528+
529+
This test mode looks at the reported warnings from your unit tests, and then
530+
for each one, it looks up the corresponding error location's source file, and
531+
inserts a suppress directive at the nearest applicable location. It then
532+
re-runs the analysis, and makes sure that the warning no longer appears.
533+
520534
<!-- 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>

Diff for: docs/api-guide/unit-testing.md.html

+81-4
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,65 @@
338338

339339
## Binary and Compiled Source Files
340340

341-
If you need to use binaries in your unit tests, there is
342-
a special test file type for that: base64gzip. Here's an
343-
example from a lint check which tries to recognize usage
344-
of Cordova in the bytecode:
341+
If you need to use binaries in your unit tests, there are two options:
342+
343+
1. base64gzip
344+
2. API stubs
345+
346+
If you want to analyze bytecode of method bodies, you'll need to use
347+
the first option.
348+
349+
The first type requires you to actually compile your test file into a
350+
set of .class files, and check those in as a gzip-compressed, base64
351+
encoded string. Lint has utilities for this; see the next section.
352+
353+
The second option is using API stubs. For simple stub files (where you
354+
only need to provide APIs you'll call as binaries, but not code), lint
355+
can produce the corresponding bytecode on the fly, so you don't need
356+
to pre-create binary contents of the class. This is particularly
357+
helpful when you just want to create stubs for a library your lint
358+
check is targeting and you want to make sure the detector is seeing
359+
the same types of elements as it will when analyzing real code outside
360+
of tests (since there is a difference between resolving into APIs from
361+
source and form binaries; when you're analyzing calls into source, you
362+
can access for example method bodies, and this isn't available via
363+
UAST from byte code.)
364+
365+
These test files also let you specify an artifact name instead of a
366+
jar path, and lint will use this to place the jar in a special place
367+
such that it recognizes it (via `JavaEvaluator.findOwnerLibrary`) as
368+
belonging to this library.
369+
370+
Here's an example of how you can create one of these binary stub
371+
files:
372+
373+
```
374+
fun testIdentityEqualsOkay() {
375+
lint().files(
376+
kotlin(
377+
"/*test contents here *using* some recycler view APIs*/"
378+
).indented(),
379+
mavenLibrary(
380+
"androidx.recyclerview:recyclerview:1.0.0",
381+
java(
382+
"""
383+
package androidx.recyclerview.widget;
384+
public class DiffUtil {
385+
public abstract static class ItemCallback<T> {
386+
public abstract boolean areItemsTheSame(T oldItem, T newItem);
387+
public abstract boolean areContentsTheSame(T oldItem, T newItem);
388+
}
389+
}
390+
"""
391+
).indented()
392+
)
393+
).run().expect(
394+
```
395+
396+
## Base64-encoded gzipped byte code
397+
398+
Here's an example from a lint check which tries to recognize usage of
399+
Cordova in the bytecode:
345400

346401
```
347402
fun testVulnerableCordovaVersionInClasses() {
@@ -411,4 +466,26 @@
411466
Dependencies and Stubs“ section above, as well as the [frequently asked
412467
questions](faq.md.html).
413468

469+
## Language Level
470+
471+
Lint will analyze Java and Kotlin test files using its own default
472+
language levels. If you need a higher (or lower) language level in order
473+
to test a particular scenario, you can use the `kotlinLanguageLevel`
474+
and `javaLanguageLevel` setter methods on the lint test configuration.
475+
Here's an example of a unit test setup for Java records:
476+
477+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin
478+
lint()
479+
.files(
480+
java("""
481+
record Person(String name, int age) {
482+
}
483+
""")
484+
.indented()
485+
)
486+
.javaLanguageLevel("17")
487+
.run()
488+
.expect(...)
489+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
490+
414491
<!-- 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>

Diff for: docs/checks/AaptCrash.md.html

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747

4848
&lt;item name="android:id"&gt;@+id/titlebar&lt;/item&gt;
4949
--------------------------------------------
50-
51-
5250
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5351

5452
Here is the source file referenced above:

Diff for: docs/checks/AcceptsUserCertificates.md.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
Editing
2424
: This check runs on the fly in the IDE editor
2525
See
26+
: https://goo.gle/AcceptsUserCertificates
27+
See
2628
: https://developer.android.com/training/articles/security-config#TrustingDebugCa
2729
Implementation
2830
: [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/NetworkSecurityConfigDetector.java)
@@ -47,8 +49,6 @@
4749

4850
&lt;certificates src="user"/&gt;
4951
--------------------------
50-
51-
5252
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5353

5454
Here is the source file referenced above:

Diff for: docs/checks/AccidentalOctal.md.html

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343

4444
versionCode 010
4545
---
46-
47-
4846
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4947

5048
Here is the source file referenced above:

Diff for: docs/checks/AdapterViewChildren.md.html

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343

4444
&lt;ListView
4545
--------
46-
47-
4846
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4947

5048
Here is the source file referenced above:

Diff for: docs/checks/AddJavascriptInterface.md.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
: https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object,%20java.lang.String)
2727
See
2828
: https://support.google.com/faqs/answer/9095419?hl=en
29+
See
30+
: https://goo.gle/AddJavascriptInterface
2931
Implementation
3032
: [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/AddJavascriptInterfaceDetector.kt)
3133
Tests
@@ -59,8 +61,6 @@
5961

6062
webView.addJavascriptInterface(object, string);
6163
----------------------
62-
63-
6464
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6565

6666
Here is the source file referenced above:

0 commit comments

Comments
 (0)