|
| 1 | +# Android HTTP Server |
| 2 | + |
| 3 | +A sample Android application that demonstrates how to run an embedded HTTP server inside an Android app using Ktor CIO engine. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Embedded HTTP server running on Android device |
| 8 | +- RESTful API with status endpoint |
| 9 | +- Auto-generated Swagger UI documentation |
| 10 | +- Static file serving from assets |
| 11 | +- CORS support |
| 12 | +- Custom error handling |
| 13 | +- Foreground service for background operation |
| 14 | +- Real-time server status updates |
| 15 | + |
| 16 | +## Tech Stack |
| 17 | + |
| 18 | +- **Kotlin** - Primary programming language |
| 19 | +- **Ktor CIO** - Embedded HTTP server engine |
| 20 | +- **Jetpack Compose** - Modern Android UI toolkit |
| 21 | +- **Kotlinx Serialization** - JSON serialization |
| 22 | +- **OpenAPI/Swagger** - API documentation |
| 23 | + |
| 24 | +## Dependencies |
| 25 | + |
| 26 | +The project uses the following key dependencies: |
| 27 | + |
| 28 | +### Project level build.gradle.kts |
| 29 | +```kotlin |
| 30 | +plugins { |
| 31 | + // add for serialization support (optional) |
| 32 | + id ("org.jetbrains.kotlin.plugin.serialization") version "2.2.10" |
| 33 | +} |
| 34 | + |
| 35 | +``` |
| 36 | + |
| 37 | +### Ktor Server Components |
| 38 | +```kotlin |
| 39 | +implementation("io.ktor:ktor-server-cio:3.2.3") |
| 40 | +implementation("io.ktor:ktor-server-core:3.2.3") |
| 41 | +implementation("io.ktor:ktor-server-content-negotiation:3.2.3") // (optional) |
| 42 | +implementation("io.ktor:ktor-server-call-logging:3.2.3") // (optional) |
| 43 | +implementation("io.ktor:ktor-server-cors:3.2.3") // (optional) |
| 44 | +implementation("io.ktor:ktor-server-status-pages:3.2.3") // (optional) |
| 45 | +implementation("io.ktor:ktor-serialization-kotlinx-json:3.2.3") // (optional) |
| 46 | +``` |
| 47 | + |
| 48 | +### OpenAPI and Swagger (optional) |
| 49 | +```kotlin |
| 50 | +implementation("io.github.smiley4:ktor-openapi:5.2.0") |
| 51 | +implementation("io.github.smiley4:ktor-swagger-ui:5.2.0") |
| 52 | +``` |
| 53 | +## Required Permissions |
| 54 | + |
| 55 | +Add these permissions to your `AndroidManifest.xml`: |
| 56 | + |
| 57 | +```xml |
| 58 | +<uses-permission android:name="android.permission.INTERNET" /> |
| 59 | +<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
| 60 | +<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> |
| 61 | +<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> |
| 62 | +<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> |
| 63 | +``` |
| 64 | + |
| 65 | +## Service Configuration |
| 66 | + |
| 67 | +Register the HTTP server service in your manifest: |
| 68 | + |
| 69 | +```xml |
| 70 | +<service |
| 71 | + android:name=".HttpServerService" |
| 72 | + android:enabled="true" |
| 73 | + android:exported="false" |
| 74 | + android:foregroundServiceType="specialUse" /> |
| 75 | +``` |
| 76 | + |
| 77 | +## Project Structure |
| 78 | + |
| 79 | +``` |
| 80 | +app/src/main/java/com/dihax/androidhttpserver/ |
| 81 | +├── MainActivity.kt # Main activity with Compose UI |
| 82 | +├── HttpServerService.kt # Foreground service for HTTP server |
| 83 | +├── MyApplication.kt # Application class |
| 84 | +└── server/ |
| 85 | + ├── Server.kt # Ktor server configuration |
| 86 | + ├── Models.kt # Data models and API responses |
| 87 | + └── Utils.kt # Utility functions |
| 88 | +
|
| 89 | +app/src/main/assets/web/ |
| 90 | +├── index.html # Landing page |
| 91 | +└── style.css # Styles for web interface |
| 92 | +``` |
| 93 | + |
| 94 | +## Key Components |
| 95 | + |
| 96 | +### 1. HTTP Server Service (HttpServerService.kt) |
| 97 | + |
| 98 | +Manages the Ktor server lifecycle as a foreground service: |
| 99 | + |
| 100 | +- Starts/stops the HTTP server on demand |
| 101 | +- Runs as foreground service with notifications |
| 102 | +- Communicates server status to UI via SharedFlow |
| 103 | +- Handles service binding for UI interaction |
| 104 | + |
| 105 | +### 2. Server Configuration (Server.kt) |
| 106 | + |
| 107 | +Configures the Ktor server with: |
| 108 | + |
| 109 | +- **Content Negotiation**: JSON serialization with Kotlinx |
| 110 | +- **CORS**: Cross-origin resource sharing |
| 111 | +- **Status Pages**: Custom error handling |
| 112 | +- **Call Logging**: Request logging for API endpoints |
| 113 | +- **OpenAPI**: Auto-generated API documentation |
| 114 | +- **Static Files**: Serves files from assets/web directory |
| 115 | + |
| 116 | +### 3. API Endpoints |
| 117 | + |
| 118 | +- `GET /api/status` - Health check endpoint |
| 119 | +- `GET /api/json` - OpenAPI JSON specification |
| 120 | +- `GET /api/swagger` - Swagger UI interface |
| 121 | +- `GET /` - Redirects to index.html |
| 122 | +- `GET /{file...}` - Static file serving |
| 123 | + |
| 124 | +## Installation and Setup |
| 125 | + |
| 126 | +1. **Clone the repository** |
| 127 | + ```bash |
| 128 | + git clone <repository-url> |
| 129 | + cd android-http-server |
| 130 | + ``` |
| 131 | + |
| 132 | +2. **Open in Android Studio** |
| 133 | + - Import the project in Android Studio |
| 134 | + - Sync Gradle files |
| 135 | + |
| 136 | +3. **Build and run** |
| 137 | + - Connect Android device or start emulator |
| 138 | + - Build and install the app |
| 139 | + |
| 140 | +## Usage |
| 141 | + |
| 142 | +1. **Grant Permissions** |
| 143 | + - Open the app |
| 144 | + - Tap "Grant Permissions" to allow notifications |
| 145 | + |
| 146 | +2. **Start Server** |
| 147 | + - Enter desired port number (default: 8080) |
| 148 | + - Tap "Start Server" |
| 149 | + - Server runs in background with notification |
| 150 | + |
| 151 | +3. **Access Server** |
| 152 | + - Copy the server address shown in the app |
| 153 | + - Open in any web browser on the same network |
| 154 | + - Access API documentation at `/api/swagger` |
| 155 | + |
| 156 | +4. **Stop Server** |
| 157 | + - Tap "Stop Server" when finished |
| 158 | + |
| 159 | +## Network Access |
| 160 | + |
| 161 | +The server binds to all network interfaces, making it accessible from: |
| 162 | +- The device itself (localhost) |
| 163 | +- Other devices on the same WiFi network |
| 164 | +- Any client that can reach the device's IP address |
| 165 | + |
| 166 | +## API Documentation |
| 167 | + |
| 168 | +When the server is running, visit `/api/swagger` for interactive API documentation generated from OpenAPI specifications. |
| 169 | + |
| 170 | +## Building for Production |
| 171 | + |
| 172 | +The app includes proper resource exclusions for Ktor OpenAPI library to ensure successful builds: |
| 173 | + |
| 174 | +```kotlin |
| 175 | +packaging { |
| 176 | + resources { |
| 177 | + excludes += arrayOf( |
| 178 | + "META-INF/ASL-2.0.txt", |
| 179 | + "draftv4/schema", |
| 180 | + "META-INF/DEPENDENCIES", |
| 181 | + // ... other exclusions |
| 182 | + ) |
| 183 | + } |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +## Security Considerations |
| 188 | + |
| 189 | +- Server runs on local network only |
| 190 | +- No authentication implemented (for demo purposes) |
| 191 | +- Consider adding authentication for production use |
| 192 | +- Firewall rules may affect external access |
| 193 | + |
| 194 | +## Requirements |
| 195 | + |
| 196 | +- Android API Level 24+ (Android 7.0) |
| 197 | +- Network connectivity (WiFi or mobile data) |
| 198 | +- Storage for temporary files (if needed) |
| 199 | + |
| 200 | +## Contributing |
| 201 | + |
| 202 | +This is a sample project demonstrating embedded HTTP server capabilities in Android. Feel free to fork and modify for your specific use case. |
| 203 | + |
| 204 | +## License |
| 205 | + |
| 206 | +This project is provided as-is for educational and demonstration purposes. |
0 commit comments