mirror of https://github.com/grpc/grpc.git
Add BinderTransport example app (#26747)
This commit adds an app that will be served as BinderTransport example later. Currently the app simply calls C++ function when button is pressed. In the future the C++ function will run BinderTransport gRPC example instead. Tests not included in this commit, later we will add a build test to CI to make sure this apk is always build-able. (This app will also be used to make sure our BinderTransport implementation is compile-able with Android toolchain, on GitHub) For now we exclude this target in bazel build test. Changes are made to WORKSPACE file in order to support android builds. Build instructions are documented at examples/android/binder/java/io/grpc/binder/cpp/example/README.mdpull/26793/head
parent
5011372677
commit
746eaf6c48
10 changed files with 213 additions and 1 deletions
@ -0,0 +1,21 @@ |
|||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
|
package="io.grpc.binder.cpp.example" |
||||||
|
android:versionCode="1" |
||||||
|
android:versionName="1.0" > |
||||||
|
|
||||||
|
<uses-sdk |
||||||
|
android:minSdkVersion="29" |
||||||
|
android:targetSdkVersion="30" /> |
||||||
|
|
||||||
|
<application |
||||||
|
android:label="gRPC BinderTransport Cpp"> |
||||||
|
<activity |
||||||
|
android:name=".MainActivity" |
||||||
|
android:label="gRPC BinderTransport Cpp" > |
||||||
|
<intent-filter> |
||||||
|
<action android:name="android.intent.action.MAIN" /> |
||||||
|
<category android:name="android.intent.category.LAUNCHER" /> |
||||||
|
</intent-filter> |
||||||
|
</activity> |
||||||
|
</application> |
||||||
|
</manifest> |
@ -0,0 +1,48 @@ |
|||||||
|
# Copyright 2021 gRPC authors. |
||||||
|
# |
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
# you may not use this file except in compliance with the License. |
||||||
|
# You may obtain a copy of the License at |
||||||
|
# |
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0 |
||||||
|
# |
||||||
|
# Unless required by applicable law or agreed to in writing, software |
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
# See the License for the specific language governing permissions and |
||||||
|
# limitations under the License. |
||||||
|
|
||||||
|
load("@build_bazel_rules_android//android:rules.bzl", "android_binary", "android_library") |
||||||
|
|
||||||
|
cc_library( |
||||||
|
name = "jni_lib", |
||||||
|
srcs = ["native.cc"], |
||||||
|
linkopts = [ |
||||||
|
"-ldl", |
||||||
|
"-llog", |
||||||
|
"-Wl,--no-undefined", |
||||||
|
], |
||||||
|
deps = [], |
||||||
|
alwayslink = True, |
||||||
|
) |
||||||
|
|
||||||
|
android_library( |
||||||
|
name = "activity", |
||||||
|
srcs = [ |
||||||
|
"ButtonPressHandler.java", |
||||||
|
"MainActivity.java", |
||||||
|
], |
||||||
|
manifest = "AndroidManifest.xml", |
||||||
|
resource_files = glob(["res/**"]), |
||||||
|
deps = [ |
||||||
|
":jni_lib", |
||||||
|
], |
||||||
|
) |
||||||
|
|
||||||
|
android_binary( |
||||||
|
name = "app", |
||||||
|
manifest = "AndroidManifest.xml", |
||||||
|
deps = [ |
||||||
|
":activity", |
||||||
|
], |
||||||
|
) |
@ -0,0 +1,15 @@ |
|||||||
|
package io.grpc.binder.cpp.example; |
||||||
|
|
||||||
|
import android.app.Application; |
||||||
|
|
||||||
|
public class ButtonPressHandler { |
||||||
|
static { |
||||||
|
System.loadLibrary("app"); |
||||||
|
} |
||||||
|
|
||||||
|
public native String native_entry(Application application); |
||||||
|
|
||||||
|
public String onPressed(Application application) { |
||||||
|
return native_entry(application); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
package io.grpc.binder.cpp.example; |
||||||
|
|
||||||
|
import android.app.Activity; |
||||||
|
import android.os.Bundle; |
||||||
|
import android.util.Log; |
||||||
|
import android.widget.Button; |
||||||
|
import android.widget.TextView; |
||||||
|
|
||||||
|
/** Main class for the example app. */ |
||||||
|
public class MainActivity extends Activity { |
||||||
|
@Override |
||||||
|
public void onCreate(Bundle savedInstanceState) { |
||||||
|
super.onCreate(savedInstanceState); |
||||||
|
Log.v("Example", "hello, world"); |
||||||
|
|
||||||
|
setContentView(R.layout.activity_main); |
||||||
|
|
||||||
|
Button clickMeButton = findViewById(R.id.clickMeButton); |
||||||
|
TextView exampleTextView = findViewById(R.id.exampleTextView); |
||||||
|
|
||||||
|
ButtonPressHandler h = new ButtonPressHandler(); |
||||||
|
|
||||||
|
clickMeButton.setOnClickListener( |
||||||
|
v -> exampleTextView.setText(h.onPressed(getApplication()))); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
# gRPC-core BinderTransport example app |
||||||
|
|
||||||
|
WIP. |
||||||
|
|
||||||
|
## Build Instruction |
||||||
|
|
||||||
|
1. Install Android SDK and NDK. Currently we only support SDK version 30.0.3 and |
||||||
|
NDK version 21.4.7075529 . Make sure you get these exact versions otherwise |
||||||
|
Bazel might complain. |
||||||
|
|
||||||
|
2. Point environment variables to install locations of SDK and NDK |
||||||
|
``` |
||||||
|
export ANDROID_HOME=$HOME/Android/Sdk/ |
||||||
|
export ANDROID_NDK_HOME=$HOME/Android/Sdk/ndk/21.4.7075529 |
||||||
|
``` |
||||||
|
3. `bazel build //examples/android/binder/java/io/grpc/binder/cpp/example:app` |
||||||
|
4. `adb install |
||||||
|
bazel-bin/examples/android/binder/java/io/grpc/binder/cpp/example/app.apk` |
@ -0,0 +1,30 @@ |
|||||||
|
// Copyright 2021 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
#include <android/log.h> |
||||||
|
#include <jni.h> |
||||||
|
|
||||||
|
extern "C" JNIEXPORT jstring JNICALL |
||||||
|
Java_io_grpc_binder_cpp_example_ButtonPressHandler_native_1entry( |
||||||
|
JNIEnv* env, jobject /*this*/, jobject /*application*/) { |
||||||
|
static bool first = true; |
||||||
|
__android_log_print(ANDROID_LOG_INFO, "Demo", "Line number %d", __LINE__); |
||||||
|
if (first) { |
||||||
|
first = false; |
||||||
|
return env->NewStringUTF("Clicked 1 time"); |
||||||
|
} else { |
||||||
|
return env->NewStringUTF("Clicked more than 1 time"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,24 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
|
android:padding="16dp" |
||||||
|
android:orientation="vertical" |
||||||
|
android:gravity="center_vertical" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="match_parent"> |
||||||
|
|
||||||
|
<TextView |
||||||
|
android:id="@+id/exampleTextView" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_gravity="center_horizontal" |
||||||
|
android:textSize="32dp" |
||||||
|
android:text="@string/thinking_face"/> |
||||||
|
|
||||||
|
<Button |
||||||
|
android:id="@+id/clickMeButton" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_gravity="center_horizontal" |
||||||
|
android:layout_marginTop="16dp" |
||||||
|
android:text="@string/click_me_button"/> |
||||||
|
</LinearLayout> |
@ -0,0 +1,5 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<resources> |
||||||
|
<string name="click_me_button">Run example</string> |
||||||
|
<string name="thinking_face">🤔</string> |
||||||
|
</resources> |
Loading…
Reference in new issue