Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed LeakCanary #5784

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/dependencies/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object Dependencies {
const val gson = "com.google.code.gson:gson:2.10.1"
const val firebase_analytics = "com.google.firebase:firebase-analytics:21.3.0"
const val firebase_crashlytics = "com.google.firebase:firebase-crashlytics:18.3.7"
const val leakcanary = "com.squareup.leakcanary:leakcanary-android:2.11"
const val leakcanary = "com.squareup.leakcanary:leakcanary-android:2.12"
const val timber = "com.jakewharton.timber:timber:5.0.1"
const val slf4j_api = "org.slf4j:slf4j-api:2.0.7"
const val slf4j_timber = "com.arcao:slf4j-timber:3.1@aar"
Expand Down
3 changes: 2 additions & 1 deletion collect_app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,9 @@ dependencies {
exclude group: 'org.robolectric' // Some tests in `collect_app` don't work with newer Robolectric
}

// Real LeakCanary for debug builds only: notifications, analysis, etc
// Real LeakCanary for debug and selfSigned builds only: notifications, analysis, etc
debugImplementation Dependencies.leakcanary
selfSignedReleaseImplementation Dependencies.leakcanary
}

// Must be at bottom to prevent dependency collisions
Expand Down
5 changes: 5 additions & 0 deletions collect_app/src/main/res/values/leak_canary_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="leak_canary_test_class_name" translatable="false">assertk.Assert</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain how this fixes things? Might be worth a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enables LeakCanary. It was disabled with this message: LeakCanary is currently disabled: test class org.junit.Test was found in classpath and according to https://square.github.io/leakcanary/changelog/#api-breaking-bettering-changes this is how we need to fix it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment.

Copy link
Member

@seadowg seadowg Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Not sure I get why we're using assertk.Assert as the test class to detect though? We don't use assertk as far as I'm aware.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, LeakCanary will look for the org.junit.Test class in your classpath and if found, will disable itself to avoid running in tests. However, some apps may ship JUnit in their debug classpaths (for example, when using OkHttp's MockWebServer) so we offer a way to customise the class that is used to determine that the app is running in a test environment.

My understanding is that by default it looks for org.junit.Test and in our case it was found and LeakCanary was disabled.
With this line, it will look for assertk.Assert that does not exist so LeakCanary will be enabled.
That's how I understand it but the documentation is poor and I'm not the only one complaining about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this line, it will look for assertk.Assert that does not exist so LeakCanary will be enabled.

Right. But that will mean it also runs during tests right? My interpretation of those docs is that we should set leak_canary_test_class_name to a class that will only be present in tests (which would be the original intention of org.junit.Test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I used androidx.test.ext.junit.runners.AndroidJUnit4 instead.

<bool name="leak_canary_allow_in_non_debuggable_build">true</bool>
</resources>
1 change: 1 addition & 0 deletions config/lint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
</issue>
<issue id="UnusedResources" severity="error">
<ignore path="res/values/dimens.xml" />
<ignore path="res/values/leak_canary_config.xml" />
<ignore regexp="ga_trackingId|google_crash_reporting_api_key|project_id|third_party_licenses|third_party_license_metadata" />
<ignore regexp="sms_invalid_phone_number_description" />
<ignore regexp="mapbox_access_token" />
Expand Down