Skip to content

Commit

Permalink
adding rough docs
Browse files Browse the repository at this point in the history
  • Loading branch information
trbutler4 committed Aug 17, 2024
1 parent ff85a27 commit 3ab6ced
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
Binary file added docs/src/assets/ndk-install.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions docs/src/getting-started/building-the-os.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ m

### Adding prebuilt binaries

#### Emulator

sync repo

```bash
Expand Down
136 changes: 136 additions & 0 deletions docs/src/getting-started/rust-in-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Rust In Android

Because there are a lot of starknet libraries in rust, and the beerus client
is written in rust, being able to run rust code on android is important
for reaching MVP.

The process for running a minimal rust "hello world" library on android is
as follows:

1. Get dependencies
- NDK
- https://github.com/bbqsrc/cargo-ndk
- cargo install cargo-ndk
- rustup target add x86_64-linux-android
- this is the only one we need although there are more
- install NDK from android studio as well
![installing ndk](../assets/ndk-install.png)
1. Update Cargo.toml
```
[lib]
crate-type = ["cdylib"]
```

```
[dependencies]
jni = "0.21.1"
```
1. Write Java side

```
package com.example.rust_example;
public class RustLib {
// define the interface for using the rust library below
public static native String hello(String input);
static {
// here we load the rust library we created
System.loadLibrary("example_rust_project");
}
}
```

1. Write rust side (Use javac to compile C headers for reference)


Use javac to compile the Java class into a C header.
```bash
javac
```

```
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_rust_example_RustLib */
#ifndef _Included_com_example_rust_example_RustLib
#define _Included_com_example_rust_example_RustLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_rust_example_RustLib
* Method: hello
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_rust_1example_RustLib_hello
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif
```

Now we implement this C header in rust.
```
use jni::{
objects::{JClass, JString},
sys::jstring,
JNIEnv,
};
use super::hello;
#[no_mangle]
pub extern "C" fn Java_com_example_rust_1example_RustLib_hello<'local>(
mut env: JNIEnv<'local>, // this is the class that owns our static method
_class: JClass<'local>,
input: JString<'local>,
) -> jstring {
// getting the input string from our android app out of java
let input: String = env
.get_string(&input)
.expect("Couldn't get java string!")
.into();
// creating a new java string to return to our android app
let output = env
.new_string(hello(&input))
.expect("Couldn't create a java string!");
output.into_raw()
}
```

1. Build for android

Run
```
make build-android
```

Copy the .so file to jni directory in android app.

Use the library like any other library.

```
package com.example.rust_example
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val result = RustLib.hello("TEST")
println(result)
}
}
```

0 comments on commit 3ab6ced

Please sign in to comment.