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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -165,6 +166,7 @@ public Intent asBindIntent() {
*
* <p>See {@link Intent#URI_ANDROID_APP_SCHEME} for details.
*/
@SuppressLint("InlinedApi")
Copy link
Contributor

@kannanjgithub kannanjgithub Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning inlinedApi is about using the constant URI_ANDROID_APP_SCHEME that was introduced in API level 22. Instead do intentForUri.toUri(android.os.Build.VERSION.SDK_INT < 22? URL_INTENT_SCHEME : URI_ANDROID_APP_SCHEME);

and remove the suppression. If the linter still complains, then add @TargetApi(22) to indicate that we are only using URI_ANDROID_APP_SCHEME at and above API level 22.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kannanjgithub yes fixed with conditional version check only - lint warnings resolved without @TargetApi working.

public String asAndroidAppUri() {
Intent intentForUri = bindIntent;
if (intentForUri.getPackage() == null) {
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
23 changes: 14 additions & 9 deletions binder/src/main/java/io/grpc/binder/internal/ServiceBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.UserHandle;
import androidx.annotation.AnyThread;
Expand Down Expand Up @@ -183,18 +184,22 @@ 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) {
Copy link
Contributor

@kannanjgithub kannanjgithub Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is going to fail the connection with Unimplemented for Android API levels 21 - 29. It would already be crashing for these API levels? @ejona86 . Also for similar changes below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdcormie probably knows what's going on better. But I'd expect we need proper error handling here, and not claiming things are UNIMPLEMENTED. INTERNAL with a message that the API is not available might be fair.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding bindServiceAsUser(), I agree that we should not fail with UNIMPLEMENTED in this case because that already means something else that applications do want to handle very specifically.

If we reach this line it's because targetUserHandle != null which means the application is asking for a cross-user Channel. The public entry point to this feature already documents an SDK >= 30 requirement which is why the old behavior of crashing with an NoSuchMethodError was actually totally fine IMHO. IF we just do checkState(SDK_INT >= 30, "Cross user Channel requires Android R+") would that make the linter happy and yield the desired INTERNAL error?

bindResult = context.bindServiceAsUser(bindIntent, conn, flags, targetUserHandle);
}
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);
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
bindResult =
devicePolicyManager.bindDeviceAdminServiceAsUser(
channelCredentials.getDevicePolicyAdminComponentName(),
bindIntent,
conn,
flags,
targetUserHandle);
}
break;
}
if (!bindResult) {
Expand Down
4 changes: 4 additions & 0 deletions lint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="OldTargetApi" severity="ignore" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it cause any problem if you set targetSdkVersion to the latest available even while the AGP is not upgraded yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it causes AAPT2 linking failures due to resource format changes. AGP 7.x is incompatible with SDK 35/36, so AGP must be upgraded first. keeping the lint.xml suppression after upgrading AGP/SDK won’t break anything, and have to remove it after the AGP/SDK upgrade to keep our configuration clean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include that relevant information here as a comment, so it is clear when it can be removed.

</lint>
Loading