init
237
build/android/Taskfile.yml
Normal file
@@ -0,0 +1,237 @@
|
||||
version: '3'
|
||||
|
||||
includes:
|
||||
common: ../Taskfile.yml
|
||||
|
||||
vars:
|
||||
APP_ID: '{{.APP_ID | default "com.wails.app"}}'
|
||||
MIN_SDK: '21'
|
||||
TARGET_SDK: '34'
|
||||
NDK_VERSION: 'r26d'
|
||||
|
||||
tasks:
|
||||
install:deps:
|
||||
summary: Check and install Android development dependencies
|
||||
cmds:
|
||||
- go run build/android/scripts/deps/install_deps.go
|
||||
env:
|
||||
TASK_FORCE_YES: '{{if .YES}}true{{else}}false{{end}}'
|
||||
prompt: This will check and install Android development dependencies. Continue?
|
||||
|
||||
build:
|
||||
summary: Creates a build of the application for Android
|
||||
deps:
|
||||
- task: common:go:mod:tidy
|
||||
- task: generate:android:bindings
|
||||
vars:
|
||||
BUILD_FLAGS:
|
||||
ref: .BUILD_FLAGS
|
||||
- task: common:build:frontend
|
||||
vars:
|
||||
BUILD_FLAGS:
|
||||
ref: .BUILD_FLAGS
|
||||
PRODUCTION:
|
||||
ref: .PRODUCTION
|
||||
- task: common:generate:icons
|
||||
cmds:
|
||||
- echo "Building Android app {{.APP_NAME}}..."
|
||||
- task: compile:go:shared
|
||||
vars:
|
||||
ARCH: '{{.ARCH | default "arm64"}}'
|
||||
vars:
|
||||
BUILD_FLAGS: '{{if eq .PRODUCTION "true"}}-tags production,android -trimpath -buildvcs=false -ldflags="-w -s"{{else}}-tags android,debug -buildvcs=false -gcflags=all="-l"{{end}}'
|
||||
env:
|
||||
PRODUCTION: '{{.PRODUCTION | default "false"}}'
|
||||
|
||||
compile:go:shared:
|
||||
summary: Compile Go code to shared library (.so)
|
||||
cmds:
|
||||
- |
|
||||
NDK_ROOT="${ANDROID_NDK_HOME:-$ANDROID_HOME/ndk/{{.NDK_VERSION}}}"
|
||||
if [ ! -d "$NDK_ROOT" ]; then
|
||||
echo "Error: Android NDK not found at $NDK_ROOT"
|
||||
echo "Please set ANDROID_NDK_HOME or install NDK {{.NDK_VERSION}} via Android Studio"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Determine toolchain based on host OS
|
||||
case "$(uname -s)" in
|
||||
Darwin) HOST_TAG="darwin-x86_64" ;;
|
||||
Linux) HOST_TAG="linux-x86_64" ;;
|
||||
*) echo "Unsupported host OS"; exit 1 ;;
|
||||
esac
|
||||
|
||||
TOOLCHAIN="$NDK_ROOT/toolchains/llvm/prebuilt/$HOST_TAG"
|
||||
|
||||
# Set compiler based on architecture
|
||||
case "{{.ARCH}}" in
|
||||
arm64)
|
||||
export CC="$TOOLCHAIN/bin/aarch64-linux-android{{.MIN_SDK}}-clang"
|
||||
export CXX="$TOOLCHAIN/bin/aarch64-linux-android{{.MIN_SDK}}-clang++"
|
||||
export GOARCH=arm64
|
||||
JNI_DIR="arm64-v8a"
|
||||
;;
|
||||
amd64|x86_64)
|
||||
export CC="$TOOLCHAIN/bin/x86_64-linux-android{{.MIN_SDK}}-clang"
|
||||
export CXX="$TOOLCHAIN/bin/x86_64-linux-android{{.MIN_SDK}}-clang++"
|
||||
export GOARCH=amd64
|
||||
JNI_DIR="x86_64"
|
||||
;;
|
||||
*)
|
||||
echo "Unsupported architecture: {{.ARCH}}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
export CGO_ENABLED=1
|
||||
export GOOS=android
|
||||
|
||||
mkdir -p {{.BIN_DIR}}
|
||||
mkdir -p build/android/app/src/main/jniLibs/$JNI_DIR
|
||||
|
||||
go build -buildmode=c-shared {{.BUILD_FLAGS}} \
|
||||
-o build/android/app/src/main/jniLibs/$JNI_DIR/libwails.so
|
||||
vars:
|
||||
BUILD_FLAGS: '{{if eq .PRODUCTION "true"}}-tags production,android -trimpath -buildvcs=false -ldflags="-w -s"{{else}}-tags android,debug -buildvcs=false -gcflags=all="-l"{{end}}'
|
||||
|
||||
compile:go:all-archs:
|
||||
summary: Compile Go code for all Android architectures (fat APK)
|
||||
cmds:
|
||||
- task: compile:go:shared
|
||||
vars:
|
||||
ARCH: arm64
|
||||
- task: compile:go:shared
|
||||
vars:
|
||||
ARCH: amd64
|
||||
|
||||
package:
|
||||
summary: Packages a production build of the application into an APK
|
||||
deps:
|
||||
- task: build
|
||||
vars:
|
||||
PRODUCTION: "true"
|
||||
cmds:
|
||||
- task: assemble:apk
|
||||
|
||||
package:fat:
|
||||
summary: Packages a production build for all architectures (fat APK)
|
||||
cmds:
|
||||
- task: compile:go:all-archs
|
||||
- task: assemble:apk
|
||||
|
||||
assemble:apk:
|
||||
summary: Assembles the APK using Gradle
|
||||
cmds:
|
||||
- |
|
||||
cd build/android
|
||||
./gradlew assembleDebug
|
||||
cp app/build/outputs/apk/debug/app-debug.apk "../../{{.BIN_DIR}}/{{.APP_NAME}}.apk"
|
||||
echo "APK created: {{.BIN_DIR}}/{{.APP_NAME}}.apk"
|
||||
|
||||
assemble:apk:release:
|
||||
summary: Assembles a release APK using Gradle
|
||||
cmds:
|
||||
- |
|
||||
cd build/android
|
||||
./gradlew assembleRelease
|
||||
cp app/build/outputs/apk/release/app-release-unsigned.apk "../../{{.BIN_DIR}}/{{.APP_NAME}}-release.apk"
|
||||
echo "Release APK created: {{.BIN_DIR}}/{{.APP_NAME}}-release.apk"
|
||||
|
||||
generate:android:bindings:
|
||||
internal: true
|
||||
summary: Generates bindings for Android
|
||||
sources:
|
||||
- "**/*.go"
|
||||
- go.mod
|
||||
- go.sum
|
||||
generates:
|
||||
- frontend/bindings/**/*
|
||||
cmds:
|
||||
- wails3 generate bindings -f '{{.BUILD_FLAGS}}' -clean=true
|
||||
env:
|
||||
GOOS: android
|
||||
CGO_ENABLED: 1
|
||||
GOARCH: '{{.ARCH | default "arm64"}}'
|
||||
|
||||
ensure-emulator:
|
||||
internal: true
|
||||
summary: Ensure Android Emulator is running
|
||||
silent: true
|
||||
cmds:
|
||||
- |
|
||||
# Check if an emulator is already running
|
||||
if adb devices | grep -q "emulator"; then
|
||||
echo "Emulator already running"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Get first available AVD
|
||||
AVD_NAME=$(emulator -list-avds | head -1)
|
||||
if [ -z "$AVD_NAME" ]; then
|
||||
echo "No Android Virtual Devices found."
|
||||
echo "Create one using: Android Studio > Tools > Device Manager"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Starting emulator: $AVD_NAME"
|
||||
emulator -avd "$AVD_NAME" -no-snapshot-load &
|
||||
|
||||
# Wait for emulator to boot (max 60 seconds)
|
||||
echo "Waiting for emulator to boot..."
|
||||
adb wait-for-device
|
||||
|
||||
for i in {1..60}; do
|
||||
BOOT_COMPLETED=$(adb shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')
|
||||
if [ "$BOOT_COMPLETED" = "1" ]; then
|
||||
echo "Emulator booted successfully"
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo "Emulator boot timeout"
|
||||
exit 1
|
||||
preconditions:
|
||||
- sh: command -v adb
|
||||
msg: "adb not found. Please install Android SDK and add platform-tools to PATH"
|
||||
- sh: command -v emulator
|
||||
msg: "emulator not found. Please install Android SDK and add emulator to PATH"
|
||||
|
||||
deploy-emulator:
|
||||
summary: Deploy to Android Emulator
|
||||
deps: [package]
|
||||
cmds:
|
||||
- adb uninstall {{.APP_ID}} 2>/dev/null || true
|
||||
- adb install "{{.BIN_DIR}}/{{.APP_NAME}}.apk"
|
||||
- adb shell am start -n {{.APP_ID}}/.MainActivity
|
||||
|
||||
run:
|
||||
summary: Run the application in Android Emulator
|
||||
deps:
|
||||
- task: ensure-emulator
|
||||
- task: build
|
||||
vars:
|
||||
ARCH: x86_64
|
||||
cmds:
|
||||
- task: assemble:apk
|
||||
- adb uninstall {{.APP_ID}} 2>/dev/null || true
|
||||
- adb install "{{.BIN_DIR}}/{{.APP_NAME}}.apk"
|
||||
- adb shell am start -n {{.APP_ID}}/.MainActivity
|
||||
|
||||
logs:
|
||||
summary: Stream Android logcat filtered to this app
|
||||
cmds:
|
||||
- adb logcat -v time | grep -E "(Wails|{{.APP_NAME}})"
|
||||
|
||||
logs:all:
|
||||
summary: Stream all Android logcat (verbose)
|
||||
cmds:
|
||||
- adb logcat -v time
|
||||
|
||||
clean:
|
||||
summary: Clean build artifacts
|
||||
cmds:
|
||||
- rm -rf {{.BIN_DIR}}
|
||||
- rm -rf build/android/app/build
|
||||
- rm -rf build/android/app/src/main/jniLibs/*/libwails.so
|
||||
- rm -rf build/android/.gradle
|
||||
63
build/android/app/build.gradle
Normal file
@@ -0,0 +1,63 @@
|
||||
plugins {
|
||||
id 'com.android.application'
|
||||
}
|
||||
|
||||
android {
|
||||
namespace 'com.wails.app'
|
||||
compileSdk 34
|
||||
|
||||
buildFeatures {
|
||||
buildConfig = true
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.wails.app"
|
||||
minSdk 21
|
||||
targetSdk 34
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
// Configure supported ABIs
|
||||
ndk {
|
||||
abiFilters 'arm64-v8a', 'x86_64'
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
debug {
|
||||
debuggable true
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_11
|
||||
targetCompatibility JavaVersion.VERSION_11
|
||||
}
|
||||
|
||||
// Source sets configuration
|
||||
sourceSets {
|
||||
main {
|
||||
// JNI libraries are in jniLibs folder
|
||||
jniLibs.srcDirs = ['src/main/jniLibs']
|
||||
// Assets for the WebView
|
||||
assets.srcDirs = ['src/main/assets']
|
||||
}
|
||||
}
|
||||
|
||||
// Packaging options
|
||||
packagingOptions {
|
||||
// Don't strip Go symbols in debug builds
|
||||
doNotStrip '*/arm64-v8a/libwails.so'
|
||||
doNotStrip '*/x86_64/libwails.so'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||
implementation 'androidx.webkit:webkit:1.9.0'
|
||||
implementation 'com.google.android.material:material:1.11.0'
|
||||
}
|
||||
12
build/android/app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
|
||||
# Keep native methods
|
||||
-keepclasseswithmembernames class * {
|
||||
native <methods>;
|
||||
}
|
||||
|
||||
# Keep Wails bridge classes
|
||||
-keep class com.wails.app.WailsBridge { *; }
|
||||
-keep class com.wails.app.WailsJSBridge { *; }
|
||||
30
build/android/app/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<!-- Internet permission for WebView -->
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.WailsApp"
|
||||
android:usesCleartextTraffic="true"
|
||||
tools:targetApi="31">
|
||||
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:configChanges="orientation|screenSize|keyboardHidden"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
198
build/android/app/src/main/java/com/wails/app/MainActivity.java
Normal file
@@ -0,0 +1,198 @@
|
||||
package com.wails.app;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebResourceResponse;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.webkit.WebViewAssetLoader;
|
||||
import com.wails.app.BuildConfig;
|
||||
|
||||
/**
|
||||
* MainActivity hosts the WebView and manages the Wails application lifecycle.
|
||||
* It uses WebViewAssetLoader to serve assets from the Go library without
|
||||
* requiring a network server.
|
||||
*/
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private static final String TAG = "WailsActivity";
|
||||
private static final String WAILS_SCHEME = "https";
|
||||
private static final String WAILS_HOST = "wails.localhost";
|
||||
|
||||
private WebView webView;
|
||||
private WailsBridge bridge;
|
||||
private WebViewAssetLoader assetLoader;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// Initialize the native Go library
|
||||
bridge = new WailsBridge(this);
|
||||
bridge.initialize();
|
||||
|
||||
// Set up WebView
|
||||
setupWebView();
|
||||
|
||||
// Load the application
|
||||
loadApplication();
|
||||
}
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
private void setupWebView() {
|
||||
webView = findViewById(R.id.webview);
|
||||
|
||||
// Configure WebView settings
|
||||
WebSettings settings = webView.getSettings();
|
||||
settings.setJavaScriptEnabled(true);
|
||||
settings.setDomStorageEnabled(true);
|
||||
settings.setDatabaseEnabled(true);
|
||||
settings.setAllowFileAccess(false);
|
||||
settings.setAllowContentAccess(false);
|
||||
settings.setMediaPlaybackRequiresUserGesture(false);
|
||||
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
|
||||
|
||||
// Enable debugging in debug builds
|
||||
if (BuildConfig.DEBUG) {
|
||||
WebView.setWebContentsDebuggingEnabled(true);
|
||||
}
|
||||
|
||||
// Set up asset loader for serving local assets
|
||||
assetLoader = new WebViewAssetLoader.Builder()
|
||||
.setDomain(WAILS_HOST)
|
||||
.addPathHandler("/", new WailsPathHandler(bridge))
|
||||
.build();
|
||||
|
||||
// Set up WebView client to intercept requests
|
||||
webView.setWebViewClient(new WebViewClient() {
|
||||
@Nullable
|
||||
@Override
|
||||
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
|
||||
String url = request.getUrl().toString();
|
||||
Log.d(TAG, "Intercepting request: " + url);
|
||||
|
||||
// Handle wails.localhost requests
|
||||
if (request.getUrl().getHost() != null &&
|
||||
request.getUrl().getHost().equals(WAILS_HOST)) {
|
||||
|
||||
// For wails API calls (runtime, capabilities, etc.), we need to pass the full URL
|
||||
// including query string because WebViewAssetLoader.PathHandler strips query params
|
||||
String path = request.getUrl().getPath();
|
||||
if (path != null && path.startsWith("/wails/")) {
|
||||
// Get full path with query string for runtime calls
|
||||
String fullPath = path;
|
||||
String query = request.getUrl().getQuery();
|
||||
if (query != null && !query.isEmpty()) {
|
||||
fullPath = path + "?" + query;
|
||||
}
|
||||
Log.d(TAG, "Wails API call detected, full path: " + fullPath);
|
||||
|
||||
// Call bridge directly with full path
|
||||
byte[] data = bridge.serveAsset(fullPath, request.getMethod(), "{}");
|
||||
if (data != null && data.length > 0) {
|
||||
java.io.InputStream inputStream = new java.io.ByteArrayInputStream(data);
|
||||
java.util.Map<String, String> headers = new java.util.HashMap<>();
|
||||
headers.put("Access-Control-Allow-Origin", "*");
|
||||
headers.put("Cache-Control", "no-cache");
|
||||
headers.put("Content-Type", "application/json");
|
||||
|
||||
return new WebResourceResponse(
|
||||
"application/json",
|
||||
"UTF-8",
|
||||
200,
|
||||
"OK",
|
||||
headers,
|
||||
inputStream
|
||||
);
|
||||
}
|
||||
// Return error response if data is null
|
||||
return new WebResourceResponse(
|
||||
"application/json",
|
||||
"UTF-8",
|
||||
500,
|
||||
"Internal Error",
|
||||
new java.util.HashMap<>(),
|
||||
new java.io.ByteArrayInputStream("{}".getBytes())
|
||||
);
|
||||
}
|
||||
|
||||
// For regular assets, use the asset loader
|
||||
return assetLoader.shouldInterceptRequest(request.getUrl());
|
||||
}
|
||||
|
||||
return super.shouldInterceptRequest(view, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
Log.d(TAG, "Page loaded: " + url);
|
||||
// Inject Wails runtime
|
||||
bridge.injectRuntime(webView, url);
|
||||
}
|
||||
});
|
||||
|
||||
// Add JavaScript interface for Go communication
|
||||
webView.addJavascriptInterface(new WailsJSBridge(bridge, webView), "wails");
|
||||
}
|
||||
|
||||
private void loadApplication() {
|
||||
// Load the main page from the asset server
|
||||
String url = WAILS_SCHEME + "://" + WAILS_HOST + "/";
|
||||
Log.d(TAG, "Loading URL: " + url);
|
||||
webView.loadUrl(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute JavaScript in the WebView from the Go side
|
||||
*/
|
||||
public void executeJavaScript(final String js) {
|
||||
runOnUiThread(() -> {
|
||||
if (webView != null) {
|
||||
webView.evaluateJavascript(js, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (bridge != null) {
|
||||
bridge.onResume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (bridge != null) {
|
||||
bridge.onPause();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (bridge != null) {
|
||||
bridge.shutdown();
|
||||
}
|
||||
if (webView != null) {
|
||||
webView.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (webView != null && webView.canGoBack()) {
|
||||
webView.goBack();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
214
build/android/app/src/main/java/com/wails/app/WailsBridge.java
Normal file
@@ -0,0 +1,214 @@
|
||||
package com.wails.app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* WailsBridge manages the connection between the Java/Android side and the Go native library.
|
||||
* It handles:
|
||||
* - Loading and initializing the native Go library
|
||||
* - Serving asset requests from Go
|
||||
* - Passing messages between JavaScript and Go
|
||||
* - Managing callbacks for async operations
|
||||
*/
|
||||
public class WailsBridge {
|
||||
private static final String TAG = "WailsBridge";
|
||||
|
||||
static {
|
||||
// Load the native Go library
|
||||
System.loadLibrary("wails");
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
private final AtomicInteger callbackIdGenerator = new AtomicInteger(0);
|
||||
private final ConcurrentHashMap<Integer, AssetCallback> pendingAssetCallbacks = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<Integer, MessageCallback> pendingMessageCallbacks = new ConcurrentHashMap<>();
|
||||
private WebView webView;
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
// Native methods - implemented in Go
|
||||
private static native void nativeInit(WailsBridge bridge);
|
||||
private static native void nativeShutdown();
|
||||
private static native void nativeOnResume();
|
||||
private static native void nativeOnPause();
|
||||
private static native void nativeOnPageFinished(String url);
|
||||
private static native byte[] nativeServeAsset(String path, String method, String headers);
|
||||
private static native String nativeHandleMessage(String message);
|
||||
private static native String nativeGetAssetMimeType(String path);
|
||||
|
||||
public WailsBridge(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the native Go library
|
||||
*/
|
||||
public void initialize() {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log.i(TAG, "Initializing Wails bridge...");
|
||||
try {
|
||||
nativeInit(this);
|
||||
initialized = true;
|
||||
Log.i(TAG, "Wails bridge initialized successfully");
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to initialize Wails bridge", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the native Go library
|
||||
*/
|
||||
public void shutdown() {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log.i(TAG, "Shutting down Wails bridge...");
|
||||
try {
|
||||
nativeShutdown();
|
||||
initialized = false;
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error during shutdown", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the activity resumes
|
||||
*/
|
||||
public void onResume() {
|
||||
if (initialized) {
|
||||
nativeOnResume();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the activity pauses
|
||||
*/
|
||||
public void onPause() {
|
||||
if (initialized) {
|
||||
nativeOnPause();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve an asset from the Go asset server
|
||||
* @param path The URL path requested
|
||||
* @param method The HTTP method
|
||||
* @param headers The request headers as JSON
|
||||
* @return The asset data, or null if not found
|
||||
*/
|
||||
public byte[] serveAsset(String path, String method, String headers) {
|
||||
if (!initialized) {
|
||||
Log.w(TAG, "Bridge not initialized, cannot serve asset: " + path);
|
||||
return null;
|
||||
}
|
||||
|
||||
Log.d(TAG, "Serving asset: " + path);
|
||||
try {
|
||||
return nativeServeAsset(path, method, headers);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error serving asset: " + path, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the MIME type for an asset
|
||||
* @param path The asset path
|
||||
* @return The MIME type string
|
||||
*/
|
||||
public String getAssetMimeType(String path) {
|
||||
if (!initialized) {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
try {
|
||||
String mimeType = nativeGetAssetMimeType(path);
|
||||
return mimeType != null ? mimeType : "application/octet-stream";
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error getting MIME type for: " + path, e);
|
||||
return "application/octet-stream";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a message from JavaScript
|
||||
* @param message The message from JavaScript (JSON)
|
||||
* @return The response to send back to JavaScript (JSON)
|
||||
*/
|
||||
public String handleMessage(String message) {
|
||||
if (!initialized) {
|
||||
Log.w(TAG, "Bridge not initialized, cannot handle message");
|
||||
return "{\"error\":\"Bridge not initialized\"}";
|
||||
}
|
||||
|
||||
Log.d(TAG, "Handling message from JS: " + message);
|
||||
try {
|
||||
return nativeHandleMessage(message);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error handling message", e);
|
||||
return "{\"error\":\"" + e.getMessage() + "\"}";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject the Wails runtime JavaScript into the WebView.
|
||||
* Called when the page finishes loading.
|
||||
* @param webView The WebView to inject into
|
||||
* @param url The URL that finished loading
|
||||
*/
|
||||
public void injectRuntime(WebView webView, String url) {
|
||||
this.webView = webView;
|
||||
// Notify Go side that page has finished loading so it can inject the runtime
|
||||
Log.d(TAG, "Page finished loading: " + url + ", notifying Go side");
|
||||
if (initialized) {
|
||||
nativeOnPageFinished(url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute JavaScript in the WebView (called from Go side)
|
||||
* @param js The JavaScript code to execute
|
||||
*/
|
||||
public void executeJavaScript(String js) {
|
||||
if (webView != null) {
|
||||
webView.post(() -> webView.evaluateJavascript(js, null));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from Go when an event needs to be emitted to JavaScript
|
||||
* @param eventName The event name
|
||||
* @param eventData The event data (JSON)
|
||||
*/
|
||||
public void emitEvent(String eventName, String eventData) {
|
||||
String js = String.format("window.wails && window.wails._emit('%s', %s);",
|
||||
escapeJsString(eventName), eventData);
|
||||
executeJavaScript(js);
|
||||
}
|
||||
|
||||
private String escapeJsString(String str) {
|
||||
return str.replace("\\", "\\\\")
|
||||
.replace("'", "\\'")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r");
|
||||
}
|
||||
|
||||
// Callback interfaces
|
||||
public interface AssetCallback {
|
||||
void onAssetReady(byte[] data, String mimeType);
|
||||
void onAssetError(String error);
|
||||
}
|
||||
|
||||
public interface MessageCallback {
|
||||
void onResponse(String response);
|
||||
void onError(String error);
|
||||
}
|
||||
}
|
||||
142
build/android/app/src/main/java/com/wails/app/WailsJSBridge.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package com.wails.app;
|
||||
|
||||
import android.util.Log;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.WebView;
|
||||
import com.wails.app.BuildConfig;
|
||||
|
||||
/**
|
||||
* WailsJSBridge provides the JavaScript interface that allows the web frontend
|
||||
* to communicate with the Go backend. This is exposed to JavaScript as the
|
||||
* `window.wails` object.
|
||||
*
|
||||
* Similar to iOS's WKScriptMessageHandler but using Android's addJavascriptInterface.
|
||||
*/
|
||||
public class WailsJSBridge {
|
||||
private static final String TAG = "WailsJSBridge";
|
||||
|
||||
private final WailsBridge bridge;
|
||||
private final WebView webView;
|
||||
|
||||
public WailsJSBridge(WailsBridge bridge, WebView webView) {
|
||||
this.bridge = bridge;
|
||||
this.webView = webView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to Go and return the response synchronously.
|
||||
* Called from JavaScript: wails.invoke(message)
|
||||
*
|
||||
* @param message The message to send (JSON string)
|
||||
* @return The response from Go (JSON string)
|
||||
*/
|
||||
@JavascriptInterface
|
||||
public String invoke(String message) {
|
||||
Log.d(TAG, "Invoke called: " + message);
|
||||
return bridge.handleMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to Go asynchronously.
|
||||
* The response will be sent back via a callback.
|
||||
* Called from JavaScript: wails.invokeAsync(callbackId, message)
|
||||
*
|
||||
* @param callbackId The callback ID to use for the response
|
||||
* @param message The message to send (JSON string)
|
||||
*/
|
||||
@JavascriptInterface
|
||||
public void invokeAsync(final String callbackId, final String message) {
|
||||
Log.d(TAG, "InvokeAsync called: " + message);
|
||||
|
||||
// Handle in background thread to not block JavaScript
|
||||
new Thread(() -> {
|
||||
try {
|
||||
String response = bridge.handleMessage(message);
|
||||
sendCallback(callbackId, response, null);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error in async invoke", e);
|
||||
sendCallback(callbackId, null, e.getMessage());
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message from JavaScript to Android's logcat
|
||||
* Called from JavaScript: wails.log(level, message)
|
||||
*
|
||||
* @param level The log level (debug, info, warn, error)
|
||||
* @param message The message to log
|
||||
*/
|
||||
@JavascriptInterface
|
||||
public void log(String level, String message) {
|
||||
switch (level.toLowerCase()) {
|
||||
case "debug":
|
||||
Log.d(TAG + "/JS", message);
|
||||
break;
|
||||
case "info":
|
||||
Log.i(TAG + "/JS", message);
|
||||
break;
|
||||
case "warn":
|
||||
Log.w(TAG + "/JS", message);
|
||||
break;
|
||||
case "error":
|
||||
Log.e(TAG + "/JS", message);
|
||||
break;
|
||||
default:
|
||||
Log.v(TAG + "/JS", message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the platform name
|
||||
* Called from JavaScript: wails.platform()
|
||||
*
|
||||
* @return "android"
|
||||
*/
|
||||
@JavascriptInterface
|
||||
public String platform() {
|
||||
return "android";
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we're running in debug mode
|
||||
* Called from JavaScript: wails.isDebug()
|
||||
*
|
||||
* @return true if debug build, false otherwise
|
||||
*/
|
||||
@JavascriptInterface
|
||||
public boolean isDebug() {
|
||||
return BuildConfig.DEBUG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a callback response to JavaScript
|
||||
*/
|
||||
private void sendCallback(String callbackId, String result, String error) {
|
||||
final String js;
|
||||
if (error != null) {
|
||||
js = String.format(
|
||||
"window.wails && window.wails._callback('%s', null, '%s');",
|
||||
escapeJsString(callbackId),
|
||||
escapeJsString(error)
|
||||
);
|
||||
} else {
|
||||
js = String.format(
|
||||
"window.wails && window.wails._callback('%s', %s, null);",
|
||||
escapeJsString(callbackId),
|
||||
result != null ? result : "null"
|
||||
);
|
||||
}
|
||||
|
||||
webView.post(() -> webView.evaluateJavascript(js, null));
|
||||
}
|
||||
|
||||
private String escapeJsString(String str) {
|
||||
if (str == null) return "";
|
||||
return str.replace("\\", "\\\\")
|
||||
.replace("'", "\\'")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.wails.app;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.webkit.WebResourceResponse;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.webkit.WebViewAssetLoader;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* WailsPathHandler implements WebViewAssetLoader.PathHandler to serve assets
|
||||
* from the Go asset server. This allows the WebView to load assets without
|
||||
* using a network server, similar to iOS's WKURLSchemeHandler.
|
||||
*/
|
||||
public class WailsPathHandler implements WebViewAssetLoader.PathHandler {
|
||||
private static final String TAG = "WailsPathHandler";
|
||||
|
||||
private final WailsBridge bridge;
|
||||
|
||||
public WailsPathHandler(WailsBridge bridge) {
|
||||
this.bridge = bridge;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public WebResourceResponse handle(@NonNull String path) {
|
||||
Log.d(TAG, "Handling path: " + path);
|
||||
|
||||
// Normalize path
|
||||
if (path.isEmpty() || path.equals("/")) {
|
||||
path = "/index.html";
|
||||
}
|
||||
|
||||
// Get asset from Go
|
||||
byte[] data = bridge.serveAsset(path, "GET", "{}");
|
||||
|
||||
if (data == null || data.length == 0) {
|
||||
Log.w(TAG, "Asset not found: " + path);
|
||||
return null; // Return null to let WebView handle 404
|
||||
}
|
||||
|
||||
// Determine MIME type
|
||||
String mimeType = bridge.getAssetMimeType(path);
|
||||
Log.d(TAG, "Serving " + path + " with type " + mimeType + " (" + data.length + " bytes)");
|
||||
|
||||
// Create response
|
||||
InputStream inputStream = new ByteArrayInputStream(data);
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Access-Control-Allow-Origin", "*");
|
||||
headers.put("Cache-Control", "no-cache");
|
||||
|
||||
return new WebResourceResponse(
|
||||
mimeType,
|
||||
"UTF-8",
|
||||
200,
|
||||
"OK",
|
||||
headers,
|
||||
inputStream
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine MIME type from file extension
|
||||
*/
|
||||
private String getMimeType(String path) {
|
||||
String lowerPath = path.toLowerCase();
|
||||
|
||||
if (lowerPath.endsWith(".html") || lowerPath.endsWith(".htm")) {
|
||||
return "text/html";
|
||||
} else if (lowerPath.endsWith(".js") || lowerPath.endsWith(".mjs")) {
|
||||
return "application/javascript";
|
||||
} else if (lowerPath.endsWith(".css")) {
|
||||
return "text/css";
|
||||
} else if (lowerPath.endsWith(".json")) {
|
||||
return "application/json";
|
||||
} else if (lowerPath.endsWith(".png")) {
|
||||
return "image/png";
|
||||
} else if (lowerPath.endsWith(".jpg") || lowerPath.endsWith(".jpeg")) {
|
||||
return "image/jpeg";
|
||||
} else if (lowerPath.endsWith(".gif")) {
|
||||
return "image/gif";
|
||||
} else if (lowerPath.endsWith(".svg")) {
|
||||
return "image/svg+xml";
|
||||
} else if (lowerPath.endsWith(".ico")) {
|
||||
return "image/x-icon";
|
||||
} else if (lowerPath.endsWith(".woff")) {
|
||||
return "font/woff";
|
||||
} else if (lowerPath.endsWith(".woff2")) {
|
||||
return "font/woff2";
|
||||
} else if (lowerPath.endsWith(".ttf")) {
|
||||
return "font/ttf";
|
||||
} else if (lowerPath.endsWith(".eot")) {
|
||||
return "application/vnd.ms-fontobject";
|
||||
} else if (lowerPath.endsWith(".xml")) {
|
||||
return "application/xml";
|
||||
} else if (lowerPath.endsWith(".txt")) {
|
||||
return "text/plain";
|
||||
} else if (lowerPath.endsWith(".wasm")) {
|
||||
return "application/wasm";
|
||||
} else if (lowerPath.endsWith(".mp3")) {
|
||||
return "audio/mpeg";
|
||||
} else if (lowerPath.endsWith(".mp4")) {
|
||||
return "video/mp4";
|
||||
} else if (lowerPath.endsWith(".webm")) {
|
||||
return "video/webm";
|
||||
} else if (lowerPath.endsWith(".webp")) {
|
||||
return "image/webp";
|
||||
}
|
||||
|
||||
return "application/octet-stream";
|
||||
}
|
||||
}
|
||||
12
build/android/app/src/main/res/layout/activity_main.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/main_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<WebView
|
||||
android:id="@+id/webview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</FrameLayout>
|
||||
BIN
build/android/app/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
build/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
build/android/app/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
build/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
build/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
BIN
build/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 5.1 KiB |
|
After Width: | Height: | Size: 5.1 KiB |
BIN
build/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 7.0 KiB |
|
After Width: | Height: | Size: 7.0 KiB |
8
build/android/app/src/main/res/values/colors.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="wails_blue">#3574D4</color>
|
||||
<color name="wails_blue_dark">#2C5FB8</color>
|
||||
<color name="wails_background">#1B2636</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="black">#FF000000</color>
|
||||
</resources>
|
||||
4
build/android/app/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Wails App</string>
|
||||
</resources>
|
||||
14
build/android/app/src/main/res/values/themes.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.WailsApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/wails_blue</item>
|
||||
<item name="colorPrimaryVariant">@color/wails_blue_dark</item>
|
||||
<item name="colorOnPrimary">@android:color/white</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">@color/wails_background</item>
|
||||
<item name="android:navigationBarColor">@color/wails_background</item>
|
||||
<!-- Window background -->
|
||||
<item name="android:windowBackground">@color/wails_background</item>
|
||||
</style>
|
||||
</resources>
|
||||
4
build/android/build.gradle
Normal file
@@ -0,0 +1,4 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
plugins {
|
||||
id 'com.android.application' version '8.7.3' apply false
|
||||
}
|
||||
26
build/android/gradle.properties
Normal file
@@ -0,0 +1,26 @@
|
||||
# Project-wide Gradle settings.
|
||||
# IDE (e.g. Android Studio) users:
|
||||
# Gradle settings configured through the IDE *will override*
|
||||
# any settings specified in this file.
|
||||
|
||||
# For more details on how to configure your build environment visit
|
||||
# http://www.gradle.org/docs/current/userguide/build_environment.html
|
||||
|
||||
# Specifies the JVM arguments used for the daemon process.
|
||||
# The setting is particularly useful for tweaking memory settings.
|
||||
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
|
||||
|
||||
# When configured, Gradle will run in incubating parallel mode.
|
||||
# This option should only be used with decoupled projects. For more details, visit
|
||||
# https://developer.android.com/build/optimize-your-build#parallel
|
||||
# org.gradle.parallel=true
|
||||
|
||||
# AndroidX package structure to make it clearer which packages are bundled with the
|
||||
# Android operating system, and which are packaged with your app's APK
|
||||
# https://developer.android.com/topic/libraries/support-library/androidx-rn
|
||||
android.useAndroidX=true
|
||||
|
||||
# Enables namespacing of each library's R class so that its R class includes only the
|
||||
# resources declared in the library itself and none from the library's dependencies,
|
||||
# thereby reducing the size of the R class for that library
|
||||
android.nonTransitiveRClass=true
|
||||
BIN
build/android/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
7
build/android/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
248
build/android/gradlew
vendored
Normal file
@@ -0,0 +1,248 @@
|
||||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright © 2015 the original authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# Gradle start up script for POSIX generated by Gradle.
|
||||
#
|
||||
# Important for running:
|
||||
#
|
||||
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||
# noncompliant, but you have some other compliant shell such as ksh or
|
||||
# bash, then to run this script, type that shell name before the whole
|
||||
# command line, like:
|
||||
#
|
||||
# ksh Gradle
|
||||
#
|
||||
# Busybox and similar reduced shells will NOT work, because this script
|
||||
# requires all of these POSIX shell features:
|
||||
# * functions;
|
||||
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||
# * compound commands having a testable exit status, especially «case»;
|
||||
# * various built-in commands including «command», «set», and «ulimit».
|
||||
#
|
||||
# Important for patching:
|
||||
#
|
||||
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||
#
|
||||
# The "traditional" practice of packing multiple parameters into a
|
||||
# space-separated string is a well documented source of bugs and security
|
||||
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||
# options in "$@", and eventually passing that to Java.
|
||||
#
|
||||
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||
# see the in-line comments for details.
|
||||
#
|
||||
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
|
||||
# Resolve links: $0 may be a link
|
||||
app_path=$0
|
||||
|
||||
# Need this for daisy-chained symlinks.
|
||||
while
|
||||
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||
[ -h "$app_path" ]
|
||||
do
|
||||
ls=$( ls -ld "$app_path" )
|
||||
link=${ls#*' -> '}
|
||||
case $link in #(
|
||||
/*) app_path=$link ;; #(
|
||||
*) app_path=$APP_HOME$link ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
} >&2
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
} >&2
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "$( uname )" in #(
|
||||
CYGWIN* ) cygwin=true ;; #(
|
||||
Darwin* ) darwin=true ;; #(
|
||||
MSYS* | MINGW* ) msys=true ;; #(
|
||||
NONSTOP* ) nonstop=true ;;
|
||||
esac
|
||||
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||
else
|
||||
JAVACMD=$JAVA_HOME/bin/java
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command, stacking in reverse order:
|
||||
# * args from the command line
|
||||
# * the main class name
|
||||
# * -classpath
|
||||
# * -D...appname settings
|
||||
# * --module-path (only if needed)
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if "$cygwin" || "$msys" ; then
|
||||
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||
|
||||
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
for arg do
|
||||
if
|
||||
case $arg in #(
|
||||
-*) false ;; # don't mess with options #(
|
||||
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||
[ -e "$t" ] ;; #(
|
||||
*) false ;;
|
||||
esac
|
||||
then
|
||||
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||
fi
|
||||
# Roll the args list around exactly as many times as the number of
|
||||
# args, so each arg winds up back in the position where it started, but
|
||||
# possibly modified.
|
||||
#
|
||||
# NB: a `for` loop captures its iteration list before it begins, so
|
||||
# changing the positional parameters here affects neither the number of
|
||||
# iterations, nor the values presented in `arg`.
|
||||
shift # remove old arg
|
||||
set -- "$@" "$arg" # push replacement arg
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
|
||||
"$@"
|
||||
|
||||
# Stop when "xargs" is not available.
|
||||
if ! command -v xargs >/dev/null 2>&1
|
||||
then
|
||||
die "xargs is not available"
|
||||
fi
|
||||
|
||||
# Use "xargs" to parse quoted args.
|
||||
#
|
||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||
#
|
||||
# In Bash we could simply go:
|
||||
#
|
||||
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||
# set -- "${ARGS[@]}" "$@"
|
||||
#
|
||||
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||
# character that might be a shell metacharacter, then use eval to reverse
|
||||
# that process (while maintaining the separation between arguments), and wrap
|
||||
# the whole thing up as a single "set" statement.
|
||||
#
|
||||
# This will of course break if any of these variables contains a newline or
|
||||
# an unmatched quote.
|
||||
#
|
||||
|
||||
eval "set -- $(
|
||||
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||
xargs -n1 |
|
||||
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||
tr '\n' ' '
|
||||
)" '"$@"'
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
93
build/android/gradlew.bat
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
@rem SPDX-License-Identifier: Apache-2.0
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%"=="" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%"=="" set DIRNAME=.
|
||||
@rem This is normally unused
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
set EXIT_CODE=%ERRORLEVEL%
|
||||
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||
exit /b %EXIT_CODE%
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
11
build/android/main_android.go
Normal file
@@ -0,0 +1,11 @@
|
||||
//go:build android
|
||||
|
||||
package main
|
||||
|
||||
import "github.com/wailsapp/wails/v3/pkg/application"
|
||||
|
||||
func init() {
|
||||
// Register main function to be called when the Android app initializes
|
||||
// This is necessary because in c-shared build mode, main() is not automatically called
|
||||
application.RegisterAndroidMain(main)
|
||||
}
|
||||
151
build/android/scripts/deps/install_deps.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Checking Android development dependencies...")
|
||||
fmt.Println()
|
||||
|
||||
errors := []string{}
|
||||
|
||||
// Check Go
|
||||
if !checkCommand("go", "version") {
|
||||
errors = append(errors, "Go is not installed. Install from https://go.dev/dl/")
|
||||
} else {
|
||||
fmt.Println("✓ Go is installed")
|
||||
}
|
||||
|
||||
// Check ANDROID_HOME
|
||||
androidHome := os.Getenv("ANDROID_HOME")
|
||||
if androidHome == "" {
|
||||
androidHome = os.Getenv("ANDROID_SDK_ROOT")
|
||||
}
|
||||
if androidHome == "" {
|
||||
// Try common default locations
|
||||
home, _ := os.UserHomeDir()
|
||||
possiblePaths := []string{
|
||||
filepath.Join(home, "Android", "Sdk"),
|
||||
filepath.Join(home, "Library", "Android", "sdk"),
|
||||
"/usr/local/share/android-sdk",
|
||||
}
|
||||
for _, p := range possiblePaths {
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
androidHome = p
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if androidHome == "" {
|
||||
errors = append(errors, "ANDROID_HOME not set. Install Android Studio and set ANDROID_HOME environment variable")
|
||||
} else {
|
||||
fmt.Printf("✓ ANDROID_HOME: %s\n", androidHome)
|
||||
}
|
||||
|
||||
// Check adb
|
||||
if !checkCommand("adb", "version") {
|
||||
if androidHome != "" {
|
||||
platformTools := filepath.Join(androidHome, "platform-tools")
|
||||
errors = append(errors, fmt.Sprintf("adb not found. Add %s to PATH", platformTools))
|
||||
} else {
|
||||
errors = append(errors, "adb not found. Install Android SDK Platform-Tools")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("✓ adb is installed")
|
||||
}
|
||||
|
||||
// Check emulator
|
||||
if !checkCommand("emulator", "-list-avds") {
|
||||
if androidHome != "" {
|
||||
emulatorPath := filepath.Join(androidHome, "emulator")
|
||||
errors = append(errors, fmt.Sprintf("emulator not found. Add %s to PATH", emulatorPath))
|
||||
} else {
|
||||
errors = append(errors, "emulator not found. Install Android Emulator via SDK Manager")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("✓ Android Emulator is installed")
|
||||
}
|
||||
|
||||
// Check NDK
|
||||
ndkHome := os.Getenv("ANDROID_NDK_HOME")
|
||||
if ndkHome == "" && androidHome != "" {
|
||||
// Look for NDK in default location
|
||||
ndkDir := filepath.Join(androidHome, "ndk")
|
||||
if entries, err := os.ReadDir(ndkDir); err == nil {
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
ndkHome = filepath.Join(ndkDir, entry.Name())
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ndkHome == "" {
|
||||
errors = append(errors, "Android NDK not found. Install NDK via Android Studio > SDK Manager > SDK Tools > NDK (Side by side)")
|
||||
} else {
|
||||
fmt.Printf("✓ Android NDK: %s\n", ndkHome)
|
||||
}
|
||||
|
||||
// Check Java
|
||||
if !checkCommand("java", "-version") {
|
||||
errors = append(errors, "Java not found. Install JDK 11+ (OpenJDK recommended)")
|
||||
} else {
|
||||
fmt.Println("✓ Java is installed")
|
||||
}
|
||||
|
||||
// Check for AVD (Android Virtual Device)
|
||||
if checkCommand("emulator", "-list-avds") {
|
||||
cmd := exec.Command("emulator", "-list-avds")
|
||||
output, err := cmd.Output()
|
||||
if err == nil && len(strings.TrimSpace(string(output))) > 0 {
|
||||
avds := strings.Split(strings.TrimSpace(string(output)), "\n")
|
||||
fmt.Printf("✓ Found %d Android Virtual Device(s)\n", len(avds))
|
||||
} else {
|
||||
fmt.Println("⚠ No Android Virtual Devices found. Create one via Android Studio > Tools > Device Manager")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
|
||||
if len(errors) > 0 {
|
||||
fmt.Println("❌ Missing dependencies:")
|
||||
for _, err := range errors {
|
||||
fmt.Printf(" - %s\n", err)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("Setup instructions:")
|
||||
fmt.Println("1. Install Android Studio: https://developer.android.com/studio")
|
||||
fmt.Println("2. Open SDK Manager and install:")
|
||||
fmt.Println(" - Android SDK Platform (API 34)")
|
||||
fmt.Println(" - Android SDK Build-Tools")
|
||||
fmt.Println(" - Android SDK Platform-Tools")
|
||||
fmt.Println(" - Android Emulator")
|
||||
fmt.Println(" - NDK (Side by side)")
|
||||
fmt.Println("3. Set environment variables:")
|
||||
if runtime.GOOS == "darwin" {
|
||||
fmt.Println(" export ANDROID_HOME=$HOME/Library/Android/sdk")
|
||||
} else {
|
||||
fmt.Println(" export ANDROID_HOME=$HOME/Android/Sdk")
|
||||
}
|
||||
fmt.Println(" export PATH=$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator")
|
||||
fmt.Println("4. Create an AVD via Android Studio > Tools > Device Manager")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("✓ All Android development dependencies are installed!")
|
||||
}
|
||||
|
||||
func checkCommand(name string, args ...string) bool {
|
||||
cmd := exec.Command(name, args...)
|
||||
cmd.Stdout = nil
|
||||
cmd.Stderr = nil
|
||||
return cmd.Run() == nil
|
||||
}
|
||||
18
build/android/settings.gradle
Normal file
@@ -0,0 +1,18 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
}
|
||||
}
|
||||
|
||||
dependencyResolutionManagement {
|
||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.name = "WailsApp"
|
||||
include ':app'
|
||||