From b5007ad15bb1e3ef4d5938a3826bdb8ca29294db Mon Sep 17 00:00:00 2001 From: Brad Barnhill Date: Wed, 6 Jul 2022 09:27:23 -0500 Subject: [PATCH] kotlin 1.7, bundle printer cleanup, lint --- README.md | 46 +++++++++++-------- build.gradle | 8 ++-- gradle.properties | 2 +- savestateobserver/build.gradle | 9 ++-- .../savestateobserver/BundlePrinter.kt | 30 ++++++------ .../OversizeBundleRegistrar.kt | 1 + 6 files changed, 53 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 2728bb2..8fafc68 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,40 @@ -# SaveStateObserver ![Build Status](https://github.com/barnhill/SaveStateObserver/workflows/Android%20CI/badge.svg) [![API](https://img.shields.io/badge/API-17%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=17) +# SaveStateObserver ![Build Status](https://github.com/barnhill/SaveStateObserver/workflows/Android%20CI/badge.svg) [![API](https://img.shields.io/badge/API-19%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=19) Android tool to output the contents sizes of saved state bundles in onSaveInstance state calls. This will allow you to analyze your bundles as you pass data to other classes to prevent them from growing to sizes that are not allowed by Android. ## Usage To use in debug builds only use: ```Gradle -debugImplementation 'com.pnuema.android:savestateobserver:2.7.1' +debugImplementation 'com.pnuema.android:savestateobserver:2.8.0' ``` Once it has been included as a dependency it will listen for lifecycle state changes and output the following information without any code being inserted in your codebase. Example Logcat: - com.virtualdyno.android D/b: ===== SaveStateObserver ===== - MainActivity saved Bundle@213688783 which contained 10 elements for 180.50 KB - Bundle@192534497 com.google.firebase.analytics.screen_service contained 3 elements for 124 bytes - referrer_name = 36 bytes - id = 12 bytes - name = 4 bytes - OuterCharacterArray = 67.05 KB - Bundle@195930374 android:viewHierarchyState contained 2 elements for 2.36 KB - android:views = 2.26 KB - android:focusedViewId = 8 bytes - android:support:fragments = 1.12 KB + 2022-07-06 08:59:54.618 27026-27026/com.pnuema.android.savestateobserver.app D/SaveStateObserver: ===== SaveStateObserver ===== + MainActivity saved Bundle@142181721 which contained 8 elements for 304.73 KB + String = 32 bytes + Bundle@238359838 innerBundle contained 4 elements for 100.17 KB + String = 40 bytes + BigString = 100.02 KB Integer = 8 bytes - Short = 8 bytes - Bundle@51706823 InnerBundle contained 2 elements for 64.20 KB - InnerCharacterArray = 20.05 KB - InnerCharacterArray2 = 44.05 KB - android:lastAutofillId = 8 bytes./ - android:fragments = 352 bytes - LongArray = 44.81 KB + Float = 8 bytes + Bundle@205345279 android:viewHierarchyState contained 1 elements for 612 bytes + android:views = 568 bytes + Integer = 8 bytes + Float = 8 bytes + Bundle@257018828 androidx.lifecycle.BundlableSavedStateRegistry.key contained 4 elements for 203.19 KB + Bundle@229907989 androidx:appcompat contained 0 elements for 4 bytes + Bundle@1664298 android:support:lifecycle contained 0 elements for 4 bytes + Bundle@2035483 android:support:activity-result contained 5 elements for 1.39 KB + KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT = 152 bytes + KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS = 752 bytes + KEY_COMPONENT_ACTIVITY_REGISTERED_RCS = 56 bytes + Bundle@230021304 KEY_COMPONENT_ACTIVITY_PENDING_RESULT contained 0 elements for 4 bytes + KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS = 8 bytes + Bundle@80053905 android:support:fragments contained 1 elements for 201.54 KB + android:support:fragments = 201.47 KB + android:lastAutofillId = 8 bytes + android:fragments = 336 bytes ## Notification Consumers can register for detection events. Start by registering this on application start in debug diff --git a/build.gradle b/build.gradle index 97b11f1..71dd9d6 100644 --- a/build.gradle +++ b/build.gradle @@ -6,17 +6,17 @@ wrapper { } buildscript { - ext.kotlin_version = '1.6.21' + ext.kotlin_version = '1.7.0' repositories { google() mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.1.3' + classpath 'com.android.tools.build:gradle:7.2.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath "org.jetbrains.dokka:dokka-gradle-plugin:1.5.0" - classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0' + classpath "org.jetbrains.dokka:dokka-gradle-plugin:1.7.0" + classpath 'com.vanniktech:gradle-maven-publish-plugin:0.19.0' } } diff --git a/gradle.properties b/gradle.properties index e553e04..e6d15db 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,7 +22,7 @@ android.useAndroidX=true android.enableJetifier=false GROUP=com.pnuema.android -VERSION_NAME=2.7.1 +VERSION_NAME=2.8.0 POM_NAME=SaveStateObserver POM_ARTIFACT_ID=savestateobserver diff --git a/savestateobserver/build.gradle b/savestateobserver/build.gradle index a30ac80..69a8f05 100644 --- a/savestateobserver/build.gradle +++ b/savestateobserver/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android' apply plugin: 'org.jetbrains.dokka' apply plugin: "com.vanniktech.maven.publish" -version = "2.7.1" +version = "2.8.0" group = "com.pnuema.android" archivesBaseName = 'savestateobserver' @@ -15,10 +15,10 @@ ext { } android { - compileSdkVersion 31 + compileSdkVersion 32 defaultConfig { minSdkVersion 19 - targetSdkVersion 31 + targetSdkVersion 32 versionCode 1 versionName version } @@ -38,12 +38,11 @@ android { kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() - useIR = true } } dependencies { - implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'androidx.appcompat:appcompat:1.4.2' implementation 'androidx.startup:startup-runtime:1.1.1' testImplementation 'junit:junit:4.13.2' } diff --git a/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/BundlePrinter.kt b/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/BundlePrinter.kt index 5f2b601..ca94622 100644 --- a/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/BundlePrinter.kt +++ b/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/BundlePrinter.kt @@ -60,12 +60,16 @@ internal object BundlePrinter { bundle.keySet().forEach { key -> if (bundle.get(key) is Bundle) { val innerBundle = bundle.get(key) as Bundle - contents.append(context.resources.getQuantityString(R.plurals.bundle_entry_format, innerBundle.size(), getPrefix(layer), System.identityHashCode(innerBundle), key, innerBundle.size(), condense(context, getBundleTotalSize(innerBundle)))) - contents.append('\n') - contents.append(getContents(context, layer + 1, Objects.requireNonNull(bundle.get(key)) as Bundle)) + contents.apply { + append(context.resources.getQuantityString(R.plurals.bundle_entry_format, innerBundle.size(), getPrefix(layer), System.identityHashCode(innerBundle), key, innerBundle.size(), condense(context, getBundleTotalSize(innerBundle)))) + append(System.lineSeparator()) + append(getContents(context, layer + 1, innerBundle)) + } } else { - contents.append(context.getString(R.string.entry_format, getPrefix(layer), key, sizeOf(context, bundle, key))) - contents.append('\n') + contents.apply { + append(context.getString(R.string.entry_format, getPrefix(layer), key, sizeOf(context, bundle, key))) + append(System.lineSeparator()) + } } } @@ -73,13 +77,13 @@ internal object BundlePrinter { } private fun sizeOf(context: Context, bundle: Bundle, key: String): String { - val parcel = Parcel.obtain().apply { + Parcel.obtain().apply { writeValue(bundle.get(key)) - } - val returnValue = parcel.dataSize() - parcel.recycle() + val returnValue = dataSize() + recycle() - return condense(context, returnValue) + return condense(context, returnValue) + } } private fun condense(context: Context, bytes: Int): String { @@ -90,7 +94,7 @@ internal object BundlePrinter { } } - private fun getPrefix(layer: Int): String { - return CharBuffer.allocate(layer * SPACE_OFFSET).toString().replace('\u0000', ' ') - } + private fun getPrefix(layer: Int): String = CharBuffer.allocate(layer * SPACE_OFFSET) + .toString() + .replace('\u0000', ' ') } diff --git a/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/OversizeBundleRegistrar.kt b/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/OversizeBundleRegistrar.kt index 82de9b9..c503ce1 100644 --- a/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/OversizeBundleRegistrar.kt +++ b/savestateobserver/src/main/kotlin/com/pnuema/android/savestateobserver/OversizeBundleRegistrar.kt @@ -5,6 +5,7 @@ import java.util.* /** * Handles registration of workers to be notified should an overize bundle be detected. */ +@Suppress("unused") object OversizeBundleRegistrar { private val mapOversizeWorkRequests: HashMap Unit> = hashMapOf()