-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,8 +54,6 @@ m | |
|
||
### Adding prebuilt binaries | ||
|
||
#### Emulator | ||
|
||
sync repo | ||
|
||
```bash | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
``` |