Flutter 应用程序在调试时与在 Google 控制台上发布时不同

问题描述 投票:0回答:1

我基本上有一个flutter应用程序,当我在物理Android设备上以调试模式运行该应用程序时,它按预期运行顺利,即使我在发布模式下运行它,它也按预期运行。 问题是当我构建应用程序包以发布到 Google Console 时。该应用程序的行为方式很奇怪,在全新安装时它应该显示演练屏幕(跳过屏幕)。在谷歌控制台上发布的应用程序上,它不会显示这些屏幕,它会立即进入登录屏幕。 api也是一样的问题。 我想补充一点,只有当我从我的设备转到应用程序信息并清除数据和缓存时,Google 控制台版本的应用程序才开始正常工作,一切都开始按预期工作。

这是我的代码: 主要的: 主要

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(),
  ));
}

启动屏幕后 initState 内的 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,
          ));
        }
      });

构建.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 {}

我不确定,但我认为我的逻辑是正确的,因为当我直接在移动设备上运行应用程序时,它会按预期工作。 我尝试更改发布的构建类型:

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'
        }
    }

我添加了一个函数,并在

if (isNewUser) {
之后调用它,它清除应用程序数据和缓存,因为当我手动删除应用程序的缓存和数据时它可以正常工作:

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");
    }
  }

这可能是与 Google Console 相关的问题吗? 您能帮助我吗?如果需要,我可以提供更多代码块或详细解释

flutter debugging mobile release google-developers-console
1个回答
0
投票

转到您的 AndroidManifest.xml,找到下面的这些属性,检查它们是否设置为 true

android:allowBackup="true"
android:fullBackupContent="true"

即使您卸载应用程序,它也会备份您的本地存储数据,因此当您重新安装时,这些数据将自动恢复。

© www.soinside.com 2019 - 2024. All rights reserved.