Why does Android Studio display "only be called from with the same library group" when I remove @SuppressLint("RestrictedApi")?

Viewed 1903

The Code A is from CameraX project, you can see source code.

Android Studio will display "only be called from with the same library group" when I remove @SuppressLint("RestrictedApi"), you can see Image 1.

Why can't I remove @SuppressLint("RestrictedApi") in Code A ? What deos a restriction API mean?

Code A

@SuppressLint("RestrictedApi")
    private fun updateCameraUi() {
        ...

        // Listener for button used to switch cameras
        controls.findViewById<ImageButton>(R.id.camera_switch_button).setOnClickListener {
            lensFacing = if (CameraX.LensFacing.FRONT == lensFacing) {
                CameraX.LensFacing.BACK
            } else {
                CameraX.LensFacing.FRONT
            }
            try {
                // Only bind use cases if we can query a camera with this orientation
                CameraX.getCameraWithLensFacing(lensFacing)

                // Unbind all use cases and bind them again with the new lens facing configuration
                CameraX.unbindAll()
                bindCameraUseCases()
            } catch (exc: Exception) {
                // Do nothing
            }
        }
    }

Image 1

enter image description here

2 Answers

There have been breaking changes in the library since the tutorial was made.
Reverting the package version to 1.0.0-alpha06, same as the tutorial, solves the problem.

These are issues with the library that don't affect your code.

In several code examples using these APIs there is often a @SuppressLint("RestrictedApi") in the file hiding the warning.

The projects should still compile and run as they should, although you must make sure that you are using the correct dependency version. The APIs are changing quite frequently still, and if you're referencing an example it might be using an older version which has since changed.

Your best bet is to look directly at the source code and if the method you are calling is declared as public then you probably won't have a problem.

Related