HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like4 BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Cargo-mobile for Wry We evaluate [cargo-mobile](https://github.com/tauri-apps/cargo-mobile) to create a mobile project for both Xcode and Android studio.Right now it provides two templates `tauri` (iOS only at the moment) and `wry` (both iOS and Android). ## Prerequisite - Works on **Linux**, **Windows**, **macOS**, and **WSL**(Windows subsystem for Linux). - **Xcode** and [**Android Studio**](https://developer.android.com/studio) installed properly. This is **the most difficult** part imho. This means all toolchains and SDK are all installed. - https://developer.apple.com/forums/thread/47843 - xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance ## Setting up Android Environment ### 1. Installing JDK #### Using Android Studio: If you have Android Studio installed, it ships with a version of JDK so you don't have to install it manually. It is usually at `<location of android studio installation>/jre` and can be used for `JAVA_HOME` env var. > you can find it on Windows at `C:\Program Files\Android\Android Studio\jre` #### Using the terminal: ##### Linux/WSL - Install it by running the following command based on your distro to install JDK: - debian ``` sudo apt install default-jdk ``` - arch (I use arch btw) ``` sudo pacman -S jdk-openjdk ``` - Set the `JAVA_HOME` env variable for this current shell (we will make it permanent later on) ```bash export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" ``` #### macOS - Install openjdk from Homebrew: ``` brew install openjdk ``` - Link to system Java wrapper and set the `JAVA_HOME` env: ``` sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk export JAVA_HOME="/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home" ``` ##### Windows - Download openjdk-11 ```powershell cd $HOME\downloads Invoke-WebRequest https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_windows-x64_bin.zip -o openjdk-11.zip Expand-Archive openjdk-11.zip -d . mkdir $env:LocalAppData\Java mv jdk-11.0.2 $env:LocalAppData\Java ``` - Set the `JAVA_HOME` env variable for this current shell (we will make it permanent later on) ```powershell $env:JAVA_HOME="$env:LocalAppData\Java\jdk-11.0.2" ``` ### 2. Installing Android SDK and NDK There is two ways to install the sdk and ndk. #### Using Android Studio: You can use the SDK Manager in Android Studio to install: > Note: you may need to tick `Show Package Details` in the right bottom corner to be able to see some of these components 1. Android Sdk Platform 33 2. Android SDK Platform-Tools 3. NDK (Side by side) 25.0.8775105 5. Android SDK Build-Tools 33.0. 6. Android SDK Command-line Tools #### Using the terminal: If you don't want or can't use Android Studio you can still get the SDK Manager cli quite easily and use it to install other components. > Note: The SDK Manager is part of the "Command line tools only" that can be downloaded from [here](https://developer.android.com/studio#command-tools) ##### Linux/WSL/macOS Download the `cmdline-tools` ```bash cd ~/Downloads # if you are on Linux/WSL: wget https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip -O # if you are on macos: wget https://dl.google.com/android/repository/commandlinetools-mac-8512546_latest.zip -O unzip cmdline-tools.zip cd cmdline-tools mkdir latest mv bin latest/ mv lib latest/ mv NOTICE.txt latest/ mv source.properties latest/ cd .. mkdir ~/.android # You can use another location for your SDK but I prefer using ~/.android mv cmdline-tools ~/.android ``` Install required SDK and NDK components ```bash export ANDROID_SDK_ROOT="$HOME/.android" ~/.android/cmdline-tools/latest/bin/sdkmanager "platforms;android-33" "platform-tools" "ndk;25.0.8775105" "build-tools;33.0.0" ``` ##### Windows Download the `cmdline-tools` ```powershell cd $HOME\downloads Invoke-WebRequest https://dl.google.com/android/repository/commandlinetools-win-8512546_latest.zip -o cmdline-tools.zip Expand-Archive cmdline-tools.zip -d . cd cmdline-tools mkdir latest mv bin latest/ mv lib latest/ mv NOTICE.txt latest/ mv source.properties latest/ cd .. mkdir $HOME\.android # You can use another location for your SDK but I prefer using $HOME\.android mv cmdline-tools $HOME\.android ``` Install required SDK and NDK components ```powershell $env:ANDROID_SDK_ROOT="$HOME\.android" &"$env:ANDROID_SDK_ROOT\cmdline-tools\latest\bin\sdkmanager.exe" "platforms;android-33" "platform-tools" "ndk;25.0.8775105" "build-tools;33.0.0" ``` > Note: the location you moved the `cmdline-tools` directory into will be the location of your android SDK. ### 3. Setting up Environment Variables You'll need to set up some environment variables to get everything to work properly. The environment variables below should be all the ones your need to be able to use [cargo-mobile](https://github.com/tauri-apps/cargo-mobile) to build/run your android app. ##### Linux/WSL/macOS ```bash # .bashrc or .zshrc export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" # if you are using Android studio, the location is different, see the section above about JDK export ANDROID_SDK_ROOT="$HOME/.android" # if you are using Android studio, the sdk location will be at `~/Android/Sdk` export NDK_HOME="$ANDROID_SDK_ROOT/ndk/25.0.8775105" export PATH="$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin" export PATH="$PATH:$ANDROID_SDK_ROOT/platform-tools" ``` > For WSL: > you also need to get ADB to connect to your emulator that is running on Windows > ```bash > export WSL_HOST="192.168.1.2" # Run `ipconfig` in windows to get your computer IP > export ADB_SERVER_SOCKET=tcp:$WSL_HOST:5037 > ``` After updating `.bashrc` either run `source ~/.bashrc` or reopen your terminal to apply the changes. ##### Windows Open a powershell instance and run the following commands in order ```powershell Function Add-EnvVar($name, $value) { [System.Environment]::SetEnvironmentVariable("$name", "$value", "User") } Function Add-PATHEntry($path) { $newPath = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + $path; [System.Environment]::SetEnvironmentVariable("Path", "$newPath", "User") } Add-EnvVar JAVA_HOME "$env:LocalAppData\Java\jdk-11.0.2" # if you are using Android studio, the location is different, see the section above about JDK $env:SDK_ROOT="$HOME\.android"# if you are using Android studio, the sdk location will be at `$env:LocalAppData\Android\Sdk` Add-EnvVar ANDROID_SDK_ROOT "$env:SDK_ROOT" Add-EnvVar NDK_HOME "$env:SDK_ROOT\ndk\25.0.8775105" Add-PATHEntry "$env:SDK_ROOT\cmdline-tools\latest\bin" Add-PATHEntry "$env:SDK_ROOT\platform-tools" ``` > IMPORTANT: you need to reboot your Windows machine in order for the environement variables to be loaded correctly. You should now have all the environment variables required and the cmdline-tools available in your PATH. You can verify this by running `sdkmanager` which should now be showing its help info. ### 4. Install Rust android targets: ```shell rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android ``` ## Getting Started Now lets bootstrap a project to develop a tauri or wry project for mobile. - Install our [fork](https://github.com/tauri-apps/cargo-mobile) of [cargo-mobile](https://github.com/BrainiumLLC/cargo-mobile) by running: ```bash cargo install --git https://github.com/tauri-apps/cargo-mobile ``` - Create a directory and init the project. ``` bash mkdir hello cd hello cargo mobile init # Project name (hello): # Stylized name (Hello): # Domain (example.com): tauri.app # Detected template packs: # [0] tauri # [1] wry # Enter an index for a template pack above. # Template pack (0): ``` ## Build and Run on Device ### Android > Make sure you're device is connected to adb > you can check by running `cargo android list` or `adb devices` - `cargo android run` ### iOS - `cargo apple run` First time running the app will be blocked. Go to your phone's `Settings > Privacy & Security > Developer Mode` to enable developer mode. `Settings -> General -> VPN and device management -> From "Developer App"` section found "Apple Development: APPLE_ID" -> Trust ## Build and Run on Emulator ### Android ##### Using Android Studio - Open the project in Android Studio `cargo android open` - Click `Trust Project`, `Use Embedded JDK` - (optional?) From my setup, I also need to add `abiFilters += listOf("arm64-v8a")` under `create("arm")` branch in `:app`'s '`build.gradle.kts`. - Choose an emulator. I usually choose Pixel 4 API 32 - (optional) if you face this error `Device supports x86, but APK only supports armeabi-v7a` then check this [Stack Overflow answer](https://stackoverflow.com/questions/41775988/what-is-the-reason-for-the-error-device-supports-x86-but-apk-only-supports-arm/43742161#43742161) to fix it. - Press run button. ##### Without Android Studio If you don't have access to Android Studio or don't want or when running in WSL, you can build and run the generated project directly from the terminal 1. List available emulators - Linux/WSL/macOS: ```bash $ANDROID_SDK_ROOT/emulator/emulator -list-avds ``` - Windows: ```powershell &"$env:ANDROID_SDK_ROOT\emulator\emulator" -list-avds ``` you should now see a list of available emulators like the following, you'll need one of them for the next step: ``` Resizable_API_33 Pixel_5_API_33 ``` 2. Start the emulator with the name of the desired emulator: - Linux/WSL/macOS: ```bash $ANDROID_SDK_ROOT/emulator/emulator -avd Resizable_API_33 ``` - Windows: ```powershell &"$env:ANDROID_SDK_ROOT\emulator\emulator" -avd Resizable_API_33 ``` 3. In a new terminal window, run: ```bash cargo android run ``` ### iOS - If you are on x86_64: `cargo build --target x86_64-apple-ios` - If you are on M1: `cargo build --target aarch64-apple-ios-sim` - `cargo apple open` - Choose a simulator. I usually choose iPhone 12 (14.4). - Press run button. ## Custom Protocols ### Android Same as others. TODO: add how does its scheme actually looks like. ### iOS Same as macOS. To get the path of your assets, you can call [`CFBundle::resources_path`](https://docs.rs/core-foundation/latest/core_foundation/bundle/struct.CFBundle.html#method.resources_path). So url like `wry://assets/index.html` could get the html file in assets directory. ## IPC Both support `ipc.PostMessage()`. ## Devtools Set `devtools` attribute to true when building webview. ### Android Open `chrome://inspect/#devices` in Chrome to get the devtools window. ### iOS Open Safari > Develop > [Your Device Name] > [Your WebView].

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully