Skip to content

Commit 20063f0

Browse files
committed
examples: Add an grpc-android dep to helloworld to demonstrate
1 parent 3da892d commit 20063f0

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

examples/MODULE.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ bazel_dep(name = "grpc-java", version = "1.82.0-SNAPSHOT", repo_name = "io_grpc_
22
bazel_dep(name = "rules_java", version = "9.3.0")
33
bazel_dep(name = "grpc-proto", version = "0.0.0-20240627-ec30f58", repo_name = "io_grpc_grpc_proto")
44
bazel_dep(name = "protobuf", version = "33.1", repo_name = "com_google_protobuf")
5+
bazel_dep(name = "rules_android", version = "0.6.0")
6+
bazel_dep(name = "rules_proto", version = "7.1.0")
57
bazel_dep(name = "rules_jvm_external", version = "6.0")
68

79
# Do not use this override in your own MODULE.bazel. It is unnecessary when
@@ -26,7 +28,11 @@ maven.install(
2628
artifacts = [
2729
"com.google.api.grpc:grpc-google-cloud-pubsub-v1:0.1.24",
2830
"com.google.api.grpc:proto-google-cloud-pubsub-v1:0.1.24",
31+
"androidx.appcompat:appcompat:1.0.0",
32+
],
33+
repositories = [
34+
"https://repo.maven.apache.org/maven2/",
35+
"https://maven.google.com",
2936
],
30-
repositories = ["https://repo.maven.apache.org/maven2/"],
3137
strict_visibility = True,
3238
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
load("@rules_android//rules:rules.bzl", "android_binary")
2+
load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")
3+
load("@rules_proto//proto:defs.bzl", "proto_library")
4+
load("@rules_java//java:defs.bzl", "java_lite_proto_library")
5+
6+
proto_library(
7+
name = "helloworld_proto",
8+
srcs = ["src/main/proto/helloworld.proto"],
9+
)
10+
11+
java_lite_proto_library(
12+
name = "helloworld_java_proto_lite",
13+
deps = [":helloworld_proto"],
14+
)
15+
16+
java_grpc_library(
17+
name = "helloworld_java_grpc_lite",
18+
srcs = [":helloworld_proto"],
19+
flavor = "lite",
20+
deps = [":helloworld_java_proto_lite"],
21+
)
22+
23+
android_binary(
24+
name = "app",
25+
srcs = glob([
26+
"src/main/java/**/*.java",
27+
]),
28+
custom_package = "io.grpc.helloworldexample",
29+
manifest = "src/main/AndroidManifest.xml",
30+
resource_files = glob(["src/main/res/**"]),
31+
visibility = ["//visibility:public"],
32+
deps = [
33+
":helloworld_java_grpc_lite",
34+
":helloworld_java_proto_lite",
35+
"@io_grpc_grpc_java//api",
36+
"@io_grpc_grpc_java//android",
37+
"@io_grpc_grpc_java//okhttp",
38+
"@io_grpc_grpc_java//protobuf-lite",
39+
"@io_grpc_grpc_java//stub",
40+
"@maven//:androidx_appcompat_appcompat",
41+
],
42+
)

examples/android/helloworld/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ dependencies {
5555
implementation 'io.grpc:grpc-okhttp:1.82.0-SNAPSHOT' // CURRENT_GRPC_VERSION
5656
implementation 'io.grpc:grpc-protobuf-lite:1.82.0-SNAPSHOT' // CURRENT_GRPC_VERSION
5757
implementation 'io.grpc:grpc-stub:1.82.0-SNAPSHOT' // CURRENT_GRPC_VERSION
58+
implementation 'io.grpc:grpc-android:1.82.0-SNAPSHOT' // CURRENT_GRPC_VERSION
5859
}

examples/android/helloworld/app/src/main/java/io/grpc/helloworldexample/HelloworldActivity.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import android.widget.EditText;
3030
import android.widget.TextView;
3131
import io.grpc.ManagedChannel;
32-
import io.grpc.ManagedChannelBuilder;
32+
import io.grpc.android.AndroidChannelBuilder;
3333
import io.grpc.examples.helloworld.GreeterGrpc;
3434
import io.grpc.examples.helloworld.HelloReply;
3535
import io.grpc.examples.helloworld.HelloRequest;
@@ -91,7 +91,12 @@ protected String doInBackground(String... params) {
9191
String portStr = params[2];
9292
int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
9393
try {
94-
channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
94+
Activity activity = activityReference.get();
95+
AndroidChannelBuilder channelBuilder = AndroidChannelBuilder.forAddress(host, port);
96+
if (activity != null) {
97+
channelBuilder.context(activity.getApplicationContext());
98+
}
99+
channel = channelBuilder.usePlaintext().build();
95100
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
96101
HelloRequest request = HelloRequest.newBuilder().setName(message).build();
97102
HelloReply reply = stub.sayHello(request);

0 commit comments

Comments
 (0)