From 99647591f6b9df438999841c898f89ec629ea818 Mon Sep 17 00:00:00 2001 From: CoyAce Date: Wed, 7 Jan 2026 20:02:19 +0800 Subject: [PATCH] app: [Android] delete redundant dataDirChan since dataDir() must call after main(), it's redundant to use channel to send path Signed-off-by: CoyAce Signed-off-by: Elias Naur --- app/app.go | 3 +-- app/os_android.go | 13 +++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/app/app.go b/app/app.go index 1b76ab9b..b072df53 100644 --- a/app/app.go +++ b/app/app.go @@ -130,8 +130,7 @@ func NewContext(ops *op.Ops, e FrameEvent) layout.Context { // On iOS NSDocumentDirectory is queried. // For Android Context.getFilesDir is used. // -// Note that on Android, DataDir blocks until main is called. -// Don't call it from init functions or global variable initializers. +// BUG: On Android, DataDir panics if called before main. func DataDir() (string, error) { return dataDir() } diff --git a/app/os_android.go b/app/os_android.go index dff804d6..c56fc10b 100644 --- a/app/os_android.go +++ b/app/os_android.go @@ -218,8 +218,6 @@ type AndroidViewEvent struct { type jvalue uint64 // The largest JNI type fits in 64 bits. -var dataDirChan = make(chan string, 1) - var android struct { // mu protects all fields of this structure. However, once a // non-nil jvm is returned from javaVM, all the other fields may @@ -295,8 +293,7 @@ var mainWindow = newWindowRendezvous() var mainFuncs = make(chan func(env *C.JNIEnv), 1) var ( - dataDirOnce sync.Once - dataPath string + dataPath string ) var ( @@ -343,9 +340,9 @@ func (w *window) NewContext() (context, error) { } func dataDir() (string, error) { - dataDirOnce.Do(func() { - dataPath = <-dataDirChan - }) + if dataPath == "" { + panic("DataDir isn't valid before main") + } return dataPath, nil } @@ -398,7 +395,7 @@ func Java_org_gioui_Gio_runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyt os.Setenv("HOME", dataDir) } - dataDirChan <- dataDir + dataPath = dataDir C.jni_ReleaseByteArrayElements(env, jdataDir, dirBytes) runMain()