This a simple hybrid app for Android that implements image filters (Sobel and Gaussian blur) in WebAssembly. The goal is to test the performance of computationally intensive algorithms implemented in JS and WASM.
The web app is implemented using React and Material UI. The native platform APIs are provided by CapacitorJS. The WASM binaries are compiled with Emscripten.
npm install
npm run dev
npm run build
AndroidStudio installed on your system is required
npx cap add android
npx cap sync
export CAPACITOR_ANDROID_STUDIO_PATH="...../android-studio/bin/studio.sh"
npx cap open android
For creating and configuring a new hybrid application the following steps are needed:
npm create vite@latest
cd APP_NAME
npm install
npm install @mui/material @emotion/react @emotion/styled
Emscripten is portable, so, should be installed in another folder.
cd .. # Exit the project folder
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
Assuming the .c files in the /wasm folder
mkdir -p public/wasm # Static files should be in public folder (see Vite doc)
cd wasm # Where the C project is
emcc main.c -O2 -sSTANDALONE_WASM -o main.wasm --no-entry
cp main.wasm ../public/wasm # Copy binary to public/wasm
cd .. # Back to root
After running these commands, wasm modules can be instantiated anywhere in the app using:
let wasmInstance;
fetch('wasm/main.wasm').then(response => {
response.arrayBuffer().then(bytes => {
WebAssembly.instantiate(bytes, {})
.then(module => {
wasmInstance = module.instance.exports;
// wasmInstance is ready.
})
});
});
npm install @capacitor/core @capacitor/cli
npx cap init # When running this, set 'dist' as source directory
npm install @capacitor/android
npx cap add android
npm run dev
npm run build && npx cap sync
After running these commands, check if content of 'public' folder was included in the 'dist' directory.
Once the project is open, compiling, running and signing operations can be done from AndroidStudio
export CAPACITOR_ANDROID_STUDIO_PATH="...../android-studio/bin/studio.sh"
npx cap open android