Merge "cherry pick: Create rust codelab via a Rust lib used by a system app" into main am: 4bed088780
Original change: https://android-review.googlesource.com/c/platform/development/+/2711374 Change-Id: Ib2e83a9a8f61f413a5d016477f32e6ae2f60afed Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -26,7 +26,7 @@ android_app {
|
||||
obfuscate: false,
|
||||
shrink_resources: true,
|
||||
},
|
||||
|
||||
required: ["libexample_rust_jni"],
|
||||
plugins: ["dagger2-compiler"]
|
||||
}
|
||||
|
||||
@@ -87,3 +87,19 @@ cc_library {
|
||||
],
|
||||
export_include_dirs: ["src/include"],
|
||||
}
|
||||
|
||||
rust_aconfig_library {
|
||||
name: "libaconfig_demo_flags_rust",
|
||||
crate_name: "aconfig_demo_flags_rust",
|
||||
aconfig_declarations: "aconfig_demo_flags",
|
||||
}
|
||||
|
||||
rust_ffi_shared {
|
||||
name: "libexample_rust_jni",
|
||||
crate_name: "example_rust_jni",
|
||||
srcs: ["src/lib.rs"],
|
||||
rustlibs: [
|
||||
"libjni",
|
||||
"libaconfig_demo_flags_rust",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -28,5 +28,6 @@
|
||||
android:textSize="25sp"
|
||||
android:autoText="true"
|
||||
android:capitalize="sentences"
|
||||
android:scrollbars="vertical"
|
||||
android:textStyle="bold|italic"/>
|
||||
</LinearLayout>
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.TextView;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
|
||||
import com.example.android.aconfig.demo.flags.Flags;
|
||||
|
||||
@@ -43,6 +44,8 @@ public class AconfigDemoActivity extends Activity {
|
||||
|
||||
setContentView(R.layout.main);
|
||||
TextView simpleTextView = (TextView) findViewById(R.id.simpleTextView);
|
||||
simpleTextView.setMovementMethod(new ScrollingMovementMethod());
|
||||
|
||||
simpleTextView.setText("Show Java Flags: \n\n");
|
||||
|
||||
StaticContent cp = new StaticContent();
|
||||
@@ -60,11 +63,16 @@ public class AconfigDemoActivity extends Activity {
|
||||
if (Flags.awesomeFlag2()) {
|
||||
Log.v("AconfigDemoActivity", Flags.FLAG_AWESOME_FLAG_2 + " is on!");
|
||||
}
|
||||
|
||||
simpleTextView.append("\n\nShow Rust Flags: \n\n");
|
||||
simpleTextView.append(printRustFlag());
|
||||
}
|
||||
|
||||
public native String printCFlag();
|
||||
public native String printRustFlag();
|
||||
|
||||
static {
|
||||
System.loadLibrary("example_cpp_lib");
|
||||
System.loadLibrary("example_rust_jni");
|
||||
}
|
||||
}
|
||||
|
||||
46
samples/AconfigDemo/src/lib.rs
Normal file
46
samples/AconfigDemo/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
//! example rust crate to be used in AconfigDemoActivity app
|
||||
|
||||
use jni::objects::JClass;
|
||||
use jni::sys::jstring;
|
||||
use jni::JNIEnv;
|
||||
|
||||
/// get flag value via static interface
|
||||
pub fn get_flag_via_static_interface() -> String {
|
||||
format!(
|
||||
"flag value: {}",
|
||||
if aconfig_demo_flags_rust::append_static_content() { "true" } else { "false" }
|
||||
)
|
||||
}
|
||||
|
||||
/// get flag value via injection
|
||||
pub fn get_flag_via_injection_interface(
|
||||
provider: &aconfig_demo_flags_rust::FlagProvider,
|
||||
) -> String {
|
||||
format!("flag value: {}", if provider.append_injected_content() { "true" } else { "false" })
|
||||
}
|
||||
|
||||
/// printRustFlag function
|
||||
#[no_mangle]
|
||||
#[allow(unused)]
|
||||
pub extern "system" fn Java_com_example_android_aconfig_demo_AconfigDemoActivity_printRustFlag<
|
||||
'local,
|
||||
>(
|
||||
mut env: JNIEnv<'local>,
|
||||
class: JClass<'local>,
|
||||
) -> jstring {
|
||||
let mut result = String::new();
|
||||
|
||||
result.push_str("flag name : append_static_content\n");
|
||||
result.push_str("use pattern : static method\n");
|
||||
result.push_str(&get_flag_via_static_interface());
|
||||
|
||||
result.push_str("\n\n");
|
||||
|
||||
result.push_str("flag name : append_injected_content\n");
|
||||
result.push_str("use pattern : injection\n");
|
||||
result.push_str(&get_flag_via_injection_interface(&aconfig_demo_flags_rust::PROVIDER));
|
||||
|
||||
let output = env.new_string(result).expect("Couldn't create java string!");
|
||||
|
||||
output.into_raw()
|
||||
}
|
||||
Reference in New Issue
Block a user