Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: can I prevent a Java class (from an external library) from being instantiated at runtime

The context:

I use AdMob mediation to display banner ads in my app. I integrated Millennial ad network SDK and Millennial AdMob adapter.

Problem: my app supports Android API 9+, whereas Millennial SDK supports API 16+. Worse, instead of gracefully failing (returning no ad to the AdMob mediation layer so that it can continue going down the mediation waterfall), the SDK crashes on devices running Android < 16 (Fatal Exception: java.lang.NoSuchMethodError android.webkit.WebSettings.setAllowUniversalAccessFromFileURLs)

Apparently Millennial developers are not planning to fix this, they recommend publishing 2 distinct APKs ("<16" without their SDK and "16+" with their SDK), which is a troublesome solution.

I would prefer a simpler solution: on devices running Android API < 16, I'd like to reproduce what happens when an AdMob adapter is missing: AdMob mediation just goes to the next network. This would mean unloading or erasing the Millennial adapter class before I instantiate the AdMod mediation banner.

The question:

Is there any way to prevent any future instantation of a given class (from a 3rd party library) at runtime? (e.g. by forcing a ClassNotFound exception)

like image 349
Sébastien Avatar asked Oct 18 '22 22:10

Sébastien


1 Answers

Use two ad units. You can set up two banner ad units at AdMob.com, one with MillennialMedia in the mediation stack and one without. You can then check the API level of the device at runtime as Bonatti suggests, and set the ad unit ID on your AdView as appropriate prior to requesting ads.

If MillennialMedia is not in the mediation configuration for the ad unit being used, their adapter will not be instantiated by the Google Mobile Ads SDK.

like image 117
RedBrogdon Avatar answered Oct 21 '22 14:10

RedBrogdon