Search code examples
androidandroid-notificationsandroid-bubbles

Android Q: Error when using bubble in android java.lang.NoSuchMethodError


I am trying to use This new feature in Android and getting error

java.lang.NoSuchMethodError: No direct method <init>()V in class Landroid/app/Notification$BubbleMetadata$Builder; or its super classes 

(declaration of 'android.app.Notification$BubbleMetadata$Builder' appears in /system/framework/framework.jar)

Project is building with 'android-Q' but the app crashes when an activity starts.

This is the sample code I am using in MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Create bubble intent
        val target = Intent(this, MainActivity::class.java)
        val bubbleIntent = PendingIntent.getActivity(this, 0, target, 0 /* flags */)

// Create bubble metadata
        val bubbleData = Notification.BubbleMetadata.Builder()
            .setDesiredHeight(600)
            // Note: although you can set the icon is not displayed in Q Beta 2
            .setIcon(Icon.createWithResource(this, R.drawable.notification_icon_background))
            .setIntent(bubbleIntent)
            .build()

// Create notification
        val chatBot = Person.Builder()
            .setBot(true)
            .setName("BubbleBot")
            .setImportant(true)
            .build()

        val builder = Notification.Builder(this, 11.toString())
            .setContentIntent(bubbleIntent)
            .setSmallIcon(R.drawable.notification_icon_background)
            .setBubbleMetadata(bubbleData)
            //.addPerson(chatBot)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(11, builder.build())


    }
}

AndroidManifest.xml

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 'android-Q'
    defaultConfig {
        applicationId "com.example.mytestapplication"
        targetSdkVersion 28
        minSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

Solution

  • To test with the Q beta image in Android, you need to follow this instructions

    Actually to use Q APIs you need to install Android Studio 3.5 Preview and target the Q android version in the app build.gradle like below:

    android {
        compileSdkVersion 'android-Q'
    
        defaultConfig {
           ...
           targetSdkVersion 'Q'
           ...
        }
    }