I basically have a flutter application, when I run the app on my physical Android device in debug mode, it runs smoothly as expected, even when I run it in release mode, it runs as expected.
The issue is when I build the appbundle for release to Google Console. The app acts in a strange way where on a fresh install it should display the walkthrough screens (skip screens). on the released app on google console, it doesnt show these screens, it straight away goes to the Login Screen. The api’s as well, same issue.
I want to add that the app of the Google console version only starts to work normally when I go to the App info from my device and clear the data and cache and everything starts to work as intended.
This is my code:
main:
Main
void main() async {
// Make sure that Flutter is initialized before accessing SharedPreferences
WidgetsFlutterBinding.ensureInitialized();
// Restrict the app to portrait mode
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
await Styles.loadAppConfig();
// Start the app with the selected home page wrapped in MaterialApp
runApp(const MyApp(
homePage: SplashScreen(),
));
}
SplashScreen inside the initState after launch screen:
SplashScreen
Timer(const Duration(seconds: 2), () async {
if (mounted) {
// Navigate to the home screen after 2 seconds
// Read the flag from shared preferences
SharedPreferences prefs = await SharedPreferences.getInstance();
// Check if this is a new user; if it's null, set it to true
bool isNewUser = prefs.getBool('isNewUser') ?? true;
bool isLoggedIn = prefs.getBool('isUserLoggedIn') ?? false;
bool isLoggedInMemberStored = await prefs.setBool('isLoggedInMemberStored', false);
bool isSessionExpired = prefs.getBool('isSessionExpired') ?? false;
// Determine the home page based on the user's status
Widget homePage = const LoginPage(); // only initialization for the homepage
// If this is the first launch of the app
if (isNewUser) {
// Set the log in to false in shared preferences (confirmation)
prefs.setBool('isUserLoggedIn', false);
// Display the walkthrough if not logged in
if (!isLoggedIn) {
homePage = WTpageController();
}
} else {
// Lead the user to login if not a new user
if (isLoggedIn) {
SessionManager().startSession();
homePage = NavbarController();
} else {
homePage = LoginPage();
}
}
runApp(MyApp(
homePage: homePage,
));
}
});
build.gradle:
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def keystorePropertiesFile = rootProject.file('key.properties')
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
def flutterVersionCode = '36' // Update the version code to '2'
def flutterVersionName = '1.0.36' // Update the version name to '1.0.1'
android {
namespace "com.****.****"
compileSdk 34
ndkVersion flutter.ndkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
applicationId "com.****.****"
minSdkVersion 21
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
// Default to debug signing config for release builds
signingConfig signingConfigs.release
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
flutter {
source '../..'
}
dependencies {}
I am not sure, but I think my logic is correct, because its working as intended when I am running the app directly to my Mobile device.
I tried to make changes to the build types of release:
buildTypes {
release {
// Default to debug signing config for release builds
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
I added a function and called it after if (isNewUser) {
where it clears the app data and cache, because it works normally when I manually delete the cache and data of the app:
Future<void> _clearCacheAndData() async {
await _clearCache();
await _clearAppData();
await _clearSharedPreferences();
}
Future<void> _clearCache() async {
try {
Directory tempDir = await getTemporaryDirectory();
Directory cacheDir = await getApplicationCacheDirectory();
if (tempDir.existsSync()) {
tempDir.deleteSync(recursive: true);
print("clearing tempDir");
}
if (cacheDir.existsSync()) {
cacheDir.deleteSync(recursive: true);
print("clearing cacheDir");
}
} catch (e) {
print("Error clearing cache: $e");
}
}
Future<void> _clearAppData() async {
try {
Directory appDir = await getApplicationDocumentsDirectory();
if (appDir.existsSync()) {
appDir.deleteSync(recursive: true);
print("clearing appDir");
}
} catch (e) {
print("Error clearing app data: $e");
}
}
Future<void> _clearSharedPreferences() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.clear();
} catch (e) {
print("Error clearing shared preferences: $e");
}
}
Could this be an issue related to Google Console?
Can you please help me and if needed, I can provide more blocks of code or explain in detail
Hadi Emile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Go to your AndroidManifest.xml, and find these attributes below to check whether they are set to true
android:allowBackup="true"
android:fullBackupContent="true"
It will back up your local storage data even when you uninstalled app, so when you re-install, these data will auto come back.
Eg: some apps did it like Spotify, if you uninstalled this app and then re-install it, open the app and it will auto-login so you don’t need to do this action
For more info, check https://developer.android.com/guide/topics/manifest/application-element#allowbackup