Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
add rust lib support
Browse files Browse the repository at this point in the history
  • Loading branch information
Siyun Wu committed Nov 27, 2021
1 parent 7f0c9e2 commit 84e9ba0
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}

dependencies {

implementation project(':rsdroid')
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ package com.linkedin.android.kotinrustproto

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.linkedin.android.kotinrustproto.databinding.ActivityMainBinding
import com.linkedin.android.rsdroid.RustCore

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater);
binding.text.text = RustCore.instance.greeting();

binding.button.setOnClickListener(View.OnClickListener {
RustCore.instance.callback(object : RustCore.Callback {
override fun onSuccess() {
binding.text.text = "Changed";
}
});
})
setContentView(binding.root);
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/text"
android:text="Callback"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildscript {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.17'

classpath 'gradle.plugin.org.mozilla.rust-android-gradle:plugin:0.9.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
1 change: 1 addition & 0 deletions rsdroid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions rsdroid/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android' // required for aar generation to link to from AnkiDroid

android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
ndkVersion "22.1.7171670" // Used by GitHub actions - avoids an install step on some machines

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName VERSION_NAME

consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

// Consider upgrade to DSL: https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block
apply plugin: "org.mozilla.rust-android-gradle.rust-android"

cargo {
module = "../rslib-bridge"
libname = "rsdroid"
targets = ["x86", "arm", "arm64"]
profile = 'release'
prebuiltToolchains = true
apiLevel = 21
verbose = true
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar", '*.so'])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.31"
}

preBuild.dependsOn "cargoBuild"
Empty file added rsdroid/consumer-rules.pro
Empty file.
1 change: 1 addition & 0 deletions rsdroid/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION_NAME=0.1.0
21 changes: 21 additions & 0 deletions rsdroid/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
5 changes: 5 additions & 0 deletions rsdroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.linkedin.android.rsdroid">


</manifest>
20 changes: 20 additions & 0 deletions rsdroid/src/main/java/com/linkedin/android/rsdroid/RustCore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.linkedin.android.rsdroid;

class RustCore {

external fun greeting(): String
external fun callback(cb : Callback)
init {
System.loadLibrary("rsdroid")
}

companion object {
val instance: RustCore = RustCore()
}


interface Callback {
fun onSuccess()
}

}
2 changes: 2 additions & 0 deletions rslib-bridge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cargo.lock
target/*
18 changes: 18 additions & 0 deletions rslib-bridge/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# cargo-features = ["strip"]

[package]
name = "rsdroid"
version = "0.1.0"
authors = ["David Allison <[email protected]>"]
edition = "2018"

[lib]
crate_type = ["dylib"]

[dependencies]
jni = { version = "0.17.0", default-features = false }
# picked bundled - TODO: Is this correct?
rusqlite = { version = "0.23.1", features = ["trace", "functions", "collation", "bundled"] }

[features]
no-android = []
1 change: 1 addition & 0 deletions rslib-bridge/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
backend_proto.rs
24 changes: 24 additions & 0 deletions rslib-bridge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[macro_use]
extern crate jni;
use jni::JNIEnv;
use jni::objects::{JClass, JString, JObject};
use jni::sys::{jbyteArray, jint, jlong, jobjectArray, jarray, jstring};
use std::ffi::CString;

#[no_mangle]
pub unsafe extern fn Java_com_linkedin_android_rsdroid_RustCore_greeting(env: JNIEnv, _: JClass) -> jstring {
let world_ptr = CString::new("Hello world from Rust world").unwrap();
let output = env.new_string(world_ptr.to_str().unwrap()).expect("Couldn't create java string!");
output.into_inner()
}

#[no_mangle]
pub unsafe extern fn Java_com_linkedin_android_rsdroid_RustCore_callback(
env: JNIEnv,
_class: JClass,
callback: JObject,
) {
env.call_method(callback, "onSuccess", "()V", &[])
.unwrap();
}

1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dependencyResolutionManagement {
}
}
rootProject.name = "KotinRustProto"
include ':rsdroid'
include ':app'

0 comments on commit 84e9ba0

Please sign in to comment.