# Feasibility Study & Architecture Plan: AI-Powered APK Builder

This document summarizes the technical feasibility, infrastructure requirements, financial planning, and architectural options for launching the **Internal Cursor AI-to-APK Builder** as a public SaaS product.

---

## 1. Product Vision & Market Niche

### The Problem
Setting up a local mobile development environment (Android Studio, Java JDK, Android SDK, and Flutter) is highly complex, takes hours, and is intimidating for beginners, designers, and non-technical entrepreneurs.

### The Solution
A web or mobile application where users can:
1. **Chat with an AI** to describe their application idea.
2. **Edit the generated code** directly in a lightweight web/mobile editor.
3. **Compile to APK with one click** and instantly download the compiled binary to their phone.

---

## 2. Technical Architecture Options

We analyzed three potential architectural paths for this product:

### Option A: Hybrid Cloud Model (Web/Mobile UI + Cloud Compiler) — 🌟 Recommended
The frontend (a Web IDE or a Flutter mobile app) serves as the interface for AI chatting and code editing. The heavy compilation is outsourced to a cloud server.

* **How it works:** When a user clicks "Build APK", the code is packaged and sent to a secure cloud build server. The server compiles the APK and returns a download link.
* **Pros:** Users can build real Android apps from any device (including smartphones/tablets) without installing any SDKs.
* **Cons:** Requires maintaining and securing cloud build servers.

### Option B: Local Development Setup (Zero Server Cost)
The IDE runs locally on the user's PC (via a local PHP server like `php -S localhost:8000`), using their local CPU, RAM, and SDKs.

* **Pros:** $0 hosting cost for the developer. No security sandboxing required.
* **Cons:** Users must manually install Flutter, Java, and the Android SDK on their computers.

### Option C: Standalone Mobile Compilation (Not Feasible)
Compiling the Flutter app directly on the user's smartphone without a server.

* **Pros:** None, as it is technically impractical.
* **Cons:** Android devices lack the official tooling to run the full Gradle and Android compiler chains efficiently. iOS completely forbids compiling and executing arbitrary binaries in their sandbox.

---

## 3. Hosting & Infrastructure Analysis

### Why Managed/Shared Hosting (e.g., Hostinger Cloud) is Insufficient
Even though premium Cloud Hosting plans offer up to 3 GB of RAM and 2 CPU Cores, they cannot be used for this project because:
1. **No Root Access:** Installing the Java JDK and Android SDK requires system-level administrator (`sudo`) access.
2. **Disabled Functions:** Managed hosts disable PHP execution functions (`shell_exec`, `exec`, `proc_open`) for security.
3. **Process Limits:** Spawning Gradle and Java compilers quickly exceeds the "Max Processes" limit (usually 120), causing the server to kill the build.

### Recommended VPS Providers (For the Cloud Compiler)
To run the compiler in the cloud, you need a **Virtual Private Server (VPS)** running Ubuntu Linux.

| Provider | Plan | Monthly Cost | Specs | Verdict |
| :--- | :--- | :--- | :--- | :--- |
| **Hetzner** | CX22 | **~$3.60** (€3.29) | 2 vCPU, 4 GB RAM, 40 GB SSD | **Cheapest high-performance option.** |
| **Hostinger** | KVM 1 | **~$4.99 – $5.99** | 1 vCPU, 4 GB RAM, 50 GB NVMe | Great to keep under your existing Hostinger account. |
| **Contabo** | Cloud VPS S | **~$6.00** | 4 vCPU, 8 GB RAM, 50 GB NVMe | **Best value.** Highly recommended for fast builds. |
| **AWS** | Lightsail | **$10.00** | 1 vCPU, 2 GB RAM, 60 GB SSD | Good if you want to stay in the AWS ecosystem. |
| **AWS** | Free Tier (`t2.micro`) | **$0.00** (12 months) | 1 vCPU, 1 GB RAM, 30 GB SSD | **Requires a 3GB Swap File** or it will crash. |

---

## 4. Disk Space & Resource Management

A major concern with Android/Gradle compilation is disk bloat. A standard build server can be kept clean using these strategies:

1. **Initial Footprint (~8–12 GB):**
   * Ubuntu OS: ~3 GB
   * Flutter SDK: ~1.5 GB
   * Android SDK (API 34 only): ~2.5 GB
   * Java JDK 17: ~0.3 GB
   * This leaves **18 to 38 GB free** on a standard 30–50 GB VPS disk.
2. **Automatic Cleanups:**
   * **`flutter clean`:** Run this automatically before or after every build. It deletes the temporary `build/` directory, saving up to 1 GB per project.
   * **Gradle Cache Pruning:** Schedule a monthly cron job to clear the Gradle cache folder (`rm -rf ~/.gradle/caches/`). Gradle will only re-download active dependencies on the next build.

---

## 5. Financial Model & Pricing Strategy

### Phase 1: Development & Private Beta (1–50 Users)
* **Goal:** Build the prototype, refine the AI prompts, and test compilation.
* **Monthly Budget:** **~$5.00 – $7.00** (Cheap VPS + Domain + Free Gemini API tier).

### Phase 2: Public Launch (50–500+ Users)
* **Goal:** Secure the server using Docker sandboxing, set up a build queue, and charge users.
* **Monthly Budget:** **~$30.00 – $65.00** (Web server, build server, paid Gemini API, email service).

### Revenue & Profit Projections (At $9.99/month subscription)
* **5 subscribers:** **$50/mo** — Breaks even.
* **20 subscribers:** **$200/mo** — Covers all costs with profit.
* **100 subscribers:** **$1,000/mo** — **~$930+ net profit.**

---

## 6. Implementation Roadmap

### Step 1: Run and test locally on Windows
Run the PHP server locally on your Windows PC using your local Flutter and Android SDKs:
```powershell
cd c:\Users\Bai Jue\Desktop\cursor\backend
php -S localhost:8000
```
Open `http://localhost:8000` to test the AI editor and APK generation.

### Step 2: Get a VPS and run the Automated Setup Script
When ready to move to the cloud, buy a VPS (Ubuntu 22.04/24.04 LTS), upload the automated setup script (`setup_flutter_server.sh`), and run:
```bash
sudo bash setup_flutter_server.sh
```
This will automatically install Java, Android SDK, Flutter, accept all licenses, and configure permissions.

### Step 3: Secure and Scale (For Public Release)
1. **Containerization:** Wrap the compilation process inside temporary **Docker** containers.
2. **Queues:** Implement a queue system so the server compiles one APK at a time to prevent CPU overload.
