Skip to content

Commit 5ba663b

Browse files
authored
android: add grpc-android into main build (#6793)
Add grpc-android into main build. grpc-android will be built if Gradle option skipAndroid is false. This change also migrates deprecated Robolectric methods to androidx.test methods.
1 parent a57f1a9 commit 5ba663b

File tree

6 files changed

+67
-135
lines changed

6 files changed

+67
-135
lines changed

android/build.gradle

Lines changed: 12 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
apply plugin: 'com.android.library'
1+
plugins {
2+
id "maven-publish"
23

3-
group = "io.grpc"
4-
version = "1.29.0-SNAPSHOT" // CURRENT_GRPC_VERSION
5-
description = 'gRPC: Android'
6-
7-
buildscript {
8-
repositories {
9-
google()
10-
jcenter()
11-
mavenCentral()
12-
maven { url "https://plugins.gradle.org/m2/" }
13-
}
14-
dependencies {
15-
classpath 'com.android.tools.build:gradle:3.3.0'
16-
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.8.1"
17-
classpath "digital.wup:android-maven-publish:3.6.2"
18-
}
4+
id "com.android.library"
5+
id "digital.wup.android-maven-publish"
196
}
207

21-
apply plugin: "maven-publish"
22-
apply plugin: "net.ltgt.errorprone"
23-
apply plugin: "digital.wup.android-maven-publish"
24-
apply plugin: "signing"
8+
description = 'gRPC: Android'
259

2610
android {
2711
compileSdkVersion 28
@@ -39,20 +23,16 @@ android {
3923
repositories {
4024
google()
4125
jcenter()
42-
mavenCentral()
43-
mavenLocal()
4426
}
4527

4628
dependencies {
47-
errorprone 'com.google.errorprone:error_prone_core:2.3.4'
48-
errorproneJavac 'com.google.errorprone:javac:9+181-r4173-1'
29+
implementation project(':grpc-core')
4930

50-
implementation 'io.grpc:grpc-core:1.29.0-SNAPSHOT' // CURRENT_GRPC_VERSION
51-
52-
testImplementation 'io.grpc:grpc-okhttp:1.29.0-SNAPSHOT' // CURRENT_GRPC_VERSION
53-
testImplementation 'junit:junit:4.12'
54-
testImplementation 'org.robolectric:robolectric:4.3.1'
55-
testImplementation 'com.google.truth:truth:1.0.1'
31+
testImplementation project('::grpc-okhttp')
32+
testImplementation libraries.androidx_test
33+
testImplementation libraries.junit
34+
testImplementation libraries.robolectric
35+
testImplementation libraries.truth
5636
}
5737

5838
task javadocs(type: Javadoc) {
@@ -83,81 +63,11 @@ task sourcesJar(type: Jar) {
8363

8464
publishing {
8565
publications {
86-
maven(MavenPublication) {
66+
maven {
8767
from components.android
8868

8969
artifact javadocJar
9070
artifact sourcesJar
91-
92-
pom {
93-
name = project.group + ":" + project.name
94-
url = 'https://github.com/grpc/grpc-java'
95-
afterEvaluate {
96-
// description is not available until evaluated.
97-
description = project.description
98-
}
99-
100-
scm {
101-
connection = 'scm:git:https://github.com/grpc/grpc-java.git'
102-
developerConnection = 'scm:git:[email protected]:grpc/grpc-java.git'
103-
url = 'https://github.com/grpc/grpc-java'
104-
}
105-
106-
licenses {
107-
license {
108-
name = 'Apache 2.0'
109-
url = 'https://opensource.org/licenses/Apache-2.0'
110-
}
111-
}
112-
113-
developers {
114-
developer {
115-
id = "grpc.io"
116-
name = "gRPC Contributors"
117-
118-
url = "https://grpc.io/"
119-
organization = "gRPC Authors"
120-
organizationUrl = "https://www.google.com"
121-
}
122-
}
123-
124-
withXml {
125-
asNode().dependencies.'*'.findAll() { dep ->
126-
dep.artifactId.text() in ['grpc-api', 'grpc-core']
127-
}.each() { core ->
128-
core.version*.value = "[" + core.version.text() + "]"
129-
}
130-
}
131-
}
132-
}
133-
}
134-
repositories {
135-
maven {
136-
if (rootProject.hasProperty('repositoryDir')) {
137-
url = new File(rootProject.repositoryDir).toURI()
138-
} else {
139-
String stagingUrl
140-
if (rootProject.hasProperty('repositoryId')) {
141-
stagingUrl = 'https://oss.sonatype.org/service/local/staging/deployByRepositoryId/' +
142-
rootProject.repositoryId
143-
} else {
144-
stagingUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
145-
}
146-
credentials {
147-
if (rootProject.hasProperty('ossrhUsername') && rootProject.hasProperty('ossrhPassword')) {
148-
username = rootProject.ossrhUsername
149-
password = rootProject.ossrhPassword
150-
}
151-
}
152-
def releaseUrl = stagingUrl
153-
def snapshotUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
154-
url = version.endsWith('SNAPSHOT') ? snapshotUrl : releaseUrl
155-
}
15671
}
15772
}
15873
}
159-
160-
signing {
161-
required false
162-
sign publishing.publications.maven
163-
}

android/settings.gradle

Lines changed: 0 additions & 1 deletion
This file was deleted.

android/src/test/java/io/grpc/android/AndroidChannelBuilderTest.java

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.net.ConnectivityManager;
2929
import android.net.Network;
3030
import android.net.NetworkInfo;
31+
import androidx.test.core.app.ApplicationProvider;
3132
import io.grpc.CallOptions;
3233
import io.grpc.ClientCall;
3334
import io.grpc.ManagedChannel;
@@ -39,7 +40,6 @@
3940
import org.junit.Test;
4041
import org.junit.runner.RunWith;
4142
import org.robolectric.RobolectricTestRunner;
42-
import org.robolectric.RuntimeEnvironment;
4343
import org.robolectric.annotation.Config;
4444
import org.robolectric.annotation.Implementation;
4545
import org.robolectric.annotation.Implements;
@@ -54,32 +54,42 @@
5454
public final class AndroidChannelBuilderTest {
5555
private final NetworkInfo WIFI_CONNECTED =
5656
ShadowNetworkInfo.newInstance(
57-
NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true);
57+
NetworkInfo.DetailedState.CONNECTED,
58+
ConnectivityManager.TYPE_WIFI,
59+
0,
60+
true,
61+
NetworkInfo.State.CONNECTED);
5862
private final NetworkInfo WIFI_DISCONNECTED =
5963
ShadowNetworkInfo.newInstance(
60-
NetworkInfo.DetailedState.DISCONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, false);
64+
NetworkInfo.DetailedState.DISCONNECTED,
65+
ConnectivityManager.TYPE_WIFI,
66+
0,
67+
true,
68+
NetworkInfo.State.DISCONNECTED);
6169
private final NetworkInfo MOBILE_CONNECTED =
6270
ShadowNetworkInfo.newInstance(
6371
NetworkInfo.DetailedState.CONNECTED,
6472
ConnectivityManager.TYPE_MOBILE,
6573
ConnectivityManager.TYPE_MOBILE_MMS,
6674
true,
67-
true);
75+
NetworkInfo.State.CONNECTED);
6876
private final NetworkInfo MOBILE_DISCONNECTED =
6977
ShadowNetworkInfo.newInstance(
7078
NetworkInfo.DetailedState.DISCONNECTED,
7179
ConnectivityManager.TYPE_MOBILE,
7280
ConnectivityManager.TYPE_MOBILE_MMS,
7381
true,
74-
false);
82+
NetworkInfo.State.DISCONNECTED);
7583

7684
private ConnectivityManager connectivityManager;
7785

7886
@Before
7987
public void setUp() {
8088
connectivityManager =
8189
(ConnectivityManager)
82-
RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
90+
ApplicationProvider
91+
.getApplicationContext()
92+
.getSystemService(Context.CONNECTIVITY_SERVICE);
8393
}
8494

8595
@Test
@@ -103,8 +113,9 @@ public void nullContextDoesNotThrow_api23() {
103113

104114
// Network change and shutdown should be no-op for the channel without an Android Context
105115
shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_CONNECTED);
106-
RuntimeEnvironment.application.sendBroadcast(
107-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
116+
ApplicationProvider
117+
.getApplicationContext()
118+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
108119
androidChannel.shutdown();
109120

110121
assertThat(delegateChannel.resetCount).isEqualTo(0);
@@ -131,48 +142,55 @@ public void resetConnectBackoff_api23() {
131142
TestChannel delegateChannel = new TestChannel();
132143
ManagedChannel androidChannel =
133144
new AndroidChannelBuilder.AndroidChannel(
134-
delegateChannel, RuntimeEnvironment.application.getApplicationContext());
145+
delegateChannel, ApplicationProvider.getApplicationContext());
135146
assertThat(delegateChannel.resetCount).isEqualTo(0);
136147

137148
// On API levels < 24, the broadcast receiver will invoke resetConnectBackoff() on the first
138149
// connectivity action broadcast regardless of previous connection status
139150
shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_CONNECTED);
140-
RuntimeEnvironment.application.sendBroadcast(
141-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
151+
ApplicationProvider
152+
.getApplicationContext()
153+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
142154
assertThat(delegateChannel.resetCount).isEqualTo(1);
143155

144156
// The broadcast receiver may fire when the active network status has not actually changed
145-
RuntimeEnvironment.application.sendBroadcast(
146-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
157+
ApplicationProvider
158+
.getApplicationContext()
159+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
147160
assertThat(delegateChannel.resetCount).isEqualTo(1);
148161

149162
// Drop the connection
150163
shadowOf(connectivityManager).setActiveNetworkInfo(null);
151-
RuntimeEnvironment.application.sendBroadcast(
152-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
164+
ApplicationProvider
165+
.getApplicationContext()
166+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
153167
assertThat(delegateChannel.resetCount).isEqualTo(1);
154168

155169
// Notify that a new but not connected network is available
156170
shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_DISCONNECTED);
157-
RuntimeEnvironment.application.sendBroadcast(
158-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
171+
ApplicationProvider
172+
.getApplicationContext()
173+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
159174
assertThat(delegateChannel.resetCount).isEqualTo(1);
160175

161176
// Establish a connection
162177
shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_CONNECTED);
163-
RuntimeEnvironment.application.sendBroadcast(
164-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
178+
ApplicationProvider
179+
.getApplicationContext()
180+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
165181
assertThat(delegateChannel.resetCount).isEqualTo(2);
166182

167183
// Disconnect, then shutdown the channel and verify that the broadcast receiver has been
168184
// unregistered
169185
shadowOf(connectivityManager).setActiveNetworkInfo(null);
170-
RuntimeEnvironment.application.sendBroadcast(
171-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
186+
ApplicationProvider
187+
.getApplicationContext()
188+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
172189
androidChannel.shutdown();
173190
shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_CONNECTED);
174-
RuntimeEnvironment.application.sendBroadcast(
175-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
191+
ApplicationProvider
192+
.getApplicationContext()
193+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
176194

177195
assertThat(delegateChannel.resetCount).isEqualTo(2);
178196
// enterIdle is not called on API levels < 24
@@ -186,7 +204,7 @@ public void resetConnectBackoffAndEnterIdle_api24() {
186204
TestChannel delegateChannel = new TestChannel();
187205
ManagedChannel androidChannel =
188206
new AndroidChannelBuilder.AndroidChannel(
189-
delegateChannel, RuntimeEnvironment.application.getApplicationContext());
207+
delegateChannel, ApplicationProvider.getApplicationContext());
190208
assertThat(delegateChannel.resetCount).isEqualTo(0);
191209
assertThat(delegateChannel.enterIdleCount).isEqualTo(0);
192210

@@ -227,7 +245,7 @@ public void newChannelWithConnection_entersIdleOnSecondConnectionChange_api24()
227245
TestChannel delegateChannel = new TestChannel();
228246
ManagedChannel androidChannel =
229247
new AndroidChannelBuilder.AndroidChannel(
230-
delegateChannel, RuntimeEnvironment.application.getApplicationContext());
248+
delegateChannel, ApplicationProvider.getApplicationContext());
231249

232250
// The first onAvailable() may just signal that the device was connected when the callback is
233251
// registered, rather than indicating a changed network, so we do not enter idle.
@@ -248,15 +266,17 @@ public void shutdownNowUnregistersBroadcastReceiver_api23() {
248266
TestChannel delegateChannel = new TestChannel();
249267
ManagedChannel androidChannel =
250268
new AndroidChannelBuilder.AndroidChannel(
251-
delegateChannel, RuntimeEnvironment.application.getApplicationContext());
269+
delegateChannel, ApplicationProvider.getApplicationContext());
252270

253271
shadowOf(connectivityManager).setActiveNetworkInfo(null);
254-
RuntimeEnvironment.application.sendBroadcast(
255-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
272+
ApplicationProvider
273+
.getApplicationContext()
274+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
256275
androidChannel.shutdownNow();
257276
shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_CONNECTED);
258-
RuntimeEnvironment.application.sendBroadcast(
259-
new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
277+
ApplicationProvider
278+
.getApplicationContext()
279+
.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
260280

261281
assertThat(delegateChannel.resetCount).isEqualTo(0);
262282
}
@@ -268,7 +288,7 @@ public void shutdownNowUnregistersNetworkCallback_api24() {
268288
TestChannel delegateChannel = new TestChannel();
269289
ManagedChannel androidChannel =
270290
new AndroidChannelBuilder.AndroidChannel(
271-
delegateChannel, RuntimeEnvironment.application.getApplicationContext());
291+
delegateChannel, ApplicationProvider.getApplicationContext());
272292

273293
androidChannel.shutdownNow();
274294
shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_CONNECTED);
@@ -318,6 +338,7 @@ private void notifyDefaultNetworkCallbacksOnLost(Network network) {
318338
}
319339

320340
@Implementation(minSdk = N)
341+
@Override
321342
protected void registerDefaultNetworkCallback(
322343
ConnectivityManager.NetworkCallback networkCallback) {
323344
defaultNetworkCallbacks.add(networkCallback);

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ subprojects {
157157
mockito: 'org.mockito:mockito-core:2.28.2',
158158
truth: 'com.google.truth:truth:1.0.1',
159159
guava_testlib: "com.google.guava:guava-testlib:${guavaVersion}",
160+
androidx_test: "androidx.test:core:1.2.0",
160161
robolectric: "org.robolectric:robolectric:4.3.1",
161162

162163
// Benchmark dependencies

buildscripts/kokoro/linux_artifacts.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ echo y | ${ANDROID_HOME}/tools/bin/sdkmanager "build-tools;28.0.3"
1616
LOCAL_MVN_TEMP=$(mktemp -d)
1717
pushd "$GRPC_JAVA_DIR/android"
1818
../gradlew publish \
19-
--include-build "$GRPC_JAVA_DIR" \
2019
-Dorg.gradle.parallel=false \
2120
-PskipCodegen=true \
2221
-PrepositoryDir="$LOCAL_MVN_TEMP"

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,6 @@ if (settings.hasProperty('skipAndroid') && skipAndroid.toBoolean()) {
8686
println '*** Android SDK is required. To avoid building Android projects, set -PskipAndroid=true'
8787
include ":grpc-cronet"
8888
project(':grpc-cronet').projectDir = "$rootDir/cronet" as File
89+
include ":grpc-android"
90+
project(':grpc-android').projectDir = "$rootDir/android" as File
8991
}

0 commit comments

Comments
 (0)