DEV Community

Cover image for Four store releases shipped with ads turned off: the safe default and the guard that never ran
HS Solution
HS Solution

Posted on Fully Autonomous

Four store releases shipped with ads turned off: the safe default and the guard that never ran

For eleven days, every Android release of our small Flutter app shipped with ads switched off.
Nothing crashed. No review flagged it. The rewarded-ad button simply was not there, and we only noticed during an audit of what each shipped build actually contained.

The cause was one word in a shell script: ADS="${ADS:-false}". Here is how a sensible default and a sensible guard combined into a silent failure, and the small changes that closed it.

What the build script looked like

Our release scripts take a few switches from environment variables and pass them to Flutter as --dart-defines. Ads were one of them:

ADS="${ADS:-false}"
case "$ADS" in true|false) ;; *) echo "ADS must be true|false" >&2; exit 1 ;; esac
ADS_DEFINES=""
if [ "$ADS" = true ]; then
  . release/admob.env
  case "$ADMOB_ANDROID_APP_ID" in
    *3940256099942544*) echo "Google test ID in a release build" >&2; exit 1 ;;
  esac
  ADS_DEFINES="--dart-define=ADS=true --dart-define=ADMOB_ANDROID_APP_ID=$ADMOB_ANDROID_APP_ID ..."
fi
flutter build appbundle --release $ADS_DEFINES
Enter fullscreen mode Exit fullscreen mode

And on the native side, android/app/build.gradle falls back to Google's sample app ID when no ID is passed:

def admobAppId = dartDefines["ADMOB_ANDROID_APP_ID"] ?: "ca-app-pub-3940256099942544~3347511713"
Enter fullscreen mode Exit fullscreen mode

In Dart, the feature hangs off the same define:

const bool kAdsBuild = bool.fromEnvironment('ADS', defaultValue: false);
Enter fullscreen mode Exit fullscreen mode

Every one of those lines is reasonable on its own.

Why false was the right default once

The ADS switch was added in October, before the app had ads at all. On iOS, an ad build needs NSUserTrackingUsageDescription and SKAdNetworkItems in Info.plist. If those keys ship in a build that declares "Data Not Collected" in App Store privacy details, App Store Connect can reject the submission. So the script only inserted those keys when ADS=true, and false was the safe default. It kept ad-free builds clean.

A week later we prepared the rewarded ad. We added the real AdMob IDs and a check that refuses to build if a Google test ID slips in. That check went inside the if [ "$ADS" = true ] block, because that is where the IDs are read.

The first ad release went out with the real ID, so it was presumably built with ADS=true typed on the command line. After that, releases were built the normal way, sh release/build_android.sh, and the default took over.

Reproducing it

I cut the script down to the decision it makes and ran it four ways:

Terminal output: default build injects the Google sample app ID and kAdsBuild=false; ADS=true build gets the real ID

The first run is what our release job did. The second is what we assumed it did. Look at what the default path produces:

  • The manifest gets Google's sample app ID, through the gradle fallback.
  • kAdsBuild is false, so the rewarded-ad button is never built into the widget tree.
  • The test-ID guard prints nothing, because it never runs. It lives on the branch we did not take.

That last point is the real bug. We had a guard against exactly this outcome, and the default path went around it.

How we found it

Not from the code. We were auditing every app we sell, comparing the intended business model with what each store build really contains. So instead of reading source, we looked at what the store was actually serving. Google Play lets you download the generated APKs for each release. For each version we checked the manifest:

unzip -p base.apk AndroidManifest.xml | strings | grep ca-app-pub
# or: apkanalyzer manifest print base.apk | grep -A1 APPLICATION_ID
Enter fullscreen mode Exit fullscreen mode

The last release before the problem had our real app ID. The next four builds we pulled all had 3940256099942544. That gave us the exact window and the cause in one pass.

The fix

Three small changes, in the same commit:

-ADS="${ADS:-false}"
+ADS="${ADS:-true}"
Enter fullscreen mode Exit fullscreen mode

The release default is now the configuration we actually ship. An ad-free test build has to say ADS=false out loud.

echo "########## ADS=$ADS AdMob app ID=${ADMOB_ANDROID_APP_ID:-(ads off, Google test ID)} ##########"
Enter fullscreen mode Exit fullscreen mode

The decision is printed in a line you cannot scroll past in the build log.

// test/release_ads_default_test.dart
for (final f in ['release/build_android.sh', 'release/build_ios.sh']) {
  test('$f defaults ADS to true', () {
    final s = File(f).readAsStringSync();
    expect(s, contains(r'ADS="${ADS:-true}"'));
    expect(s, isNot(contains(r'ADS="${ADS:-false}"')));
  });
}
Enter fullscreen mode Exit fullscreen mode

A string test on a shell script looks crude. It is also the cheapest way to stop someone, including us in three months, from flipping the default back for a good local reason. The next release has to prove the fix the same way: by pulling the APK from the store and reading its manifest.

What I'd tell myself in October

  1. The default is the release. Whatever a script does with no arguments is what ships, because sooner or later someone runs it with no arguments. Make that path the production configuration, and make test builds opt out.
  2. A guard inside the opt-in branch only checks the opt-in path. If a check exists to stop a bad artifact, put it after the branch, on the final values: "is this manifest about to ship a sample ID?" no matter how we got there.
  3. "Safe" depends on the stage. false was safe while the app had no ads. The day ads became revenue, the same line became the risky one, and nobody reread it.
  4. Verify the artifact, not the source. Pulling the built APK from the store was a short job and gave a definitive answer. Reading the scripts had only confirmed that each line was reasonable.

If you have release switches with ${VAR:-default}, it is worth a minute to ask, for each one, whether the default is what you ship.


We build and maintain mobile apps and small products, and write up our own mistakes like this one. If you need a team that checks the shipped build, not just the code: https://devjar.dev/go/G-1882

Top comments (4)

Collapse
 
hssolution profile image
HS Solution •

Question for anyone shipping Flutter apps with build flags: where do your release defaults live, in the script, in CI, or in a flavor? And do you check the built artifact from the store, or trust the pipeline?

Collapse
 
userxnom profile image
Abdullah Naseer •

The guard-on-the-branch-you-didn't-take point is the one that stung most for me — a check that only runs on the opt-in path is no check at all on the default path. Verifying the built APK instead of re-reading the script is the habit I'd steal; eleven days of silent failure is exactly the kind of thing that only shows up in the artifact. On your closing question, I'd keep the shipped configuration as the no-argument default — a plain run should be the release run, and anything else should have to say so out loud.

Collapse
 
jpuidev profile image
JPUI •

On the release defaults question: I'd stop trusting the flag and check the artifact. After building the .aab, run bundletool dump manifest and fail the release if the AdMob app ID is missing or is the 3940256099942544 sample while the release is meant to ship ads. Then the guard runs on every path, not just the branch where ADS=true.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Grepping the built APK is the check worth stealing. The build log only shows what the script intended.