Skip to content

Android: Clean up lint warnings and other warnings to get build success #12143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
}
compileSdkVersion 34
defaultConfig {
minSdkVersion 21
minSdkVersion 22
targetSdkVersion 33
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ private void configureNetworkMonitoring() {
connectivityManager.registerDefaultNetworkCallback(defaultNetworkCallback);
unregisterRunnable =
new Runnable() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void run() {
connectivityManager.unregisterNetworkCallback(defaultNetworkCallback);
Expand All @@ -231,7 +230,6 @@ public void run() {
context.registerReceiver(networkReceiver, networkIntentFilter);
unregisterRunnable =
new Runnable() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void run() {
context.unregisterReceiver(networkReceiver);
Expand Down
2 changes: 1 addition & 1 deletion binder/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
targetCompatibility 1.8
}
defaultConfig {
minSdkVersion 21
minSdkVersion 22
targetSdkVersion 33
versionCode 1
versionName "1.0"
Expand Down
2 changes: 0 additions & 2 deletions binder/src/main/java/io/grpc/binder/SecurityPolicies.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public Status checkAuthorization(int uid) {
* Creates {@link SecurityPolicy} which checks if the app is a device owner app. See {@link
* DevicePolicyManager}.
*/
@RequiresApi(18)
public static io.grpc.binder.SecurityPolicy isDeviceOwner(Context applicationContext) {
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) applicationContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
Expand All @@ -199,7 +198,6 @@ public static io.grpc.binder.SecurityPolicy isDeviceOwner(Context applicationCon
* Creates {@link SecurityPolicy} which checks if the app is a profile owner app. See {@link
* DevicePolicyManager}.
*/
@RequiresApi(21)
public static SecurityPolicy isProfileOwner(Context applicationContext) {
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) applicationContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
Expand Down
25 changes: 17 additions & 8 deletions binder/src/main/java/io/grpc/binder/internal/ServiceBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,27 @@ private static Status bindInternal(
bindResult = context.bindService(bindIntent, conn, flags);
break;
case BIND_SERVICE_AS_USER:
bindResult = context.bindServiceAsUser(bindIntent, conn, flags, targetUserHandle);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
bindResult = context.bindServiceAsUser(bindIntent, conn, flags, targetUserHandle);
} else {
return Status.INTERNAL.withDescription("Cross user Channel requires Android R+");
}
break;
case DEVICE_POLICY_BIND_SEVICE_ADMIN:
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
bindResult =
devicePolicyManager.bindDeviceAdminServiceAsUser(
channelCredentials.getDevicePolicyAdminComponentName(),
bindIntent,
conn,
flags,
targetUserHandle);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
bindResult =
devicePolicyManager.bindDeviceAdminServiceAsUser(
channelCredentials.getDevicePolicyAdminComponentName(),
bindIntent,
conn,
flags,
targetUserHandle);
} else {
return Status.INTERNAL.withDescription(
"Device policy admin binding requires Android R+");
}
break;
}
if (!bindResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.IBinder;
import android.os.Parcel;
import android.os.UserHandle;
Expand Down Expand Up @@ -327,6 +328,50 @@ public void testResolveNonExistentServiceWithTargetUserThrows() throws Exception
assertThat(statusException.getStatus().getDescription()).contains("12345");
}

@Test
@Config(sdk = 30)
public void testBindService_doesNotThrowInternalErrorWhenSdkAtLeastR() {
UserHandle userHandle = generateUserHandle(/* userId= */ 12345);
binding = newBuilder().setTargetUserHandle(userHandle).build();
binding.bind();
shadowOf(getMainLooper()).idle();

assertThat(Build.VERSION.SDK_INT).isEqualTo(Build.VERSION_CODES.R);
assertThat(observer.unboundReason).isNull();
}

@Test
@Config(sdk = 28)
public void testBindServiceAsUser_returnsErrorWhenSdkBelowR() {
UserHandle userHandle = generateUserHandle(/* userId= */ 12345);
binding = newBuilder().setTargetUserHandle(userHandle).build();
binding.bind();
shadowOf(getMainLooper()).idle();

assertThat(observer.unboundReason.getCode()).isEqualTo(Code.INTERNAL);
assertThat(observer.unboundReason.getDescription())
.isEqualTo("Cross user Channel requires Android R+");
}

@Test
@Config(sdk = 28)
public void testDevicePolicyBlind_returnsErrorWhenSdkBelowR() {
String deviceAdminClassName = "DevicePolicyAdmin";
ComponentName adminComponent = new ComponentName(appContext, deviceAdminClassName);
allowBindDeviceAdminForUser(appContext, adminComponent, 10);
binding =
newBuilder()
.setTargetUserHandle(UserHandle.getUserHandleForUid(10))
.setChannelCredentials(BinderChannelCredentials.forDevicePolicyAdmin(adminComponent))
.build();
binding.bind();
shadowOf(getMainLooper()).idle();

assertThat(observer.unboundReason.getCode()).isEqualTo(Code.INTERNAL);
assertThat(observer.unboundReason.getDescription())
.isEqualTo("Device policy admin binding requires Android R+");
}

@Test
@Config(sdk = 30)
public void testBindWithDeviceAdmin() throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion cronet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
namespace = 'io.grpc.cronet'
compileSdkVersion 33
defaultConfig {
minSdkVersion 21
minSdkVersion 22
targetSdkVersion 33
versionCode 1
versionName "1.0"
Expand Down
8 changes: 8 additions & 0 deletions lint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!--
Suppress temporarily due to AAPT2 failures with SDK 35/36 on AGP 7.x.
Remove after AGP upgrade.
-->
<issue id="OldTargetApi" severity="ignore" />
</lint>