Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 15 additions & 87 deletions .github/workflows/android-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ jobs:
runs-on: ubuntu-latest
env:
RELEASE_WORKDIR: ${{ github.workspace }}/.release-mobile
PROD_ADMOB_ANDROID_APP_ID: ca-app-pub-8521676951882691~5595921738
PROD_ADMOB_ANDROID_INTERSTITIAL_ID: ca-app-pub-8521676951882691/8083010939
PROD_ADMOB_ANDROID_REWARDED_ID: ca-app-pub-8521676951882691/3704782221
EAS_BUILD_PROFILE: production
EXPO_PUBLIC_ADMOB_USE_TEST_IDS: "false"
EXPO_PUBLIC_ADMOB_ANDROID_APP_ID: ca-app-pub-8521676951882691~5595921738
EXPO_PUBLIC_ADMOB_ANDROID_INTERSTITIAL_ID: ca-app-pub-8521676951882691/8083010939
EXPO_PUBLIC_ADMOB_ANDROID_REWARDED_ID: ca-app-pub-8521676951882691/3704782221

steps:
- uses: actions/checkout@v5
Expand Down Expand Up @@ -189,99 +191,19 @@ jobs:
env:
CI: "1"
EXPO_NO_TELEMETRY: "1"
EAS_BUILD_PROFILE: production
EXPO_PUBLIC_ADMOB_USE_TEST_IDS: "false"
EXPO_PUBLIC_ADMOB_ANDROID_APP_ID: ${{ env.PROD_ADMOB_ANDROID_APP_ID }}
EXPO_PUBLIC_ADMOB_ANDROID_INTERSTITIAL_ID: ${{ env.PROD_ADMOB_ANDROID_INTERSTITIAL_ID }}
EXPO_PUBLIC_ADMOB_ANDROID_REWARDED_ID: ${{ env.PROD_ADMOB_ANDROID_REWARDED_ID }}
PROD_ADMOB_ANDROID_APP_ID: ${{ env.PROD_ADMOB_ANDROID_APP_ID }}
PROD_ADMOB_ANDROID_INTERSTITIAL_ID: ${{ env.PROD_ADMOB_ANDROID_INTERSTITIAL_ID }}
PROD_ADMOB_ANDROID_REWARDED_ID: ${{ env.PROD_ADMOB_ANDROID_REWARDED_ID }}
run: |
set -euo pipefail

npx expo config --type public --json > expo-public-config.json

node <<'NODE'
const fs = require('node:fs')

const SAMPLE_IDS = [
'ca-app-pub-3940256099942544~3347511713',
'ca-app-pub-3940256099942544/1033173712',
'ca-app-pub-3940256099942544/5224354917',
]

const config = JSON.parse(fs.readFileSync('expo-public-config.json', 'utf8'))
const extra = config.extra ?? config.expo?.extra ?? {}
const adMob = extra.adMob ?? {}
const plugins = Array.isArray(config.plugins)
? config.plugins
: Array.isArray(config.expo?.plugins)
? config.expo.plugins
: []
const adMobPlugin = plugins.find((plugin) =>
Array.isArray(plugin)
? plugin[0] === 'react-native-google-mobile-ads'
: plugin === 'react-native-google-mobile-ads'
)
const adMobPluginOptions =
Array.isArray(adMobPlugin) && adMobPlugin[1] && typeof adMobPlugin[1] === 'object'
? adMobPlugin[1]
: {}

if (adMob.useTestIds !== false) {
throw new Error(`Expected extra.adMob.useTestIds=false, got ${String(adMob.useTestIds)}`)
}

const expectedPluginValues = {
androidAppId: process.env.PROD_ADMOB_ANDROID_APP_ID,
}

for (const [key, value] of Object.entries(expectedPluginValues)) {
if (!value) {
throw new Error(`Missing expected env value for ${key}`)
}
if (adMobPluginOptions[key] !== value) {
throw new Error(
`Expected react-native-google-mobile-ads.${key}=${value}, got ${String(adMobPluginOptions[key])}`
)
}
if (SAMPLE_IDS.includes(value) || SAMPLE_IDS.includes(adMobPluginOptions[key])) {
throw new Error(`Detected Google sample AdMob ID for ${key}`)
}
}

const expectedExtraValues = {
androidInterstitialId: process.env.PROD_ADMOB_ANDROID_INTERSTITIAL_ID,
androidRewardedId: process.env.PROD_ADMOB_ANDROID_REWARDED_ID,
}

for (const [key, value] of Object.entries(expectedExtraValues)) {
if (!value) {
throw new Error(`Missing expected env value for ${key}`)
}
if (adMob[key] !== value) {
throw new Error(`Expected extra.adMob.${key}=${value}, got ${String(adMob[key])}`)
}
if (SAMPLE_IDS.includes(value) || SAMPLE_IDS.includes(adMob[key])) {
throw new Error(`Detected Google sample AdMob ID for ${key}`)
}
}

console.log('Expo AdMob config validation passed.')
NODE
node scripts/assert-production-admob-config.js expo-public-config.json

- name: Generate Android project with Expo prebuild
working-directory: ${{ env.RELEASE_WORKDIR }}/apps/mobile
shell: bash
env:
CI: "1"
EXPO_NO_TELEMETRY: "1"
EAS_BUILD_PROFILE: production
EXPO_PUBLIC_ADMOB_USE_TEST_IDS: "false"
EXPO_PUBLIC_ADMOB_ANDROID_APP_ID: ${{ env.PROD_ADMOB_ANDROID_APP_ID }}
EXPO_PUBLIC_ADMOB_ANDROID_INTERSTITIAL_ID: ${{ env.PROD_ADMOB_ANDROID_INTERSTITIAL_ID }}
EXPO_PUBLIC_ADMOB_ANDROID_REWARDED_ID: ${{ env.PROD_ADMOB_ANDROID_REWARDED_ID }}
run: |
set -euo pipefail

Expand All @@ -290,8 +212,6 @@ jobs:
- name: Validate generated Android project does not contain sample AdMob IDs
working-directory: ${{ env.RELEASE_WORKDIR }}/apps/mobile
shell: bash
env:
PROD_ADMOB_ANDROID_APP_ID: ${{ env.PROD_ADMOB_ANDROID_APP_ID }}
run: |
set -euo pipefail

Expand All @@ -301,7 +221,7 @@ jobs:
exit 1
fi

if ! grep -F -q "${PROD_ADMOB_ANDROID_APP_ID}" "${MANIFEST_PATH}"; then
if ! grep -F -q "${EXPO_PUBLIC_ADMOB_ANDROID_APP_ID}" "${MANIFEST_PATH}"; then
echo "Android manifest does not contain the expected production AdMob app ID." >&2
exit 1
fi
Expand Down Expand Up @@ -350,6 +270,14 @@ jobs:
chmod +x gradlew
./gradlew --no-daemon bundleRelease

- name: Verify shipped AAB embeds production AdMob IDs
working-directory: ${{ env.RELEASE_WORKDIR }}/apps/mobile
shell: bash
run: |
set -euo pipefail
node scripts/assert-aab-admob-config.js \
android/app/build/outputs/bundle/release/app-release.aab

- name: Verify Android widget is bundled in release manifest
working-directory: ${{ env.RELEASE_WORKDIR }}/apps/mobile/android
shell: bash
Expand Down
47 changes: 47 additions & 0 deletions apps/mobile/__tests__/hooks/use-ad-mob.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,53 @@ describe('mobile useAdMob', () => {
mocks.state.rewardeds[0]?.emit('rewarded')
mocks.state.rewardeds[0]?.emit('closed')
await expect(rewardedAttempt).resolves.toBe(true)

expect(mocks.createInterstitial).not.toHaveBeenCalledWith('test-interstitial')
expect(mocks.createRewarded).not.toHaveBeenCalledWith('test-rewarded')
})

it('falls back to test ids in development when useTestIds is not configured', async () => {
mocks.constants.expoConfig.extra = {
adMob: {
androidInterstitialId: 'prod-interstitial',
androidRewardedId: 'prod-rewarded',
},
}
const { result } = await renderUseAdMob()

await TestRenderer.act(async () => {
await result.initialize()
})

const interstitialAttempt = result.showInterstitialIfDue()
await Promise.resolve()
expect(mocks.createInterstitial).toHaveBeenCalledWith('test-interstitial')
expect(mocks.createInterstitial).not.toHaveBeenCalledWith('prod-interstitial')
mocks.state.interstitials[0]?.emit('loaded')
await Promise.resolve()
mocks.state.interstitials[0]?.emit('closed')
await interstitialAttempt
})

it('does not request an ad when production ids are missing', async () => {
mocks.constants.expoConfig.extra = {
adMob: {
useTestIds: false,
androidInterstitialId: null,
androidRewardedId: null,
},
}
const { result } = await renderUseAdMob()

await TestRenderer.act(async () => {
await result.initialize()
})

await result.showInterstitialIfDue()
await expect(result.showRewardedAd()).resolves.toBe(false)

expect(mocks.createInterstitial).not.toHaveBeenCalled()
expect(mocks.createRewarded).not.toHaveBeenCalled()
})

it('increments the claimed reward count in the profile cache', async () => {
Expand Down
2 changes: 2 additions & 0 deletions apps/mobile/scripts/android-release-apk.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const { execSync } = require('child_process')
const fs = require('fs')
const os = require('os')
const path = require('path')
const { resolveProductionAdMobEnv } = require('./production-admob-env')

process.env.EXPO_NO_METRO_WORKSPACE_ROOT = '1'
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
Object.assign(process.env, resolveProductionAdMobEnv(process.env))

const projectRoot = path.join(__dirname, '..')
const androidDir = path.join(__dirname, '..', 'android')
Expand Down
16 changes: 8 additions & 8 deletions apps/mobile/scripts/android-release-device.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')
const { resolveProductionAdMobEnv } = require('./production-admob-env')

const projectRoot = path.resolve(__dirname, '..')
const androidDir = path.join(projectRoot, 'android')
Expand Down Expand Up @@ -91,27 +92,26 @@ async function waitForPid(deviceId) {
}

async function main() {
const releaseEnv = {
...resolveProductionAdMobEnv(process.env),
EXPO_NO_METRO_WORKSPACE_ROOT: '1',
}

const deviceId = await getConnectedDeviceId()

console.log(`Using adb device: ${deviceId}`)
console.log('Syncing Android native resources from Expo config...')

await run('cmd.exe', ['/c', 'npx', 'expo', 'prebuild', '--platform', 'android', '--no-install'], {
cwd: projectRoot,
env: {
...process.env,
EXPO_NO_METRO_WORKSPACE_ROOT: '1',
},
env: releaseEnv,
})

console.log('Building release APK with Gradle...')

await run('cmd.exe', ['/c', 'gradlew.bat', 'assembleRelease'], {
cwd: androidDir,
env: {
...process.env,
EXPO_NO_METRO_WORKSPACE_ROOT: '1',
},
env: releaseEnv,
})

if (!fs.existsSync(apkPath)) {
Expand Down
67 changes: 67 additions & 0 deletions apps/mobile/scripts/assert-aab-admob-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const fs = require("node:fs");
const { execFileSync } = require("node:child_process");
const {
SAMPLE_AD_UNIT_IDS,
} = require("./assert-production-admob-config");

const JS_BUNDLE_ENTRY = "base/assets/index.android.bundle";

function readRequiredEnv(name) {
const value = process.env[name];

if (typeof value !== "string" || value.trim().length === 0) {
throw new Error(`Missing required environment variable: ${name}`);
}

return value.trim();
}

function readJsBundle(aabPath) {
return execFileSync("unzip", ["-p", aabPath, JS_BUNDLE_ENTRY], {
maxBuffer: 256 * 1024 * 1024,
}).toString("latin1");
}

function assertAabAdMobConfig(aabPath) {
if (!fs.existsSync(aabPath)) {
throw new Error(`AAB not found at ${aabPath}`);
}

const expectedUnitIds = [
readRequiredEnv("EXPO_PUBLIC_ADMOB_ANDROID_INTERSTITIAL_ID"),
readRequiredEnv("EXPO_PUBLIC_ADMOB_ANDROID_REWARDED_ID"),
];

const bundle = readJsBundle(aabPath);

const foundSampleId = SAMPLE_AD_UNIT_IDS.find((sampleId) =>
bundle.includes(sampleId)
);
if (foundSampleId) {
throw new Error(
`Shipped AAB embeds a Google sample AdMob ID: ${foundSampleId}`
);
}

const missingUnitId = expectedUnitIds.find(
(unitId) => !bundle.includes(unitId)
);
if (missingUnitId) {
throw new Error(
`Shipped AAB does not embed the production AdMob ID: ${missingUnitId}`
);
}
}

if (require.main === module) {
const aabPath = process.argv[2];

if (!aabPath) {
throw new Error(
"Usage: node scripts/assert-aab-admob-config.js <app-release.aab>"
);
}

assertAabAdMobConfig(aabPath);
console.log("Shipped AAB AdMob validation passed.");
}
Loading
Loading