我已经编写了一个实现某些Android通知功能的AAR库。当我尝试在运行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)
我确保我的代码遵循Gson中显示的内容
该异常发生在下面的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'
}
该错误表明在设备上运行时,new NotificationCompat.Buider
带有上下文和String CHANNEL_ID
的构造函数不存在,但是Android Studio构建该模块没有任何错误。如果Android Studio正确构建了AAR(该方法在构建时已存在于库中),则该方法在运行时不应该存在吗?
答案 0 :(得分:0)
我的最佳猜测是,即使您的库指定支持28.0.0或更高版本,您正在使用该库的应用程序也会强制使用版本比26.1.0更旧的支持库。您可以运行以下命令进行验证:
./gradlew :app:dependencyInsight --configuration releaseCompileClasspath --dependency support-compat
应该向您显示正在引入的版本以及从何处引入。请注意,如果您的应用程序配置中有多个productFlavors
,则您的配置可能会有所不同(例如,如果您具有风味“生产”,则应指定--configuration productionReleaseCompileClasspath
)。
基本上,您的库是使用support-compat
28. +编译的(无论何时解决,我强烈建议您使用固定的版本号),但是当包含在应用程序中时,应用程序配置会拉与您的库不兼容的support-compat
库的另一个版本中。您需要更新该应用程序,以确保它使用的是support-compat
26.1.0或更高版本。