# Building Binaries

This guide covers building iOS and Android binaries for OrchardGo.

## Build Types

### Debug Builds
- Include development tools
- Larger bundle size
- Slower performance
- Not for production

### Release Builds
- Optimized for production
- Smaller bundle size
- Better performance
- Required for app stores

## iOS Builds

### Prerequisites

- Xcode installed (see [.xcode-version](../../.xcode-version))
- Apple Developer account
- Valid provisioning profiles
- Signing certificates

### Building for Development

```bash
# Build and run on simulator
yarn start --platform ios --configuration Debug

# Build for physical device
yarn start --platform ios --device "Your Device Name" --configuration Debug
```

### Building for Release

#### Via Xcode

1. **Open workspace:**
   ```bash
   yarn xcode
   ```

2. **Select scheme:**
   - Product → Scheme → Edit Scheme
   - Change Build Configuration to "Release"

3. **Select target:**
   - Generic iOS Device or specific device

4. **Archive:**
   - Product → Archive
   - Wait for build to complete

5. **Distribute:**
   - Window → Organizer
   - Select your archive
   - Click "Distribute App"
   - Follow wizard for App Store, TestFlight, or Ad Hoc

#### Via Command Line

```bash
# Build release IPA
cd ios
xcodebuild archive \
  -workspace orchardgo.xcworkspace \
  -scheme orchardgo \
  -archivePath orchardgo.xcarchive \
  -configuration Release

# Export IPA
xcodebuild -exportArchive \
  -archivePath orchardgo.xcarchive \
  -exportPath . \
  -exportOptionsPlist ExportOptions.plist
```

### iOS Build Configuration

#### Info.plist

Key configurations in `ios/orchardgo/Info.plist`:
- Bundle identifier
- Display name
- Version number
- Build number
- URL schemes
- Required device capabilities
- Permissions (camera, location, etc.)

#### Podfile

CocoaPods dependencies in `ios/Podfile`:
- React Native pods
- Firebase pods
- Third-party libraries

Update pods after changes:
```bash
cd ios
pod install
```

## Android Builds

### Prerequisites

- Android Studio installed
- Java Development Kit (see [.java-version](../../.java-version))
- Android SDK
- Signing keystore

### Building for Development

```bash
# Build and run debug APK
yarn start --platform android

# Install on specific device
adb -s [DEVICE_ID] install app-debug.apk
```

### Building for Release

#### Generate Keystore (First Time Only)

```bash
# Generate release keystore
keytool -genkeypair \
  -v \
  -keystore orchardgo-release.keystore \
  -alias orchardgo \
  -keyalg RSA \
  -keysize 2048 \
  -validity 10000
```

Store keystore securely and never commit to repository!

#### Configure Signing

Edit `android/gradle.properties`:
```properties
ORCHARDGO_RELEASE_STORE_FILE=orchardgo-release.keystore
ORCHARDGO_RELEASE_KEY_ALIAS=orchardgo
ORCHARDGO_RELEASE_STORE_PASSWORD=your_store_password
ORCHARDGO_RELEASE_KEY_PASSWORD=your_key_password
```

#### Build Release APK

```bash
# Clean and build
cd android
./gradlew clean
./gradlew assembleRelease

# APK location:
# android/app/build/outputs/apk/release/app-release.apk
```

#### Build Release AAB (For Play Store)

```bash
# Build Android App Bundle
cd android
./gradlew bundleRelease

# AAB location:
# android/app/build/outputs/bundle/release/app-release.aab
```

### Android Build Configuration

#### build.gradle

Key configurations in `android/app/build.gradle`:
- Application ID
- Version code
- Version name
- Min/target SDK versions
- Build types (debug/release)
- Signing configs

#### google-services.json

Firebase configuration file. Generate dynamically:
```bash
yarn apply:google-services --brand orchard --env prod
```

See [Push Notifications Guide](../guides/push-notifications.md) for details.

## Multi-Brand Builds

OrchardGo supports multiple brands (Orchard, Awal).

### iOS Brand Configuration

Brand-specific resources in:
```
brands/
├── orchard/
│   ├── icons/
│   ├── images/
│   └── GoogleService-Info.plist
└── awal/
    ├── icons/
    ├── images/
    └── GoogleService-Info.plist
```

Build for specific brand:
```bash
yarn start --platform ios --brand awal
```

### Android Brand Configuration

Similar structure for Android. Build for specific brand:
```bash
yarn start --platform android --brand awal
```

## Environment-Specific Builds

### QA Builds

```bash
# iOS QA
yarn start --platform ios --env qa --configuration Release

# Android QA
yarn start --platform android --env qa --variant release
```

### Production Builds

```bash
# iOS Production
yarn start --platform ios --env prod --configuration Release

# Android Production
yarn start --platform android --env prod --variant release
```

## Build Optimization

### iOS Optimization

1. **Enable Bitcode** (if required)
   - Build Settings → Enable Bitcode → YES

2. **Optimize Swift**
   - Build Settings → Swift Compilation Mode → Optimize for Speed

3. **Strip Debug Symbols**
   - Build Settings → Strip Debug Symbols During Copy → YES (Release)

### Android Optimization

1. **Enable ProGuard/R8**
   ```gradle
   buildTypes {
       release {
           minifyEnabled true
           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
       }
   }
   ```

2. **Enable Shrinking**
   ```gradle
   buildTypes {
       release {
           shrinkResources true
       }
   }
   ```

3. **Split APKs by ABI**
   ```gradle
   splits {
       abi {
           enable true
           reset()
           include "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
       }
   }
   ```

## Build Scripts

### Deploy Binary

```bash
# Deploy native binary to app stores
yarn deploy:binary
```

This script:
1. Builds release binary
2. Uploads to App Store Connect / Play Console
3. Creates release notes
4. Notifies team

See script for platform-specific options.

## Versioning

### Version Numbers

Update version in `package.json`:
```json
{
  "version": "2.18.0"
}
```

This updates:
- iOS: CFBundleShortVersionString
- Android: versionName

### Build Numbers

Build numbers auto-increment per build:
- iOS: CFBundleVersion
- Android: versionCode

## Testing Builds

### TestFlight (iOS)

1. Build release archive
2. Upload to App Store Connect
3. Select for TestFlight
4. Add testers
5. Send for testing

### Internal Testing (Android)

1. Build release AAB
2. Upload to Play Console
3. Create release in Internal Testing track
4. Add testers
5. Send for testing

## Troubleshooting

### iOS Build Failures

#### "Code signing error"
- Verify certificates are installed
- Check provisioning profiles
- Ensure bundle ID matches

#### "Pod install failed"
```bash
cd ios
rm -rf Pods Podfile.lock
pod install --repo-update
```

#### "Xcode version mismatch"
- Install correct Xcode version (see [.xcode-version](../../.xcode-version))
- Run: `sudo xcode-select -s /Applications/Xcode.app`

### Android Build Failures

#### "Gradle sync failed"
```bash
cd android
./gradlew clean
rm -rf .gradle
./gradlew assembleRelease
```

#### "Keystore not found"
- Verify keystore path in gradle.properties
- Ensure keystore file exists
- Check keystore password

#### "SDK not found"
```bash
# Set ANDROID_HOME
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
```

## Build Artifacts

### iOS Artifacts
- `.app` - Application bundle
- `.ipa` - Installable package
- `.dSYM` - Debug symbols (for Sentry)

### Android Artifacts
- `.apk` - Android Package (direct install)
- `.aab` - Android App Bundle (Play Store)
- `mapping.txt` - ProGuard mapping (for Sentry)

## CI/CD Builds

Builds are automated in Jenkins:
- Pull request builds (debug)
- Nightly builds (debug + release)
- Release builds (production)

See [CI/CD Documentation](../reference/ci-cd.md) for details.

## Next Steps

- [CodePush Deployment](./codepush.md)
- [Release Process](./release-process.md)
- [App Store Submission](./app-store-submission.md)
