avatarKofi Otuo

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1833

Abstract

/8b1dd003c24465d41ad13a5a1e951c66.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><p id="3b7d"><code>Self</code> in method arguments is <i>syntactic sugar</i> for the receiving type of the method, that is, the type whose <code>impl</code> this method is in, and in this case, UIit is <code>Inputs</code>. You could replace Self with <code>Inputs</code> and the code would work fine.</p><p id="6188">Remember <code>#[generate_interface]</code> originates from the <a href="https://crates.io/crates/rifgen"><co<b>de>rifgen</co<b></a> crate as described in the <a href="https://blog.devgenius.io/integrating-rust-with-android-development-ef341c2f9cca">first part of the series</a>.</p><p id="c9c8">Now let us implement the U.I.</p><h2 id="f210">Designing the U.I. 📱</h2><p id="d49a">As described earlier, the U.I. would have 2 input text fields and 3 output text fields. It would be a simple layout to illustrate:</p> <figure id="a424"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Kofituo/2f0cb830eb039744b3e67e61298f5c83.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="8508">And that’s it! This is what the application should <a href="https://github.com/Kofituo/RustApplication/blob/main/app/src/main/java/com/example/rustapplication/Illustration.jpg"><b>look like</b></a>. Here’s a gif of it</p>
    <figure id="73bf">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=http

Options

s%3A%2F%2Fgfycat.com%2Fifr%2Ffeistyvaluablechinesecrocodilelizard&display_name=Gfycat&url=https%3A%2F%2Fgfycat.com%2Ffeistyvaluablechinesecrocodilelizard&image=https%3A%2F%2Fthumbs.gfycat.com%2FFeistyValuableChinesecrocodilelizard-size_restricted.gif&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=gfycat" allowfullscreen="" frameborder="0" height="546" width="246"> </div> </div> </figure></iframe></div></div></figure><p id="4b8f">Whenever the user taps the ‘equal to’ button, the numbers from the text boxes are passed to the struct written in <code>Rust</code>. The values are then computed and the result is displayed in the textbox. Feel free to modify the code and try out other ways of adding some <code>Rust</code> variety to your Android project.</p><h2 id="34c1">Next Steps 👣</h2><p id="3092"><b>If you faced any problems while trying this walkthrough or have any questions, feel free to reach me via email at [email protected]!</b></p><h2 id="be42">Support</h2><p id="b5d3">If you’ve enjoyed this series and would like to show gratitude, you can support the author through <a href="https://bmc.link/otuokofi"><b>Buy Me A Coffee</b></a><b>.</b></p><h2 id="3b92">Further Reading 📖</h2><p id="78b7">Learn more about the dependencies used in this walkthrough:</p><ul><li><a href="https://github.com/Dushistov/flapigen-rs"><b>Dushistov/flapigen-rs: Tool for connecting programs or libraries written in Rust with other languages</b></a></li><li><a href="https://github.com/Kofituo/rifgen"><b>Kofituo/rifgen:Program for translating libraries written in Rust to interface files. It works with flapigen</b></a></li><li><a href="https://github.com/sfackler/rust-jni-sys"><b>sfackler/rust-jni-sys</b></a></li></ul><h2 id="d4d6">Repository</h2></article></body>

Integrating Rust With Android Development — Final Part

This is the second and final part of the series which explains how to add Rust code to your Android project without the hassle of studying or utilizing JNI, or C in your project. In the first part of the series, we created a simple Android Studio project and a simple library written in Rust to log information. In this second and final part, we shall go through a practical example where we apply Rust functions to our work.

In this walkthrough, we would create a simple calculator app that evaluates the result when the user presses the ‘equal to’ button. The application would take 2 inputs and then perform 3 operations on the inputs: Addition, Subtraction, and Multiplication. This simple knowledge would be helpful in real-life cases where you might have to perform time-consuming or low-level operations on Android. Without further ado, let’s create the backend of our application (You could design the U.I initially, but for this walkthrough, I feel writing the Rust backend first is more convenient)

Designing The Backend ⚙️

As summarized earlier, we’d create a struct that holds 2 values and also create functions that would return the corresponding addition, subtraction, and multiplication. We would add this struct to lib.rs.

Self in method arguments is syntactic sugar for the receiving type of the method, that is, the type whose impl this method is in, and in this case, UIit is Inputs. You could replace Self with Inputs and the code would work fine.

Remember #[generate_interface] originates from the de>rifgen crate as described in the first part of the series.

Now let us implement the U.I.

Designing the U.I. 📱

As described earlier, the U.I. would have 2 input text fields and 3 output text fields. It would be a simple layout to illustrate:

And that’s it! This is what the application should look like. Here’s a gif of it

Whenever the user taps the ‘equal to’ button, the numbers from the text boxes are passed to the struct written in Rust. The values are then computed and the result is displayed in the textbox. Feel free to modify the code and try out other ways of adding some Rust variety to your Android project.

Next Steps 👣

If you faced any problems while trying this walkthrough or have any questions, feel free to reach me via email at [email protected]!

Support

If you’ve enjoyed this series and would like to show gratitude, you can support the author through Buy Me A Coffee.

Further Reading 📖

Learn more about the dependencies used in this walkthrough:

Repository

Software Development
Software Engineering
Rust
Android
Kotlin
Recommended from ReadMedium