Builder
class Builder
kotlin.Any | |
↳ | android.os.StrictMode.VmPolicy.Builder |
Creates VmPolicy
instances. Methods whose names start with detect
specify what problems we should look for. Methods whose names start with penalty
specify what we should do when we detect a problem.
You can call as many detect
and penalty
methods as you like. Currently order is insignificant: all penalties apply to all detected problems.
For example, detect everything and log anything that's found:
StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build(); StrictMode.setVmPolicy(policy);
Summary
Public constructors | |
---|---|
Builder() |
|
Builder(base: StrictMode.VmPolicy!) Build upon an existing VmPolicy. |
Public methods | |
---|---|
StrictMode.VmPolicy! |
build() Construct the VmPolicy instance. |
StrictMode.VmPolicy.Builder |
Detect leaks of |
StrictMode.VmPolicy.Builder |
Detect everything that's potentially suspect. |
StrictMode.VmPolicy.Builder |
Detect any network traffic from the calling app which is not wrapped in SSL/TLS. |
StrictMode.VmPolicy.Builder |
Detect when the calling application sends a |
StrictMode.VmPolicy.Builder |
Detect access to filesystem paths stored in credential protected storage areas while the user is locked. |
StrictMode.VmPolicy.Builder |
Detect when the calling application exposes a |
StrictMode.VmPolicy.Builder |
Detect any implicit reliance on Direct Boot automatic filtering of |
StrictMode.VmPolicy.Builder |
Detect attempts to invoke a method on a |
StrictMode.VmPolicy.Builder |
Detect when an |
StrictMode.VmPolicy.Builder |
Detect when a |
StrictMode.VmPolicy.Builder |
Detect when an |
StrictMode.VmPolicy.Builder |
Detect reflective usage of APIs that are not part of the public Android SDK. |
StrictMode.VmPolicy.Builder |
Detect when your app sends an unsafe |
StrictMode.VmPolicy.Builder |
Detect any sockets in the calling app which have not been tagged using |
StrictMode.VmPolicy.Builder |
Crashes the whole process on violation. |
StrictMode.VmPolicy.Builder |
Crashes the whole process when cleartext network traffic is detected. |
StrictMode.VmPolicy.Builder |
Crashes the whole process when a |
StrictMode.VmPolicy.Builder |
Enable detected violations log a stacktrace and timing data to the on policy violation. |
StrictMode.VmPolicy.Builder |
penaltyListener(executor: Executor, listener: StrictMode.OnVmViolationListener) Call # |
StrictMode.VmPolicy.Builder |
Log detected violations to the system log. |
StrictMode.VmPolicy.Builder |
Permit reflective usage of APIs that are not part of the public Android SDK. |
StrictMode.VmPolicy.Builder |
Permit your app to launch any |
StrictMode.VmPolicy.Builder |
setClassInstanceLimit(klass: Class<Any!>!, instanceLimit: Int) Set an upper bound on how many instances of a class can be in memory at once. |
Public constructors
Public methods
build
fun build(): StrictMode.VmPolicy!
Construct the VmPolicy instance.
Note: if no penalties are enabled before calling build
, penaltyLog
is implicitly set.
detectActivityLeaks
fun detectActivityLeaks(): StrictMode.VmPolicy.Builder
Detect leaks of android.app.Activity
subclasses.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectAll
fun detectAll(): StrictMode.VmPolicy.Builder
Detect everything that's potentially suspect.
In the Honeycomb release this includes leaks of SQLite cursors, Activities, and other closable objects but will likely expand in future releases.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectCleartextNetwork
fun detectCleartextNetwork(): StrictMode.VmPolicy.Builder
Detect any network traffic from the calling app which is not wrapped in SSL/TLS. This can help you detect places that your app is inadvertently sending cleartext data across the network.
Using penaltyDeath()
or penaltyDeathOnCleartextNetwork()
will block further traffic on that socket to prevent accidental data leakage, in addition to crashing your process.
Using penaltyDropBox()
will log the raw contents of the packet that triggered the violation.
This inspects both IPv4/IPv6 and TCP/UDP network traffic, but it may be subject to false positives, such as when STARTTLS protocols or HTTP proxies are used.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectContentUriWithoutPermission
fun detectContentUriWithoutPermission(): StrictMode.VmPolicy.Builder
Detect when the calling application sends a content://
to another app without setting android.content.Intent#FLAG_GRANT_READ_URI_PERMISSION
or android.content.Intent#FLAG_GRANT_WRITE_URI_PERMISSION
.
Forgetting to include one or more of these flags when sending an intent is typically an app bug.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectCredentialProtectedWhileLocked
fun detectCredentialProtectedWhileLocked(): StrictMode.VmPolicy.Builder
Detect access to filesystem paths stored in credential protected storage areas while the user is locked.
When a user is locked, credential protected storage is unavailable, and files stored in these locations appear to not exist, which can result in subtle app bugs if they assume default behaviors or empty states. Instead, apps should store data needed while a user is locked under device protected storage areas.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectFileUriExposure
fun detectFileUriExposure(): StrictMode.VmPolicy.Builder
Detect when the calling application exposes a file://
android.net.Uri
to another app.
This exposure is discouraged since the receiving app may not have access to the shared path. For example, the receiving app may not have requested the android.Manifest.permission#READ_EXTERNAL_STORAGE
runtime permission, or the platform may be sharing the android.net.Uri
across user profile boundaries.
Instead, apps should use content://
Uris so the platform can extend temporary permission for the receiving app to access the resource.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectImplicitDirectBoot
fun detectImplicitDirectBoot(): StrictMode.VmPolicy.Builder
Detect any implicit reliance on Direct Boot automatic filtering of PackageManager
values. Violations are only triggered when implicit calls are made while the user is locked.
Apps becoming Direct Boot aware need to carefully inspect each query site and explicitly decide which combination of flags they want to use:
PackageManager#MATCH_DIRECT_BOOT_AWARE
PackageManager#MATCH_DIRECT_BOOT_UNAWARE
PackageManager#MATCH_DIRECT_BOOT_AUTO
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectIncorrectContextUse
fun detectIncorrectContextUse(): StrictMode.VmPolicy.Builder
Detect attempts to invoke a method on a Context
that is not suited for such operation.
An example of this is trying to obtain an instance of UI service (e.g. android.view.WindowManager
) from a non-visual Context
. This is not allowed, since a non-visual Context
is not adjusted to any visual area, and therefore can report incorrect metrics or resources.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectLeakedClosableObjects
fun detectLeakedClosableObjects(): StrictMode.VmPolicy.Builder
Detect when an java.io.Closeable
or other object with an explicit termination method is finalized without having been closed.
You always want to explicitly close such objects to avoid unnecessary resources leaks.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectLeakedRegistrationObjects
fun detectLeakedRegistrationObjects(): StrictMode.VmPolicy.Builder
Detect when a BroadcastReceiver
or ServiceConnection
is leaked during Context
teardown.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectLeakedSqlLiteObjects
fun detectLeakedSqlLiteObjects(): StrictMode.VmPolicy.Builder
Detect when an android.database.sqlite.SQLiteCursor
or other SQLite object is finalized without having been closed.
You always want to explicitly close your SQLite cursors to avoid unnecessary database contention and temporary memory leaks.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectNonSdkApiUsage
fun detectNonSdkApiUsage(): StrictMode.VmPolicy.Builder
Detect reflective usage of APIs that are not part of the public Android SDK.
Note that any non-SDK APIs that this processes accesses before this detection is enabled may not be detected. To ensure that all such API accesses are detected, you should apply this policy as early as possible after process creation.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectUnsafeIntentLaunch
fun detectUnsafeIntentLaunch(): StrictMode.VmPolicy.Builder
Detect when your app sends an unsafe Intent
.
Violations may indicate security vulnerabilities in the design of your app, where a malicious app could trick you into granting Uri
permissions or launching unexported components. Here are some typical design patterns that can be used to safely resolve these violations:
- If you are sending an implicit intent to an unexported component, you should make it an explicit intent by using
Intent#setPackage
, android.content.Intent#setClassName orIntent#setComponent
. - If you are unparceling and sending an intent from the intent delivered, The ideal approach is to migrate to using a
android.app.PendingIntent
, which ensures that your launch is performed using the identity of the original creator, completely avoiding the security issues described above. - If using a
android.app.PendingIntent
isn't feasible, an alternative approach is to create a brand newIntent
and carefully copy only specific values from the originalIntent
after careful validation.
Note that this may detect false-positives if your app sends itself an Intent
which is first routed through the OS, such as using android.content.Intent#createChooser. In these cases, careful inspection is required to determine if the return point into your app is appropriately protected with a signature permission or marked as unexported. If the return point is not protected, your app is likely vulnerable to malicious apps.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
detectUntaggedSockets
fun detectUntaggedSockets(): StrictMode.VmPolicy.Builder
Detect any sockets in the calling app which have not been tagged using TrafficStats
. Tagging sockets can help you investigate network usage inside your app, such as a narrowing down heavy usage to a specific library or component.
This currently does not detect sockets created in native code.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
penaltyDeath
fun penaltyDeath(): StrictMode.VmPolicy.Builder
Crashes the whole process on violation. This penalty runs at the end of all enabled penalties so you'll still get your logging or other violations before the process dies.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
penaltyDeathOnCleartextNetwork
fun penaltyDeathOnCleartextNetwork(): StrictMode.VmPolicy.Builder
Crashes the whole process when cleartext network traffic is detected.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
See Also
penaltyDeathOnFileUriExposure
fun penaltyDeathOnFileUriExposure(): StrictMode.VmPolicy.Builder
Crashes the whole process when a file://
android.net.Uri
is exposed beyond this app.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
See Also
penaltyDropBox
fun penaltyDropBox(): StrictMode.VmPolicy.Builder
Enable detected violations log a stacktrace and timing data to the on policy violation. Intended mostly for platform integrators doing beta user field data collection.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
penaltyListener
fun penaltyListener(
executor: Executor,
listener: StrictMode.OnVmViolationListener
): StrictMode.VmPolicy.Builder
Call #OnVmViolationListener#onVmViolation(Violation)
on every violation.
Parameters | |
---|---|
executor |
Executor: This value cannot be null . |
listener |
StrictMode.OnVmViolationListener: This value cannot be null . |
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
penaltyLog
fun penaltyLog(): StrictMode.VmPolicy.Builder
Log detected violations to the system log.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
permitNonSdkApiUsage
fun permitNonSdkApiUsage(): StrictMode.VmPolicy.Builder
Permit reflective usage of APIs that are not part of the public Android SDK. Note that this only affects StrictMode
, the underlying runtime may continue to restrict or warn on access to methods that are not part of the public SDK.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
permitUnsafeIntentLaunch
fun permitUnsafeIntentLaunch(): StrictMode.VmPolicy.Builder
Permit your app to launch any Intent
which originated from outside your app.
Disabling this check is strongly discouraged, as violations may indicate security vulnerabilities in the design of your app, where a malicious app could trick you into granting Uri
permissions or launching unexported components.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |
See Also
setClassInstanceLimit
fun setClassInstanceLimit(
klass: Class<Any!>!,
instanceLimit: Int
): StrictMode.VmPolicy.Builder
Set an upper bound on how many instances of a class can be in memory at once. Helps to prevent object leaks.
Return | |
---|---|
StrictMode.VmPolicy.Builder |
This value cannot be null . |