From f9cc43363379d80502d28fcc775184425d4fe1a1 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 16 Nov 2026 23:13:53 +0530 Subject: [PATCH] fix(cjk): reuse plugin ClassLoader so Pinyin survives engine release release() drops the engine when the keyboard is trimmed, and the next load created a new PluginClassLoader. Its fresh PinyinDecoderJni tried to load libjni_pinyin.so again, which ART rejects because the library is already opened by the first loader, so Pinyin fell back to the mock engine until a restart. Cache the loader per plugin APK, as the handwriting loader does. --- .../keyboard/latin/cjk/CjkPluginLoader.kt | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt b/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt index cfab696e3..2c7ac63c6 100644 --- a/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt +++ b/app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt @@ -27,6 +27,11 @@ object CjkPluginLoader { private var activeEngine: ICjkEngine? = null private var lastApkModified: Long = 0L + // A native library (jni_pinyin) can only be loaded by one ClassLoader. The loader must outlive the engine + // (release() drops it on every keyboard close), otherwise Pinyin stays unavailable after a re-open. + private var cachedClassLoader: ClassLoader? = null + private var cachedLoaderApkModified: Long = 0L + fun getTargetAbi(): String { for (abi in Build.SUPPORTED_ABIS) { when (abi) { @@ -191,12 +196,17 @@ object CjkPluginLoader { extractPluginAssetsAndLibs(apkFile, nativeLibDir, sharedDir) - val classLoader = PluginClassLoader( - apkFile.absolutePath, - context.codeCacheDir.absolutePath, - nativeLibDir.absolutePath, - context.classLoader - ) + val apkModified = apkFile.lastModified() + val classLoader = cachedClassLoader?.takeIf { cachedLoaderApkModified == apkModified } + ?: PluginClassLoader( + apkFile.absolutePath, + context.codeCacheDir.absolutePath, + nativeLibDir.absolutePath, + context.classLoader + ).also { + cachedClassLoader = it + cachedLoaderApkModified = apkModified + } val clazz = classLoader.loadClass(PLUGIN_CLASS_NAME) val engine = clazz.getDeclaredConstructor().newInstance() as ICjkEngine @@ -372,6 +382,7 @@ object CjkPluginLoader { try { activeEngine?.cleanup() activeEngine = null + cachedClassLoader = null val apkFile = File(context.filesDir, PLUGIN_FILENAME) if (apkFile.exists()) apkFile.delete() val nativeLibBase = File(context.filesDir, "plugin_libs")