Skip to content

v1.3.2

Choose a tag to compare

@i2hammad i2hammad released this 22 May 18:55
· 123 commits to main since this release

AdManageKit Release Notes

Version 1.3.2

Release Date: May 22, 2025

Overview

This release enhances the ad management system in the com.i2hammad.admanagekit.admob package, introducing per-ad-unit caching, a 1-hour ad expiration mechanism, and a new boolean parameter to control whether cached or new ads are served. These changes improve ad delivery performance, reduce network requests, and ensure robust ad handling across NativeBannerSmall, NativeBannerMedium, and NativeLarge ad formats.

New Features

  • Per-Ad-Unit Caching:
    • Ads are now cached per adUnitId, allowing multiple ad units to maintain separate cached ads.
    • Implemented in NativeAdManager using a MutableMap<String, CachedAd> to store ads with their timestamps.
  • 1-Hour Ad Expiration:
    • Cached ads expire after 1 hour (3600 seconds) to ensure freshness.
    • Expired ads are automatically destroyed and removed from the cache to prevent memory leaks.
  • Boolean Option for Cached/New Ads:
    • Added useCachedAd: Boolean parameter (default: false) to loadNativeBannerAd (NativeBannerSmall, NativeBannerMedium) and loadNativeAds (NativeLarge).
    • When useCachedAd is true, a valid cached ad (not expired) is served if available; otherwise, a new ad is fetched.
  • Fallback to Cached Ads:
    • If a new ad fails to load and useCachedAd is false, the system attempts to serve a valid cached ad for the same adUnitId before triggering a failure callback.

Enhancements

  • Unified Caching Logic:
    • Replaced enableCachingLargeNativeAds in NativeLarge with enableCachingNativeAds in NativeAdManager for consistent caching control across all ad formats.
    • Standardized caching methods (setCachedNativeAd(adUnitId, nativeAd), getCachedNativeAd(adUnitId)) across all ad classes.
  • Improved AdChoices Handling:
    • Enhanced adChoicesView logic in NativeBannerMedium and NativeLarge to check nativeAd.adChoicesInfo directly, ensuring proper visibility and avoiding redundant assignments.
  • Consistent Logging and Visibility:
    • Added detailed logging for ad loading, failures, and caching events (e.g., "No valid cached ad available for adUnitId").
    • Standardized visibility constants (View.GONE, View.VISIBLE, View.INVISIBLE) across all classes.
  • Memory Management:
    • NativeAdManager destroys old or expired ads when caching new ones or clearing the cache, preventing memory leaks.
    • Added clearCachedAd(adUnitId) and clearAllCachedAds() methods to manage ad lifecycle.

Bug Fixes

  • Fixed inconsistent adChoicesView handling in NativeBannerMedium by simplifying visibility logic.
  • Corrected firebaseAnalytics initialization to ensure it’s set before use in displayAd methods.
  • Removed redundant NativeAdManager methods (e.g., setCachedNativeLargeAd, getCachedNativeLargeAd) to streamline caching logic.

Breaking Changes

  • NativeAdManager methods setCachedNativeAd and getCachedNativeAd now require an adUnitId parameter to support per-ad-unit caching.
  • Removed enableCachingLargeNativeAds from NativeLarge; use NativeAdManager.enableCachingNativeAds instead.
  • Apps using the old single-ad caching methods must update to the new per-ad-unit caching approach.

Usage Example

// Load a new ad
val nativeBannerSmall = NativeBannerSmall(context)
nativeBannerSmall.loadNativeBannerAd(activity, "your-ad-unit-id", useCachedAd = false, object : AdLoadCallback {
    override fun onAdLoaded() { Log.d("Ad", "Ad loaded") }
    override fun onFailedToLoad(adError: LoadAdError) { Log.d("Ad", "Ad failed: ${adError.message}") }
    override fun onAdImpression() { Log.d("Ad", "Ad impression") }
    override fun onAdClicked() { Log.d("Ad", "Ad clicked") }
    override fun onAdClosed() { Log.d("Ad", "Ad closed") }
})

// Load a cached ad if available and not expired
nativeBannerSmall.loadNativeBannerAd(activity, "your-ad-unit-id", useCachedAd = true)

Recommendations

  • Thread Safety: If multiple ad units are loaded concurrently, consider making NativeAdManager.cachedAds thread-safe using Collections.synchronizedMap or ConcurrentHashMap.
  • Testing: Verify caching behavior with:
    • Multiple ad units to ensure per-ad-unit caching.
    • Ads just before/after the 1-hour expiration.
    • Toggling useCachedAd and enableCachingNativeAds.
  • Lifecycle Management: Call NativeAdManager.clearAllCachedAds() in appropriate lifecycle methods (e.g., onDestroy) to free resources.

Known Limitations

  • Ad expiration is managed manually (1 hour) as Google’s NativeAd does not provide built-in expiration metadata. Verify with your ad provider (e.g., AdMob) for specific policies.
  • No automatic cache refresh; apps must explicitly request new ads when needed.

Dependencies

  • No new dependencies introduced.
  • Requires existing dependencies: Google AdMob SDK, Firebase Analytics, Shimmer, and project-specific resources (R.layout, BillingConfig, AdManager).

Acknowledgments

These changes improve ad performance and reliability, ensuring a seamless user experience while maintaining compliance with ad provider policies.

Full Changelog: v1.3.1...v1.3.2