-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Android+Vulkan example #9266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SahilM9098
wants to merge
2
commits into
ocornut:master
Choose a base branch
from
SahilM9098:feature/android-vulkan-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Android+Vulkan example #9266
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
|    | ||
|
|
||
| ## 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
15
examples/example_android_vulkan/app/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
32
examples/example_android_vulkan/app/src/main/cpp/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DDoS Attack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK