# Test Apps Build Process Review

## Summary

Both test apps are configured to **automatically build the plugin** as part of their build process. The plugin is included as a Gradle project dependency, so Gradle handles building it automatically.

Note that Native Test App 1 `android-test-app` is used most frequently.

---

## Native Test Apps: `*-test-app` (Standalone native app)

## Native Test App 1: `android-test-app` (Standalone Android)

**Location**: `test-apps/android-test-app/`

### Configuration

**Plugin Reference** (`settings.gradle`):
```gradle
// Reference plugin from root project
def pluginPath = new File(settingsDir.parentFile.parentFile, 'android')
include ':daily-notification-plugin'
project(':daily-notification-plugin').projectDir = pluginPath
```

**Plugin Dependency** (`app/build.gradle`):
```gradle
dependencies {
    implementation project(':capacitor-android')
    implementation project(':daily-notification-plugin')  // ✅ Plugin included
    // Plugin dependencies also included
}
```

**Capacitor Setup**:
- References Capacitor from `daily-notification-test/node_modules/` (shared dependency)
- Includes `:capacitor-android` project module

### Build Process

1. **Gradle resolves plugin project** - Finds plugin at `../../android`
2. **Gradle builds plugin module** - Compiles plugin Java code to AAR (internally)
3. **Gradle builds app module** - Compiles app code
4. **Gradle links plugin** - Includes plugin classes in app APK
5. **Final output**: `app/build/outputs/apk/debug/app-debug.apk`

### Build Commands

Note that these require Java > 22.12

This set is for the most basic Android app:

```bash
cd android-test-app

# Build debug APK (builds plugin automatically)
./gradlew assembleDebug

# Get the full list of available AVDs
avdmanager list avd

# Run one
emulator -avd AVD_NAME

# Simply see that one is running
adb devices

# Now install on the emulator
adb install -r ./app/build/outputs/apk/debug/app-debug.apk

# Now start the app
adb shell am start -n org.timesafari.dailynotification/.MainActivity

# Build release APK
./gradlew assembleRelease
```

### Prerequisites

- ✅ Gradle wrapper present (`gradlew`, `gradlew.bat`, `gradle/wrapper/`)
- ✅ Capacitor must be installed in `daily-notification-test/node_modules/` (shared)
- ✅ Plugin must exist at root `android/` directory

---

## Native Test App 2: `ios-test-app` (Standalone iOS)

**Location**: `test-apps/ios-test-app/`

---

## Vue Test App: `daily-notification-test` (Vue 3 + Capacitor)

This is a Vue app, to illustrate integration with apps like Time Safari.

**Location**: `test-apps/daily-notification-test/`

### Configuration

**Plugin Installation** (`package.json`):
```json
{
  "dependencies": {
    "@timesafari/daily-notification-plugin": "file:../../"
  }
}
```

**Capacitor Auto-Configuration**:
- `npx cap sync android` automatically:
  1. Installs plugin from `file:../../` → `node_modules/@timesafari/daily-notification-plugin/`
  2. Generates `capacitor.settings.gradle` with plugin reference
  3. Generates `capacitor.build.gradle` with plugin dependency
  4. Generates `capacitor.plugins.json` with plugin registration

**Plugin Reference** (`capacitor.settings.gradle` - auto-generated):
```gradle
include ':timesafari-daily-notification-plugin'
project(':timesafari-daily-notification-plugin').projectDir = 
    new File('../node_modules/@timesafari/daily-notification-plugin/android')
```

**Plugin Dependency** (`capacitor.build.gradle` - auto-generated):
```gradle
dependencies {
    implementation project(':timesafari-daily-notification-plugin')
}
```

### Build Process

1. **npm install** - Installs plugin from `file:../../` to `node_modules/`
2. **npm run build** - Builds Vue 3 web app → `dist/`
3. **npx cap sync android** - Capacitor:
   - Copies web assets to `android/app/src/main/assets/`
   - Configures plugin in Gradle files
   - Registers plugin in `capacitor.plugins.json`
4. **Fix script runs** - Verifies plugin path is correct (post-sync hook)
5. **Gradle builds** - Plugin is built as part of app build
6. **Final output**: `android/app/build/outputs/apk/debug/app-debug.apk`

### Build Commands

```bash
cd test-apps/daily-notification-test

# Initial setup (one-time)
npm install                    # Installs plugin from file:../../
npx cap sync android          # Configures Android build

# Development workflow
npm run build                 # Builds Vue 3 web app
npx cap sync android          # Syncs web assets + plugin config
cd android
./gradlew assembleDebug       # Builds Android app (includes plugin)

# Or use Capacitor CLI (does everything)
npx cap run android           # Builds web + syncs + builds Android + runs
```

### Post-Install Hook

The `postinstall` script (`scripts/fix-capacitor-plugins.js`) automatically:
- ✅ Verifies plugin is registered in `capacitor.plugins.json`
- ✅ Verifies plugin path in `capacitor.settings.gradle` points to `android/` (standard structure)
- ✅ Fixes path if it incorrectly points to old `android/plugin/` structure

---

## Key Points

### ✅ Both Apps Build Plugin Automatically

- **No manual plugin build needed** - Gradle handles it
- **Plugin is a project dependency** - Built before the app
- **Standard Gradle behavior** - Works like any Android library module

### ✅ Plugin Structure is Standard

- **Plugin location**: `android/src/main/java/...` (standard Capacitor structure)
- **No path fixes needed** - Capacitor auto-generates correct paths
- **Works with `npx cap sync`** - No manual configuration required

### ✅ Build Dependencies

**android-test-app**:
- Requires Capacitor from `daily-notification-test/node_modules/` (shared)
- References plugin directly from root `android/` directory

**daily-notification-test**:
- Requires `npm install` to install plugin
- Requires `npx cap sync android` to configure build
- Plugin installed to `node_modules/` like any npm package

---

## Verification

### Check Plugin is Included

```bash
# For android-test-app
cd test-apps/android-test-app
./gradlew :app:dependencies | grep daily-notification

# For daily-notification-test
cd test-apps/daily-notification-test/android
./gradlew :app:dependencies | grep timesafari
```

### Check Plugin Registration

```bash
# Vue app only
cat test-apps/daily-notification-test/android/app/src/main/assets/capacitor.plugins.json
```

Should contain:
```json
[
  {
    "name": "DailyNotification",
    "classpath": "org.timesafari.dailynotification.DailyNotificationPlugin"
  }
]
```

---

## Troubleshooting

### android-test-app: "Capacitor not found"

**Solution**: Run `npm install` in `test-apps/daily-notification-test/` first to install Capacitor dependencies.

### android-test-app: "Plugin not found"

**Solution**: Verify `android/build.gradle` exists at the root project level.

### daily-notification-test: Plugin path wrong

**Solution**: Run `node scripts/fix-capacitor-plugins.js` after `npx cap sync android`. The script now verifies/fixes the path to use standard `android/` structure.

### Both: Build succeeds but plugin doesn't work

**Solution**: 
- Check `capacitor.plugins.json` has plugin registered
- Verify plugin classes are in the APK: `unzip -l app-debug.apk | grep DailyNotification`

---

## Summary

✅ **Both test apps handle plugin building automatically**  
✅ **Plugin uses standard Capacitor structure** (`android/src/main/java/`)  
✅ **No manual plugin builds required** - Gradle handles dependencies  
✅ **Build processes are configured correctly** - Ready to use

The test apps are properly configured to build and test the plugin!
