From d51177e8668160d73de5edbd35b4e2f180777445 Mon Sep 17 00:00:00 2001 From: Junrou Nishida Date: Sun, 12 Oct 2025 14:26:00 +0900 Subject: [PATCH] doc: update mainTemplate.gradle --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/README.md b/README.md index 0a5a85140..415bdd3ea 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,83 @@ The easiest way to include `libstdc++_shared.so` in your APK is to place it in t You can also include `libstdc++_shared.so` at build time by adding the following code to your [`mainTemplate.gradle` file](https://docs.unity3d.com/Manual/gradle-templates.html), and the sample project is using this method. +
+ +NDK >= 23 + +```gradle +// Include libc++_shared.so +task copyLibcppShared { + doLast { + def ndkDir = android.ndkDirectory + def abiFilters = android.defaultConfig.ndk.abiFilters + def destDir = file("$projectDir/src/main/jniLibs") + + // Mapping from ABI to architecture triple (for NDK 23+) + def abiToTriple = [ + 'arm64-v8a': 'aarch64-linux-android', + 'armeabi-v7a': 'arm-linux-androideabi', + 'x86': 'i686-linux-android', + 'x86_64': 'x86_64-linux-android', + 'riscv64': 'riscv64-linux-android' + ] + + // Find the prebuilt directory (usually there's only one) + def prebuiltDir = null + def prebuiltBase = file("$ndkDir/toolchains/llvm/prebuilt") + if (prebuiltBase.exists()) { + def prebuiltDirs = prebuiltBase.listFiles()?.findAll { it.isDirectory() } + if (prebuiltDirs && prebuiltDirs.size() > 0) { + prebuiltDir = prebuiltDirs[0] + } + } + + abiFilters.each { abi -> + if (prebuiltDir != null) { + def triple = abiToTriple[abi] + if (triple != null) { + def libcppPath = file("$prebuiltDir/sysroot/usr/lib/$triple/libc++_shared.so") + if (libcppPath.exists()) { + def destAbiDir = file("$destDir/$abi") + copy { + from libcppPath + into destAbiDir + } + } + } + } + } + } +} + +task cleanCopyLibcppShared { + doLast { + def destDir = file("$projectDir/src/main/jniLibs") + def abiFilters = android.defaultConfig.ndk.abiFilters + + abiFilters.each { abi -> + def libcppFile = file("$destDir/$abi/libc++_shared.so") + if (libcppFile.exists()) { + libcppFile.delete() + } + } + } +} +clean.dependsOn 'cleanCopyLibcppShared' + +tasks.whenTaskAdded { task -> + if (task.name == "mergeDebugJniLibFolders" || task.name == "mergeReleaseJniLibFolders") { + task.dependsOn("copyLibcppShared") + } +} +``` + +
+ +
+ +NDK < 23 + ```gradle // Include libc++_shared.so task copyLibcppShared(type: Copy) { @@ -186,6 +263,8 @@ tasks.whenTaskAdded { task -> } ``` +
+ ## :plate_with_cutlery: Try the sample app Before using the plugin in your project, it's strongly recommended that you check if sample scenes work.