Skip to content

Commit

Permalink
Merge pull request #6 from barnhill/2.8.0
Browse files Browse the repository at this point in the history
kotlin 1.7, bundle printer cleanup, lint
  • Loading branch information
Brad Barnhill authored Jul 6, 2022
2 parents 089b619 + b5007ad commit be4f186
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 43 deletions.
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions savestateobserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -15,10 +15,10 @@ ext {
}

android {
compileSdkVersion 31
compileSdkVersion 32
defaultConfig {
minSdkVersion 19
targetSdkVersion 31
targetSdkVersion 32
versionCode 1
versionName version
}
Expand All @@ -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'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,30 @@ 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())
}
}
}

return contents.toString()
}

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 {
Expand All @@ -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', ' ')
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, (stringifyBundle: String) -> Unit> = hashMapOf()

Expand Down

0 comments on commit be4f186

Please sign in to comment.