<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anton Rapetov</title>
    <description>The latest articles on DEV Community by Anton Rapetov (@willir).</description>
    <link>https://dev.to/willir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F181013%2Fb623393b-83a2-4e57-9355-dd1d8c905e5e.png</url>
      <title>DEV Community: Anton Rapetov</title>
      <link>https://dev.to/willir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/willir"/>
    <language>en</language>
    <item>
      <title>One more plugin to build Rust for Android</title>
      <dc:creator>Anton Rapetov</dc:creator>
      <pubDate>Sun, 24 Nov 2019 14:40:59 +0000</pubDate>
      <link>https://dev.to/willir/one-more-plugin-to-build-rust-for-android-125h</link>
      <guid>https://dev.to/willir/one-more-plugin-to-build-rust-for-android-125h</guid>
      <description>&lt;p&gt;There are already several good articles on how to develop on Rust for Android: &lt;a href="https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-21-rust-on-android.html"&gt;Building and Deploying a Rust library on Android&lt;/a&gt; by Mozilla, and &lt;a href="https://dev.to/robertohuertasm/rust-once-and-share-it-with-android-ios-and-flutter-286o"&gt;Rust once and share it with Android, iOS, and Flutter&lt;/a&gt; by Roberto Huertas.&lt;/p&gt;

&lt;p&gt;However, they describe how to build Rust manually (from the command line). That might be not the most convenient way to do it, since it makes the whole compilation and installing a several step process. I would prefer if Rust compilation was done automatically by &lt;code&gt;gradle&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As it happens there is already such plugin: &lt;a href="https://github.com/mozilla/rust-android-gradle"&gt;Rust Android Gradle&lt;/a&gt; by Mozilla. It is very good, but it misses one thing - it doesn't allow you to configure different compilation options for different gradle &lt;code&gt;buildType&lt;/code&gt;s. For me it is crucial, so I decided to write my own plugin.&lt;/p&gt;

&lt;p&gt;Here it is &lt;a href="https://github.com/willir/cargo-ndk-android-gradle"&gt;Cargo NDK for Android projects&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's see how you can use it. I won't spend much time talking about details that were already covered in the article above. I will only cover the main plugin use cases in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing dependencies
&lt;/h2&gt;

&lt;p&gt;I assume that the Rust compiler itself is installed.&lt;/p&gt;

&lt;p&gt;To develop for Android you will need Android Studio. It can be downloaded from its official site: &lt;a href="https://developer.android.com/studio"&gt;Android Studio&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also, I would suggest installing the Rust plugin: &lt;a href="https://intellij-rust.github.io/"&gt;IntelliJ Rust&lt;/a&gt;, &lt;a href="https://plugins.jetbrains.com/plugin/8195-toml/"&gt;Toml&lt;/a&gt;. Currently (to my opinion) it works better than &lt;code&gt;rls&lt;/code&gt; + &lt;code&gt;vscode&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once Android Studio is installed you also need NDK. Here are official instructions on how to install NDK: &lt;a href="https://developer.android.com/studio/projects/install-ndk"&gt;Install NDK&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next step is to add android targets for rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And to install &lt;code&gt;cargo-ndk&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo &lt;span class="nb"&gt;install &lt;/span&gt;cargo-ndk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;cargo-ndk&lt;/code&gt; automatically finds and sets all appropriate environment variables to build rust libraries for android. So we won't need to specify them manually in &lt;code&gt;~/.cargo/config&lt;/code&gt; or somewhere else.&lt;/p&gt;

&lt;p&gt;We also need to set the &lt;code&gt;NDK_HOME&lt;/code&gt; environment variable, as &lt;a href="https://github.com/bbqsrc/cargo-ndk#usage"&gt;&lt;code&gt;cargo ndk&lt;/code&gt;&lt;/a&gt; describes it. On my Linux, it is: &lt;code&gt;$HOME/Android/Sdk/ndk-bundle&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gradle Build Config
&lt;/h2&gt;

&lt;p&gt;I'll assume that you already have a Rust code to build, so here I'll only cover the gradle config.&lt;/p&gt;

&lt;p&gt;The plugin is published on the gradle repo, so its usage is more or less straight forward. First in &lt;code&gt;build.gradle&lt;/code&gt; we add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;buildscript&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;repositories&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
        &lt;span class="n"&gt;maven&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="s2"&gt;"https://plugins.gradle.org/m2/"&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;...&lt;/span&gt;
        &lt;span class="n"&gt;classpath&lt;/span&gt; &lt;span class="s2"&gt;"gradle.plugin.com.github.willir.rust:plugin:0.1.1"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then in &lt;code&gt;app/build.gradle&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="nl"&gt;plugin:&lt;/span&gt; &lt;span class="s2"&gt;"com.github.willir.rust.cargo-ndk-android"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that's it, now the rust library should compile along with the rest Android project.&lt;/p&gt;

&lt;p&gt;By default &lt;code&gt;buildDebug&lt;/code&gt; gradle task will build &lt;code&gt;dev&lt;/code&gt; (&lt;code&gt;debug&lt;/code&gt;) cargo profile, and &lt;code&gt;buildRelease&lt;/code&gt; will build &lt;code&gt;release&lt;/code&gt; cargo profile. It can be changed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;cargoNdk&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;buildTypes&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;release&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;buildType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"release"&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;debug&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;buildType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"release"&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, for both &lt;code&gt;buildType&lt;/code&gt;s &lt;code&gt;release&lt;/code&gt; profile is going to be built.&lt;/p&gt;

&lt;p&gt;The full list of options can be found on the GitHub page: &lt;a href="https://github.com/willir/cargo-ndk-android-gradle#all-options"&gt;All Options&lt;/a&gt;. Any of those options can be overridden for a specific &lt;code&gt;buildType&lt;/code&gt;. Let's talk about some features, that might be helpful during development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Specify target via gradle property
&lt;/h2&gt;

&lt;p&gt;Sometimes, during development, you don't need to build for all targets (&lt;code&gt;arm&lt;/code&gt;, &lt;code&gt;arm64&lt;/code&gt;), since you only test on one specific phone, so you don't want to spend time waiting for unused targets to be built. In this case, you can specify gradle property &lt;code&gt;rust-target&lt;/code&gt; to the target you're testing right now. E.g.: &lt;code&gt;gradle buildDebug -Prust-target=arm64&lt;/code&gt;. If &lt;code&gt;rust-target&lt;/code&gt; is present plugin will only build for that target.&lt;/p&gt;

&lt;h2&gt;
  
  
  apiLevel
&lt;/h2&gt;

&lt;p&gt;By default &lt;code&gt;apiLevel&lt;/code&gt; equals the &lt;code&gt;android.defaultConfig.minSdkVersion&lt;/code&gt;, as far as I can tell, it is the most sensible option. It can be overridden.&lt;/p&gt;

&lt;p&gt;However, there is one catch - android supports 64-bit targets only since API &lt;code&gt;21&lt;/code&gt;. So, for example, for API &lt;code&gt;19&lt;/code&gt; you cannot build &lt;code&gt;arm64&lt;/code&gt; and &lt;code&gt;x86_64&lt;/code&gt;. It still makes sense to build &lt;code&gt;arm64&lt;/code&gt; and &lt;code&gt;x86_64&lt;/code&gt;, even if your &lt;code&gt;minSdkVersion&lt;/code&gt; is 19, they won't work on Android 4.4, but on Android 5.0+ they will work, and that's a lot of phones. So if your apiLevel is &lt;code&gt;19&lt;/code&gt;, and you build &lt;code&gt;arm64&lt;/code&gt;, &lt;code&gt;x86_64&lt;/code&gt; targets, the plugin automatically increases &lt;code&gt;apiLevel&lt;/code&gt; to &lt;code&gt;21&lt;/code&gt; for those targets.&lt;/p&gt;




&lt;p&gt;That is basically it, I would like if somebody finds this plugin helpful. If you find any bugs, please report them, I'll try to fix them as soon as possible.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>android</category>
      <category>cargo</category>
      <category>ndk</category>
    </item>
  </channel>
</rss>
