wails/v3/pkg/application/single_instance_linux.go
Lea Anthony 4d8ec29feb feat: Add Android support for Wails v3
This commit adds comprehensive Android support for Wails v3, enabling
Go applications to run as native Android apps with WebView-based UI.

Key features:
- Android-specific application implementation with JNI bridge
- WebView integration via WebViewAssetLoader for serving assets
- JavaScript runtime injection and execution via JNI callbacks
- Binding call support with async result callbacks
- Event system support for Android platform
- Full example Android app with Gradle build system

Technical details:
- Uses CGO with Android NDK for cross-compilation
- Implements JNI callbacks for Go <-> Java communication
- Supports both ARM64 and x86_64 architectures
- WebView debugging support via Chrome DevTools Protocol
- Handles empty response body case in binding calls to prevent panic

Files added:
- v3/pkg/application/*_android.go - Android platform implementations
- v3/pkg/events/events_android.go - Android event definitions
- v3/internal/*/\*_android.go - Android-specific internal packages
- v3/examples/android/ - Complete example Android application
- v3/ANDROID_ARCHITECTURE.md - Architecture documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 21:06:59 +11:00

101 lines
2.3 KiB
Go

//go:build linux && !android
package application
import (
"errors"
"os"
"strings"
"sync"
"syscall"
"github.com/godbus/dbus/v5"
)
type dbusHandler func(string)
var setup sync.Once
func (f dbusHandler) SendMessage(message string) *dbus.Error {
f(message)
return nil
}
type linuxLock struct {
file *os.File
uniqueID string
dbusPath string
dbusName string
manager *singleInstanceManager
}
func newPlatformLock(manager *singleInstanceManager) (platformLock, error) {
return &linuxLock{
manager: manager,
}, nil
}
func (l *linuxLock) acquire(uniqueID string) error {
if uniqueID == "" {
return errors.New("UniqueID is required for single instance lock")
}
id := "wails_app_" + strings.ReplaceAll(strings.ReplaceAll(uniqueID, "-", "_"), ".", "_")
l.dbusName = "org." + id + ".SingleInstance"
l.dbusPath = "/org/" + id + "/SingleInstance"
conn, err := dbus.ConnectSessionBus()
// if we will reach any error during establishing connection or sending message we will just continue.
// It should not be the case that such thing will happen actually, but just in case.
if err != nil {
return err
}
setup.Do(func() {
f := dbusHandler(func(message string) {
secondInstanceBuffer <- message
})
err = conn.Export(f, dbus.ObjectPath(l.dbusPath), l.dbusName)
})
if err != nil {
return err
}
reply, err := conn.RequestName(l.dbusName, dbus.NameFlagDoNotQueue)
if err != nil {
return err
}
// if name already taken, try to send args to existing instance, if no success just launch new instance
if reply == dbus.RequestNameReplyExists {
return alreadyRunningError
}
return nil
}
func (l *linuxLock) release() {
if l.file != nil {
syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN)
l.file.Close()
os.Remove(l.file.Name())
l.file = nil
}
}
func (l *linuxLock) notify(data string) error {
conn, err := dbus.ConnectSessionBus()
// if we will reach any error during establishing connection or sending message we will just continue.
// It should not be the case that such thing will happen actually, but just in case.
if err != nil {
return err
}
err = conn.Object(l.dbusName, dbus.ObjectPath(l.dbusPath)).Call(l.dbusName+".SendMessage", 0, data).Store()
if err != nil {
return err
}
os.Exit(l.manager.options.ExitCode)
return nil
}