airbnb / Showkase

🔦 Showkase is an annotation-processor based Android library that helps you organize, discover, search and visualize Jetpack Compose UI elements
https://medium.com/airbnb-engineering/introducing-showkase-a-library-to-organize-discover-and-visualize-your-jetpack-compose-elements-d5c34ef01095
Apache License 2.0
2.11k stars 107 forks source link

Updating compileSdkVersion to 34 causes deceprecation errors for newInstance() #346

Closed ahinton-league closed 1 year ago

ahinton-league commented 1 year ago

The following code generated by showkase has a deprecation warning when we update our Android SDK version to 34

public fun Showkase.getMetadata(): ShowkaseElementsMetadata {
    try {
      val showkaseComponentProvider =
          Class.forName("REDACTED").newInstance() // deprecated!!!
          as ShowkaseProvider
      return showkaseComponentProvider.metadata()
    } catch(exception: ClassNotFoundException) {
      error("The class wasn't generated correctly. Make sure that you have setup Showkase correctly by following the steps here - https://github.com/airbnb/Showkase#Installation.")
    }
}

Added in API level 1 Deprecated in API level 34 public T newInstance () This method was deprecated in API level 34. This method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException . The call clazz.newInstance()

can be replaced by clazz.getDeclaredConstructor().newInstance()

ahinton-league commented 1 year ago

Our development policies include treating warnings as errors. This breaks all our builds and prevents us from adopting compose 1.5 and android 34.

ahinton-league commented 1 year ago

Dup of https://github.com/airbnb/Showkase/issues/335