This file contains hidden or 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
[package] | |
name = "rustylib" | |
version = "0.1.0" | |
authors = ["Roberto Huertas <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[lib] | |
name = "rustylib" |
This file contains hidden or 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
use std::ffi::{CStr, CString}; | |
use std::os::raw::c_char; | |
#[no_mangle] | |
pub unsafe extern "C" fn hello(to: *const c_char) -> *mut c_char { | |
let c_str = CStr::from_ptr(to); | |
let recipient = match c_str.to_str() { | |
Ok(s) => s, | |
Err(_) => "you", | |
}; |
This file contains hidden or 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
#include <stdarg.h> | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
char *hello(const char *to); | |
void hello_release(char *s); |
This file contains hidden or 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
import SwiftUI | |
struct ContentView: View { | |
let s = getName() | |
var body: some View { | |
Text(s) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { |
This file contains hidden or 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
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
let result = hello("Rob") | |
let s_result = String(cString: result!) | |
// IMPORTANT: once we get the result we have to release the pointer. |
This file contains hidden or 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
#![cfg(target_os = "android")] | |
#![allow(non_snake_case)] | |
use crate::hello; | |
use jni::objects::{JClass, JString}; | |
use jni::sys::jstring; | |
use jni::JNIEnv; | |
use std::ffi::CString; | |
// NOTE: RustKt references the name rusty.kt, which will be the kotlin file exposing the functions below. |
This file contains hidden or 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
#!/usr/bin/env bash | |
# set the version to use the library | |
min_ver=22 | |
# verify before executing this that you have the proper targets installed | |
cargo ndk --target aarch64-linux-android --android-platform ${min_ver} -- build --release | |
cargo ndk --target armv7-linux-androideabi --android-platform ${min_ver} -- build --release | |
cargo ndk --target i686-linux-android --android-platform ${min_ver} -- build --release | |
cargo ndk --target x86_64-linux-android --android-platform ${min_ver} -- build --release | |
# moving libraries to the android project |
This file contains hidden or 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
package com.robertohuertas.rusty_android_lib | |
external fun hello(to: String): String | |
external fun helloDirect(to: String): String | |
fun loadRustyLib() { | |
System.loadLibrary("rustylib") | |
} |
This file contains hidden or 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
package com.robertohuertas.rusty_android | |
import android.os.Bundle | |
import com.google.android.material.snackbar.Snackbar | |
import androidx.appcompat.app.AppCompatActivity | |
import android.view.Menu | |
import android.view.MenuItem | |
import android.widget.TextView | |
import kotlinx.android.synthetic.main.activity_main.* |
This file contains hidden or 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
package com.robertohuertas.rusty_flutter_lib | |
// importing the Android library | |
import com.robertohuertas.rusty_android_lib.* | |
import io.flutter.plugin.common.MethodCall | |
import io.flutter.plugin.common.MethodChannel | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | |
import io.flutter.plugin.common.MethodChannel.Result | |
import io.flutter.plugin.common.PluginRegistry.Registrar |