# Panduan Build APK Android - DMS Sales App

## Prasyarat

Pastikan Anda sudah menginstall:
- Node.js (v14 atau lebih baru)
- Java Development Kit (JDK) 11 atau 17
- Android Studio (untuk build dengan GUI)
- Gradle (biasanya sudah include di Android Studio)

## Script NPM yang Tersedia

### 1. Build React App
```bash
npm run build
```
Build production React app ke folder `build/`

### 2. Sync dengan Capacitor
```bash
npm run cap:sync
```
Sync semua platform (iOS & Android)

```bash
npm run cap:sync:android
```
Sync hanya Android

### 3. Buka Android Studio
```bash
npm run cap:open:android
```
Membuka project Android di Android Studio

### 4. Build dan Buka Android Studio
```bash
npm run android:open
```
Build React app, sync dengan Android, dan buka Android Studio

### 5. Build APK Debug (Command Line)
```bash
npm run android:apk
```
Build React app, sync, dan generate APK debug secara otomatis
APK akan tersimpan di: `android/app/build/outputs/apk/debug/app-debug.apk`

### 6. Build APK Debug (Manual)
```bash
npm run android:apk:debug
```
Hanya build APK debug (tanpa build React app terlebih dahulu)

### 7. Build APK Release
```bash
npm run android:apk:release
```
Build APK release (production)
APK akan tersimpan di: `android/app/build/outputs/apk/release/app-release-unsigned.apk`

## Cara Tercepat Build APK

### Untuk Testing (Debug APK):
```bash
npm run android:apk
```

Setelah selesai, APK ada di:
```
android/app/build/outputs/apk/debug/app-debug.apk
```

### Untuk Production (Release APK):
```bash
npm run build
npm run cap:sync:android
npm run android:apk:release
```

Setelah selesai, APK ada di:
```
android/app/build/outputs/apk/release/app-release-unsigned.apk
```

## Build dengan Android Studio

1. Build dan sync:
```bash
npm run android:build
```

2. Buka Android Studio:
```bash
npm run cap:open:android
```

3. Di Android Studio:
   - Tunggu Gradle sync selesai
   - Pilih menu: **Build → Build Bundle(s) / APK(s) → Build APK(s)**
   - Tunggu proses build selesai
   - Klik "locate" untuk membuka folder APK

## Signing APK untuk Production

Untuk release APK yang bisa dipublish ke Play Store, Anda perlu signing dengan keystore:

### 1. Generate Keystore (sekali saja):
```bash
keytool -genkey -v -keystore dms-sales-app.keystore -alias dms-sales -keyalg RSA -keysize 2048 -validity 10000
```

### 2. Konfigurasi Signing di `android/app/build.gradle`:

Tambahkan sebelum `android {`:
```gradle
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
```

Tambahkan di dalam `android { ... }`:
```gradle
signingConfigs {
    release {
        keyAlias keystoreProperties['keyAlias']
        keyPassword keystoreProperties['keyPassword']
        storeFile file(keystoreProperties['storeFile'])
        storePassword keystoreProperties['storePassword']
    }
}

buildTypes {
    release {
        signingConfig signingConfigs.release
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
```

### 3. Buat file `android/keystore.properties`:
```properties
storeFile=../dms-sales-app.keystore
storePassword=YOUR_STORE_PASSWORD
keyAlias=dms-sales
keyPassword=YOUR_KEY_PASSWORD
```

### 4. Build Signed APK:
```bash
npm run android:apk:release
```

APK signed akan tersimpan di: `android/app/build/outputs/apk/release/app-release.apk`

## Troubleshooting

### Error: Gradle command not found
Pastikan Android Studio sudah terinstall dan Gradle sudah ada di PATH, atau gunakan:
```bash
cd android
./gradlew assembleDebug
```

### Error: Java version
Pastikan menggunakan JDK 11 atau 17:
```bash
java -version
```

### Error: Build failed
Coba clean build:
```bash
cd android
./gradlew clean
./gradlew assembleDebug
```

### APK tidak bisa diinstall
- Pastikan "Install from Unknown Sources" diaktifkan di Android
- Untuk release APK, pastikan sudah di-sign dengan keystore

## Install APK ke Device

### Via USB (ADB):
```bash
adb install android/app/build/outputs/apk/debug/app-debug.apk
```

### Via File Transfer:
1. Copy APK ke device
2. Buka file manager di device
3. Tap APK file
4. Izinkan install dari unknown sources
5. Install

## Update Capacitor Config

Jika perlu update konfigurasi app, edit `capacitor.config.json`:
```json
{
  "appId": "com.dms.sales",
  "appName": "DMS Sales",
  "webDir": "build",
  "bundledWebRuntime": false
}
```

Setelah update, jalankan:
```bash
npm run cap:sync:android
```

## Tips

1. Selalu build React app terlebih dahulu sebelum sync ke Android
2. Gunakan debug APK untuk testing
3. Gunakan release APK yang sudah di-sign untuk production
4. Simpan keystore file dengan aman (jangan commit ke git)
5. Tambahkan `*.keystore` dan `keystore.properties` ke `.gitignore`

## Struktur Output APK

```
android/app/build/outputs/apk/
├── debug/
│   └── app-debug.apk          (untuk testing)
└── release/
    └── app-release.apk        (untuk production, setelah signing)
```
