Skip to content

Instantly share code, notes, and snippets.

View robertohuertasm's full-sized avatar
🦀
friendly rustacean

Roberto Huertas robertohuertasm

🦀
friendly rustacean
View GitHub Profile
@robertohuertasm
robertohuertasm / Cargo.toml
Last active November 3, 2019 10:40
rust_for_android_ios_flutter
[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"
@robertohuertasm
robertohuertasm / lib.rs
Created November 3, 2019 10:43
rust_for_android_ios_flutter
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",
};
@robertohuertasm
robertohuertasm / rustylib.h
Created November 3, 2019 10:45
rust_for_android_ios_flutter
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
char *hello(const char *to);
void hello_release(char *s);
@robertohuertasm
robertohuertasm / ContentView.swift
Created November 3, 2019 10:49
rust_for_android_ios_flutter
import SwiftUI
struct ContentView: View {
let s = getName()
var body: some View {
Text(s)
}
}
struct ContentView_Previews: PreviewProvider {
@robertohuertasm
robertohuertasm / ViewController.swift
Created November 3, 2019 10:50
rust_for_android_ios_flutter
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.
@robertohuertasm
robertohuertasm / android.rs
Created November 3, 2019 10:54
rust_for_android_ios_flutter
#![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.
@robertohuertasm
robertohuertasm / android_build.sh
Created November 3, 2019 10:56
rust_for_android_ios_flutter
#!/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
@robertohuertasm
robertohuertasm / rusty.kt
Created November 3, 2019 10:59
rust_for_android_ios_flutter
package com.robertohuertas.rusty_android_lib
external fun hello(to: String): String
external fun helloDirect(to: String): String
fun loadRustyLib() {
System.loadLibrary("rustylib")
}
@robertohuertasm
robertohuertasm / MainActivity.kt
Created November 3, 2019 11:03
rust_for_android_ios_flutter
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.*
@robertohuertasm
robertohuertasm / RustyFlutterLibPlugin.kt
Created November 3, 2019 11:06
rust_for_android_ios_flutter
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