I could reproduce this issue with the NativeAdvancedExample app.
The reason you could not reproduce it, is that when you start this app, display an ad then hit the back button (or rather gesture),
the system does not destroy the MainActivity (as in calling onDestroy()). It just pauses it (onPause()) which does not cause a leak.
The leak only happens when the Activity is destroyed and in my own app when I do the "back" action, the system always destroys it,
supposedly because it is much more complex than this demo app and consumes a lot more memory.
I modified the NativeAdvancedExample app and added a "Destroy Activity" button that calls finish() on the MainActivity to destroy it, and the leak appears (see end of message).
I have uploaded the modified NativeAdvancedExample app via the Google form.
The only modification I made are:
- added the 'Destroy Activity' button to MainActivity
- added leak canary dependency to build.gradle
- disabled the native ad inspector in Manifest so it does not get in the way
To reproduce the problem:
- run the debug version of the app and display an ad
- tap the 'Destroy Activity' button. This calls the MainActivity#onDestroy
- wait for a bit for leak canary to analyze the dump and in the logcat you will see:
D 55:11.775 ====================================
D 55:11.775 HEAP ANALYSIS RESULT
D 55:11.775 ====================================
D 55:11.775 1 APPLICATION LEAKS
D 55:11.775 References underlined with "~~~" are likely causes.
D 55:11.775 Learn more at
https://squ.re/leaks.
D 55:11.775 52706 bytes retained by leaking objects
D 55:11.775 Signature: 91178d655ffcb34658bf9e70d4e86805f1f5cad8
D 55:11.775 ┬───
D 55:11.775 │ GC Root: Global variable in native code
D 55:11.775 │
D 55:11.775 ├─ java.util.HashMap instance
D 55:11.775 │ Leaking: UNKNOWN
D 55:11.775 │ Retaining 1.7 MB in 8406 objects
D 55:11.775 │ ↓ HashMap["ctx"]
D 55:11.775 │ ~~~~~~~
D 55:11.775 ╰→ com.google.example.gms.nativeadvancedexample.MainActivity instance
D 55:11.775 Leaking: YES (ObjectWatcher was watching this because com.google.example.gms.nativeadvancedexample.MainActivity
D 55:11.775 received Activity#onDestroy() callback and Activity#mDestroyed is true)
D 55:11.775 Retaining 52.7 kB in 998 objects
D 55:11.775 key = 56868e69-221c-41f6-8624-1abe282b7443
D 55:11.775 watchDurationMillis = 5265
D 55:11.775 retainedDurationMillis = 262
D 55:11.775 mApplication instance of android.app.Application
D 55:11.775 mBase instance of androidx.appcompat.view.ContextThemeWrapper
Note that the problem also happens with WebView banners, with AdView being leaked instead.