https://github.com/ssrlive/rust_on_android
https://github.com/ssrlive/rust_on_android
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ssrlive/rust_on_android
- Owner: ssrlive
- Created: 2022-10-06T14:48:12.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-22T12:10:25.000Z (10 months ago)
- Last Synced: 2025-03-21T03:34:41.468Z (3 months ago)
- Language: Java
- Size: 102 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# how to import rust code into android project
Just a demo, see the [commit #4](https://github.com/ssrlive/rust_on_android/commit/7fa92cd01b24258469ed173a33b593e8d472fe99) for the details.
## Android Studio
* Android Studio
* Android SDK Platform 31
* Android NDK 24.0.8215888
* Android SDK Build-Tools 31.0.0
* Android SDK Command-line Tools
* Android SDK Platform-Tools#### Config: Tools >> SDK Manager >> SDK Tools (middle tab):

## Rust
Install rust on your PC from [rustup](https://rustup.rs),
then add some Android targets (arm64, arm, x86_64, x86) for rust.
```
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
```
Uses [rust-android-gradle](https://github.com/mozilla/rust-android-gradle) plugin, so is built with the command:
```cli
gradlew cargoBuild# build release version
gradlew assembleRelease
```> Set `JAVA_HOME` environment variable
> ```
> Windows: set JAVA_HOME="C:\Program Files\Android\Android Studio\jbr"
> macOS: export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home/
> Linux: (needn't to anything, using the system settings.)
> ```### Function naming convention
In `src/lib.rs` you need to name the function according to the following naming convention in order to make it available in `Java`.
If the _Java_ function is called `greeting` and it is saved in a file named `RustBindings.java` pulled from package `com.example.myrustapp` then in _Rust_ the function name is:
| Java | package name | filename | function name |
| :--: | :-------------------: | :----------: | :-----------: |
| Java | com_example_myrustapp | RustBindings | greeting |Which would look like this:
`Java_com_example_myrustapp_RustBindings_greeting(...)`
## Python
Install [Python](https://www.python.org/downloads/) on your PC.
> In `macOS` Monterey 12.3 and above, python was removed by Apple, you must install [Python3](https://www.python.org/downloads/) by yourself, then run this command to make python3 as python.
> ```
> ln -s -f /usr/local/bin/python3 /usr/local/bin/python
> ```