Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/example_android_vulkan/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
102 changes: 102 additions & 0 deletions examples/example_android_vulkan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Android Vulkan + Dear ImGui Example

A standalone Android application that renders [Dear ImGui](https://github.com/ocornut/imgui) using the Vulkan graphics API. It is built natively in C++ via `NativeActivity`, alongside a minimal Java subclass to handle OS-level features like the virtual keyboard and clipboard.

![ImGui Demo](https://img.shields.io/badge/ImGui-1.92.6-blue) ![Vulkan](https://img.shields.io/badge/Vulkan-1.0-red) ![Android](https://img.shields.io/badge/Android-API%2026+-green)

## Features

- **Native focus** — `NativeActivity` with `android_native_app_glue` to handle the main loop purely in C++.
- **Keyboard & Clipboard** — Includes a minimal `MainActivity.java` subclass to provide JNI hooks for the Android soft keyboard and native `ClipboardManager`.
- **Vulkan rendering** — Instance, Device, Swapchain, RenderPass all managed via ImGui helpers
- **Touch input** — `imgui_impl_android.h` handles multi-touch natively
- **Orientation handling** — Swapchain auto-rebuilds on rotation
- **Modular code** — Clean separation into `vulkan_helper.h`, `menu.h`, and `main.cpp`

## Requirements

| Requirement | Version |
| -------------- | ---------------- |
| Android Studio | 2024.x+ |
| Android NDK | 29.0.x |
| CMake | 3.22.1 |
| Min SDK | 26 (Android 8.0) |
| Target SDK | 36 |
| ABI | `arm64-v8a` |

## Project Structure

```
app/src/main/
├── AndroidManifest.xml ← Configured to use MainActivity
├── java/imgui/example/android/
│ └── MainActivity.java ← Minimal NativeActivity subclass for Keyboard & Clipboard JNI
└── cpp/
├── CMakeLists.txt ← Build config (links imgui + vulkan)
├── main.cpp ← Entry point: lifecycle (Init, MainLoopStep, Shutdown)
├── vulkan_helper.h ← vkh:: namespace — Vulkan setup, rendering, cleanup
└── menu.h ← menu:: namespace — Your ImGui UI (edit this!)
```

ImGui sources are referenced from the parent imgui directory (`../../../../../../`).

## Build & Run

```bash
# Build
./gradlew assembleDebug

# Build + install on connected device
./gradlew installDebug

# View logs
adb logcat -s ImGuiVulkan
```

## Customizing the UI

Edit **`menu.h`** — the `menu::Draw()` function is called every frame:

```cpp
namespace menu
{
inline void Draw()
{
ImGui::Begin("My App");
ImGui::Text("Hello!");
// Add your widgets here...
ImGui::End();
}
}
```

All ImGui widgets work out of the box: windows, buttons, sliders, checkboxes, color pickers, popups, combo boxes, etc.

## How It Works

1. **`android_main()`** — NativeActivity entry point, runs the event + render loop
2. **`Init()`** — Called on `APP_CMD_INIT_WINDOW`:
- Creates `VkInstance`, `VkDevice`, `VkQueue`
- Creates `VkSurfaceKHR` from `ANativeWindow`
- Sets up swapchain, render pass, framebuffers via `ImGui_ImplVulkanH_Window`
- Initializes Dear ImGui with `imgui_impl_android` + `imgui_impl_vulkan`
3. **`MainLoopStep()`** — Called every frame:
- Checks for orientation/size changes → rebuilds swapchain if needed
- Calls `ImGui_ImplVulkan_NewFrame()` + `ImGui_ImplAndroid_NewFrame()`
- Calls `menu::Draw()` for your UI
- Records Vulkan command buffer + presents
4. **`Shutdown()`** — Called on `APP_CMD_TERM_WINDOW`, cleans up everything

## Key Files Reference

| File | What to modify |
| --------------------- | ----------------------------------------------- |
| `menu.h` | UI widgets — add buttons, sliders, windows |
| `vulkan_helper.h` | Vulkan settings (present mode, min image count) |
| `main.cpp` | App lifecycle, ImGui style/scaling, JNI calls |
| `MainActivity.java` | Android soft keyboard and clipboard handling |
| `AndroidManifest.xml` | App name, permissions, orientation |

## License

This example uses [Dear ImGui](https://github.com/ocornut/imgui) which is licensed under the MIT License.
1 change: 1 addition & 0 deletions examples/example_android_vulkan/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DDoS Attack

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

44 changes: 44 additions & 0 deletions examples/example_android_vulkan/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
alias(libs.plugins.android.application)
}

android {
namespace = "imgui.example.android"
compileSdk = 36

defaultConfig {
applicationId = "imgui.example.android"
minSdk = 26
targetSdk = 36
versionCode = 1
versionName = "1.0"

ndk{
abiFilters += "arm64-v8a"
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
externalNativeBuild {
cmake {
path = file("src/main/cpp/CMakeLists.txt")
version = "3.22.1"
}
}
ndkVersion = "29.0.14206865"
}

dependencies {
}
21 changes: 21 additions & 0 deletions examples/example_android_vulkan/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
15 changes: 15 additions & 0 deletions examples/example_android_vulkan/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true">

<activity android:name=".MainActivity" android:exported="true" android:configChanges="orientation|screenSize|keyboardHidden">
<meta-data android:name="android.app.lib_name" android:value="androidvulkanimgui"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
32 changes: 32 additions & 0 deletions examples/example_android_vulkan/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.22.1)

project(androidvulkanimgui)

set(IMGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../)

include_directories(
${IMGUI_DIR}
${IMGUI_DIR}/backends
${ANDROID_NDK}/sources/android/native_app_glue
)

set(IMGUI_SOURCES
${IMGUI_DIR}/imgui.cpp
${IMGUI_DIR}/imgui_draw.cpp
${IMGUI_DIR}/imgui_tables.cpp
${IMGUI_DIR}/imgui_widgets.cpp
${IMGUI_DIR}/imgui_demo.cpp
${IMGUI_DIR}/backends/imgui_impl_android.cpp
${IMGUI_DIR}/backends/imgui_impl_vulkan.cpp
)

add_library(${CMAKE_PROJECT_NAME} SHARED
main.cpp
${IMGUI_SOURCES}
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c
)

target_link_libraries(${CMAKE_PROJECT_NAME}
android
log
vulkan)
Loading