Jekyll2021-07-14T23:10:36+00:00https://dev.brainiumstudios.com/feed.xmlBrainium Dev Blogcargo-mobile: Rust on mobile made easy!2020-11-24T18:00:00+00:002020-11-24T18:00:00+00:00https://dev.brainiumstudios.com/2020/11/24/cargo-mobile<p><strong><em>The answer to “how do I use Rust on iOS and Android?”</em></strong></p>
<p>For nearly 2 years, we’ve been perfecting a tool called <a href="https://github.com/BrainiumLLC/cargo-mobile">cargo-mobile</a>. It’s a framework-agnostic tool that generates all the boilerplate of a Rust mobile project, and is packed full of handy commands for building and running on mobile devices.</p>
<p>If you’d like to follow along, installation is easy:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cargo <span class="nb">install</span> <span class="nt">--git</span> https://github.com/BrainiumLLC/cargo-mobile
</code></pre></div></div>
<p>Note that cargo-mobile currently only works on macOS! If you shoot us a PR that adds support for Linux or Windows, we’ll shower you in heart emoji. Feel free to ask us for guidance if you’re interested!</p>
<p>Additionally, until Rust 1.49.0 is released, you can’t build for iOS unless you’re using <a href="https://github.com/BrainiumLLC/cargo-mobile#status">the current beta</a>.</p>
<p>To start a new project, you just need to run <code class="language-plaintext highlighter-rouge">cargo mobile init</code>:</p>
<figure class="terminal">
<img alt="`cargo mobile init` demo" src="/assets/cargo-mobile/init.svg" />
</figure>
<p>Just like that, you have a version of <a href="https://bevyengine.org/">Bevy</a>’s <a href="https://github.com/bevyengine/bevy/blob/master/examples/game/breakout.rs">Breakout example</a> that can run out of the box on desktop, iOS, and Android!</p>
<p>As usual, you can use <code class="language-plaintext highlighter-rouge">cargo run</code> to run on desktop:</p>
<p><a title="`cargo run` demo" href="/assets/cargo-mobile/desktop.png">
<img alt="`cargo run` demo" title="`cargo run` demo" src="/assets/cargo-mobile/desktop.png" />
</a></p>
<p>More excitingly, it’s easy to run on iOS and Android!</p>
<h2 id="ios"><a href="#ios">iOS</a></h2>
<p>We provide a convenience command for opening your project in Xcode:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cargo apple open
</code></pre></div></div>
<p>From there, you can build your project like you normally would in Xcode! Just hit that Play button. It’ll work on Simulator, too.</p>
<p><a title="Xcode project" href="/assets/cargo-mobile/xcode.png">
<img alt="Xcode project" title="Xcode project" src="/assets/cargo-mobile/xcode.png" />
</a></p>
<p>You can even set breakpoints!</p>
<p><a title="Xcode breakpoints" href="/assets/cargo-mobile/breakpoints.png">
<img alt="Xcode breakpoints" title="Xcode breakpoints" src="/assets/cargo-mobile/breakpoints.png" />
</a></p>
<p>If you’re not a fan of opening Xcode, you can also run directly from the command line. Be warned that the output might contain some spurious errors; we’re still improving that! Also, you can’t currently use Simulator unless you’re running from Xcode.</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cargo apple run
</code></pre></div></div>
<p>Whether you run from Xcode or the command line, you should ultimately be greeted by Breakout:</p>
<p><a title="Running on an iPhone XR" href="/assets/cargo-mobile/iphone.jpg">
<img alt="Running on an iPhone XR" title="Running on an iPhone XR" src="/assets/cargo-mobile/iphone.jpg" />
</a></p>
<p>We provide several other helpful iOS commands, which are listed in our help info:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cargo apple
</code></pre></div></div>
<h2 id="android"><a href="#android">Android</a></h2>
<p>Naturally, we also provide a convenience command for opening your project in Android Studio:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cargo android open
</code></pre></div></div>
<p>Once again, you just need to hit the Run button like you normally would.</p>
<p><a title="Android Studio project" href="/assets/cargo-mobile/android-studio.png">
<img alt="Android Studio project" title="Android Studio project" src="/assets/cargo-mobile/android-studio.png" />
</a></p>
<p>Alternatively, you can skip Android Studio and run directly from the command line:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cargo android run
</code></pre></div></div>
<p>Either way, it should pop up on your connected device:</p>
<p><a title="Running on a Pixel 4" href="/assets/cargo-mobile/pixel.jpg">
<img alt="Running on a Pixel 4" title="Running on a Pixel 4" src="/assets/cargo-mobile/pixel.jpg" />
</a></p>
<p>You can find more Android commands in our help info:</p>
<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cargo android
</code></pre></div></div>
<h2 id="going-forward"><a href="#going-forward">Going forward</a></h2>
<p>Be advised that Bevy’s mobile support is still <a href="https://bevyengine.org/news/bevy-0-3/">very new</a>, so the Breakout demo doesn’t work perfectly yet. They’re doing an amazing job nonetheless! We’ve been following their progress, and a lot of contributors worked very hard to make it happen.</p>
<p>We’d love to see PRs that add template packs for other engines and frameworks, so that cargo-mobile is convenient for anyone and everyone to use. We’d also love to show you a demo using Brainstorm, our own internal mobile-first game engine, but we’re not ready to open source it just yet!</p>
<p>If you have any questions, issues, or ideas, feel free to <a href="https://github.com/BrainiumLLC/cargo-mobile/issues">open an issue</a>!</p>Francesca LovebloomThe answer to “how do I use Rust on iOS and Android?”