File size: 4,438 Bytes
e1f81b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env zx

import "zx/globals"
import {readFile, writeFile} from "fs/promises"
import {setBuildEnv} from './set-build-env.mjs';
import {syncAutolinkingCache} from './clear-autolinking-cache.mjs';

// build only for real devices new arch:
process.env.ORG_GRADLE_PROJECT_reactNativeArchitectures = 'arm64-v8a'

// Optional --name <suffix> — produces a parallel-installable build with a
// suffixed package name and matching app label. Validation lives in
// app.config.ts (which reads MENTRAOS_BUILD_NAME). e.g.
//   bun android-release --name stable
//   → applicationId: com.mentra.mentra.stable
//   → app label:     stable
// Set the suffix before setBuildEnv / prebuild so react-native.config.js and
// app.config.ts agree on the package. Autolinking is checked once, after
// prebuild, so a stale base-package cache cannot survive into assembleRelease.
const nameSuffix = argv.name ? String(argv.name).trim() : null
if (nameSuffix) {
  process.env.MENTRAOS_BUILD_NAME = nameSuffix
}
await setBuildEnv({syncAutolinking: false});

console.log('Building Android release...');
if (nameSuffix) {
  console.log(`  Variant: MENTRAOS_BUILD_NAME=${nameSuffix}`)
}

// Prebuild Android (reads MENTRAOS_BUILD_NAME via app.config.ts)
await $({ stdio: 'inherit' })`bun expo prebuild --platform android`;

// Authoritative post-prebuild guard (same as android.mjs): compare the resolved
// graph and generated applicationId to the cached artifact, then wipe if a
// suffix / region switch left ReactNativeApplicationEntryPoint on the old package.
await syncAutolinkingCache();

// Patch the build-time copy of google-services.json to include a client entry
// for the suffixed package, since Firebase only knows about the base package.
// The cloned entry reuses the base Firebase app ID — fine for local/dev builds.
if (nameSuffix) {
  const gsPath = 'android/app/google-services.json'
  const gs = JSON.parse(await readFile(gsPath, 'utf-8'))
  const newPkg = `com.mentra.mentra.${nameSuffix}`
  const baseClient = gs.client?.find(
    (c) => c.client_info?.android_client_info?.package_name === 'com.mentra.mentra',
  )
  const alreadyHas = gs.client?.some(
    (c) => c.client_info?.android_client_info?.package_name === newPkg,
  )
  if (baseClient && !alreadyHas) {
    const clone = JSON.parse(JSON.stringify(baseClient))
    clone.client_info.android_client_info.package_name = newPkg
    gs.client.push(clone)
    await writeFile(gsPath, JSON.stringify(gs, null, 2))
  }
}

// bundle js code:
await $({stdio: "inherit"})`bun expo export --platform android --clear`

// Build release APK
await $({ stdio: 'inherit', cwd: 'android' })`./gradlew assembleRelease`;

// Install APK on device. Prefer ANDROID_SERIAL; otherwise pick a phone when
// Mentra Live glasses are also attached (adb fails on "more than one device").
const apkPath = 'android/app/build/outputs/apk/release/app-release.apk'
const serial = await resolveAdbSerial()
console.log(`Installing APK on ${serial}...`)
await $({stdio: 'inherit'})`adb -s ${serial} install -r ${apkPath}`

console.log('✅ Android release built and installed successfully!');
if (nameSuffix) {
  console.log(`   Package: com.mentra.mentra.${nameSuffix}`)
  console.log(`   App label: ${nameSuffix}`)
}

async function resolveAdbSerial() {
  if (process.env.ANDROID_SERIAL?.trim()) {
    return process.env.ANDROID_SERIAL.trim()
  }

  const {stdout} = await $`adb devices -l`
  const devices = stdout
    .split('\n')
    .slice(1)
    .map((line) => line.trim())
    .filter((line) => line && !line.startsWith('*'))
    .map((line) => {
      const parts = line.split(/\s+/)
      return {serial: parts[0], state: parts[1], raw: line}
    })
    .filter((d) => d.state === 'device')

  if (devices.length === 0) {
    throw new Error('No adb devices ready. Connect a phone and retry.')
  }
  if (devices.length === 1) {
    return devices[0].serial
  }

  const phones = devices.filter((d) => !/MentraLive|Mentra_Live/i.test(d.raw))
  if (phones.length === 1) {
    console.log(
      `Multiple adb devices; installing on phone ${phones[0].serial} (skipping Mentra Live)`,
    )
    return phones[0].serial
  }

  const list = devices.map((d) => `  ${d.raw}`).join('\n')
  throw new Error(
    `Multiple adb devices; set ANDROID_SERIAL to choose one:\n${list}`,
  )
}