java - 无法在 Android O 中构建 NotificationCompat.Builder

标签 java android

我编写了一个 AAR 库来实现一些 Android 通知功能。当我尝试在运行 Android O (26+) 的设备上创建通知时,出现以下错误:

java.lang.NoSuchMethodError: No direct method <init>(Landroid/content/Context;Ljava/lang/String;)V in class Landroid/support/v4/app/NotificationCompat$Builder; or its super classes (declaration of 'android.support.v4.app.NotificationCompat$Builder' appears in /data/app/org.myapp-VuZkSUvRuAyiaIG5th_Mrw==/base.apk)
       at org.myapp.notificationutility.NotificationService.createNotification(NotificationService.java:208)
       at org.myapp.notificationutility.NotificationService.showNotification(NotificationService.java:169)
       at org.myapp.notificationutility.NotificationService.onStartCommand(NotificationService.java:117)
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3668)
       at android.app.ActivityThread.access$1600(ActivityThread.java:200)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:193)
       at android.app.ActivityThread.main(ActivityThread.java:6680)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我确保我的代码遵循 https://developer.android.com/training/notify-user/build-notification#java 中显示的内容

异常发生在下面的 new NotificationCompat.Buider 行。我的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String name = "My App";
    NotificationChannel channel = new NotificationChannel(
        CHANNEL_ID,
        name,
        NotificationManager.IMPORTANCE_LOW);
        channel.setLightColor(Color.CYAN);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager notificationManager =
        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(icon)
    .setContentTitle(notificationTitle)
    .setContentText(notificationContent)
    .setPriority(NotificationCompat.PRIORITY_LOW)
    .setAutoCancel(false)
    .setOngoing(true);

我的gradle文件:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compileOnly fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:support-compat:28.+'
    testImplementation 'junit:junit:4.12'
}

该错误表明在设备上运行时不存在采用上下文和 String CHANNEL_IDnew NotificationCompat.Buider 构造函数,但 Android Studio 构建模块时没有任何错误。如果 Android Studio 构建 AAR 没有错误(该方法在构建时存在于库中),那么该方法不应该在运行时存在吗?

最佳答案

我最好的猜测是,您在其中使用此库的应用程序强制使用早于 26.1.0 的支持库版本,即使您的库指定它需要 28.0.0 或更高版本。您可以通过运行来验证这一点:

./gradlew :app:dependencyInsight --configuration releaseCompileClasspath --dependency support-compat

它应该向您显示正在引入的版本以及从何处引入。请注意,如果您的应用配置中有多个 productFlavors,您的配置可能会有所不同(例如,如果您有 flavor “production”,您将指定 --configuration productionReleaseCompileClasspath)。

基本上,您的库是使用 support-compat 28.+ 编译的(无论当时解析为什么——我强烈建议改用固定版本号),但是当包含在应用程序中时,应用程序配置会引入与您的库不兼容的不同版本的 support-compat 库。您需要更新应用以确保它使用 support-compat 26.1.0 或更高版本。

关于java - 无法在 Android O 中构建 NotificationCompat.Builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54041193/

相关文章:

java - 如何将一个区间[start,end]分成n个距离相等的点?

android - 向特定用户android发送通知

java - 了解类属性是否在类函数中使用

java - 使 Asynctask 返回 Cursor

java - 标准 MBean 的描述

java - Spring Boot 中 MessageSource 的 NoSuchMessageException

php - Android 和 PHP : POST-ing data from Android to mysql db using PHP

android - 自定义通知最大高度?

java - OpenCV4Android 错误 : Assertion failed in Core. minMaxLoc(mROI) 方法

Android Espresso multidex 失败