mirror of
https://github.com/googlemaps/android-samples.git
synced 2025-12-08 18:02:20 +00:00
This commit continues the process of replacing `android:onClick` attributes with programmatic click listeners using View Binding. The following activities have been updated: - `CameraDemoActivity`: All `onClick` attributes have been refactored. - `UiSettingsDemoActivity`: All `onClick` attributes have been refactored. Additionally, this commit includes: - A fix for a crash in `VisibleRegionDemoActivity` caused by a `NullPointerException` in `OnMapAndViewReadyListener`. - An increase in the Gradle heap size to prevent out-of-memory errors during the build. - A fix for a lint error in `options_demo.xml`.
4.0 KiB
4.0 KiB
Refactoring Progress for onClick Attributes
This file summarizes the progress, successful strategies, and lessons learned during the refactoring of android:onClick attributes to programmatic click listeners using View Binding.
Progress Summary
Objective: Refactor all android:onClick attributes in XML layouts to use programmatic click listeners via View Binding.
Completed Tasks:
- Enabled View Binding: Successfully enabled the
viewBindingfeature in thebuild.gradle.ktsfiles for thejava-app,kotlin-app, andcommon-uimodules. - Created Tracking File: Generated
ON_CLICKS.mdwhich lists all uniqueonClickfunction names found in the project's layout files. - Refactored Activities:
VisibleRegionDemoActivity: Fully refactored for bothjava-appandkotlin-app. AllonClickattributes (setNoPadding,setMorePadding,moveToOperaHouse,moveToSFO,moveToAUS) have been replaced.SnapshotDemoActivity: Fully refactored for bothjava-appandkotlin-app. TheonScreenshotandonClearScreenshotonClickattributes have been replaced.UiSettingsDemoActivity: Fully refactored. AllonClickattributes have been replaced.CameraDemoActivity: Fully refactored. AllonClickattributes have been replaced.OptionsDemoActivity: Fixed a lint error that was blocking the build.
Skipped Tasks
StreetViewPanoramaNavigationDemoActivity: Tabled for now. TheonClickattributes in this activity (onRequestPosition,onMovePosition, etc.) are proving difficult to test reliably due to the asynchronous nature of the Street View Panorama and the difficulty in verifying UI changes (like camera movement or toasts) in a consistent manner within the test environment. This will be revisited later.
Solutions and Strategies That Work
- Data Binding Root ID: When using View Binding, ensure that if a layout has multiple configurations (e.g.,
layoutandlayout-land), the root XML element in each version has the sameandroid:id. A mismatch will cause a build-time error. - API Key for Tests: The build process requires a Maps API key, even for tests. The most effective solution was to create
secrets.propertiesfiles in each module (java-appandkotlin-app) with a placeholder key and then modify theApiDemoApplicationclass in both modules to bypass the API key validation check during debug builds. - View Binding Scoping: For View Binding to work correctly, it must be enabled in the Gradle module where the XML layout file resides (in this case,
common-ui), not just in the application modules that use the layout. - Reliable View Matching in Tests: Using
ViewMatchers.withId()is significantly more stable and reliable thanViewMatchers.withText()for locating UI elements in Espresso tests, as text can change due to localization or other factors.
Frequent Mistakes and Lessons Learned
- Accidental File Deletion: A
git restore .command was necessary to recover a large number of project files that were accidentally deleted. This highlights the importance of careful command execution and version control as a safety net. - Build Failures due to Lint: During the refactoring process, lint can fail the build because
onClickhandlers are removed from the code before they are removed from the XML. Temporarily disablingabortOnErrorin the module'sbuild.gradle.ktsfile is an effective workaround. - Incorrect Gradle DSL Syntax: I initially used Groovy syntax (e.g.,
execution '...') in Kotlin Gradle files (build.gradle.kts), which caused build failures. The correct Kotlin DSL syntax (e.g.,execution = "...") must be used. - Incorrect File Paths: I made an error assuming the
secrets.propertiesfile was located at the project root, when the build script was configured to look for it in each module's root directory. This highlighted the importance of carefully reading build script configurations.