Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awesome Notifications: PlatformException(SHARED_PREFERENCES_NOT_AVAILABLE

I get an error when I use API 34 release mode

Version: awesome_notifications: ^0.9.3+1

build.gradle

android {
namespace "com...."
compileSdk 34
ndkVersion '26.1.10909125'

compileOptions {
    sourceCompatibility JavaVersion.VERSION_18
    targetCompatibility JavaVersion.VERSION_18
}
kotlinOptions {
jvmTarget = '18'
}
kotlin {
jvmToolchain(18)
}

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

defaultConfig {
    applicationId "com...."
    minSdkVersion 22
    targetSdkVersion 34
    multiDexEnabled true
    versionCode 11
    versionName "1.11" 
}

buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.debug
    }

    debug {
        shrinkResources false
        minifyEnabled false
        signingConfig signingConfigs.debug
    }
}
buildToolsVersion '34.0.0'
}

Error when I do flutter run --release, but when debug mode all runs normally.

I/flutter (24429): error notif init: PlatformException(SHARED_PREFERENCES_NOT_AVAILABLE, class p2.b isn't parameterized, sharedPreferences.get, null) I/flutter (24429): Lokaasi error: #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648) I/flutter (24429): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334) I/flutter (24429): I/flutter (24429): #2 MethodChannelAwesomeNotifications.initialize (package:awesome_notifications/awesome_notifications_method_channel.dart:300)

Line 300 leads to this code: var result = await methodChannel.invokeMethod(CHANNEL_METHOD_INITIALIZE, { INITIALIZE_DEBUG_MODE: debug, INITIALIZE_DEFAULT_ICON: defaultIconPath, INITIALIZE_CHANNELS: serializedChannels, INITIALIZE_CHANNELS_GROUPS: serializedChannelGroups, BACKGROUND_HANDLE: dartCallbackReference!.toRawHandle() });

I'm try change version awesome_notifications package, not work. Saya sudah mencari solusi dan saya menemukan ini awesome-notifications-flutter-not-working-in-release-mode, namun belum menemukan solusi.

like image 298
Rian Pratama Avatar asked Nov 01 '25 11:11

Rian Pratama


1 Answers

The issue was with the awesome_notification plugin using both com.google.common.reflect and com.google.gson.reflect for TypeToken but only had proguard rules to avoid R8 full mode code shrinking for the gson package.

The R8 full mode is set to true by default now, hence this issue happens. See more on this here and here.

Add the below rules in your proguard-rules.pro file to fix the issue:

-keep class com.google.common.reflect.TypeToken
-keep class * extends com.google.common.reflect.TypeToken

Answer from my own post: Proguard rules to avoid Shared Preferences Not Found in flutter

like image 154
OutdatedGuy Avatar answered Nov 03 '25 15:11

OutdatedGuy