如何通过代码获取Android端Google Ads SDK(AdMob)版本?
[GADRequest sdkVersion]) Got it, I totally get wanting to replicate that handy iOS SDK version check on Android—luckily there are a couple straightforward ways to do this, especially for your 17.1.1 SDK version:
Method 1: Use the Official MobileAds API (Recommended)
Google provides a dedicated method to fetch the SDK version directly, which mirrors iOS's [GADRequest sdkVersion] perfectly. Just use the MobileAds.getVersion() method:
// Make sure to import the required class first import com.google.android.gms.ads.MobileAds; // Fetch the version string String adSdkVersion = MobileAds.getVersion(); // This will return exactly the version you declared in Gradle, like "17.1.1"
This method is clean, purpose-built for this use case, and fully supported in the 17.1.1 version of the AdMob SDK you’re using.
Method 2: Fetch via Package Manager (Fallback for Older SDKs)
If you ever need to support older SDK versions where MobileAds.getVersion() isn’t available, you can pull the version from the SDK’s package info:
try { // Get package info for the AdMob SDK's underlying package PackageInfo packageInfo = getPackageManager().getPackageInfo("com.google.android.gms.ads", 0); String adSdkVersion = packageInfo.versionName; } catch (PackageManager.NameNotFoundException e) { // Handle the rare case where the package isn't found (unlikely if you have the SDK added) e.printStackTrace(); }
This works by accessing the version name of the AdMob SDK’s Android package, which matches the version you specified in your Gradle dependency.
内容的提问来源于stack exchange,提问作者user2679041

