Flutter - 修改 Android 包名称 = 应用程序无法工作(崩溃)

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

几周前,我开发了一个使用 Firebase 作为后端的应用程序。然而,我发现设置很混乱,我想更改我的 Android 应用程序的包名称。当我尝试使用应用程序的新 Firebase 项目重新配置 Firebase 时,我在尝试使用

flutterfire configure
覆盖以前的配置时遇到了错误。

为了解决这个问题,我决定使用

flutter create
创建一个新的 Flutter 项目,复制旧的
lib
文件夹来替换新 Flutter 项目中的文件夹,并对
pubspec.yaml
文件执行相同的操作。之后,我使用
flutterfire configure
设置新的 Firebase 项目,并将
com.mesak.done
指定为 Android 包名称。

虽然我的 IDE 没有标记代码中的任何错误,但我在尝试在 Android 模拟器上启动应用程序时遇到了许多错误。这些错误大多与Android包名修改不正确有关。

我可能没有正确解决问题,因为现在,当我尝试在 Android 模拟器上启动该应用程序时,该应用程序已成功构建,并且登录页面出现在屏幕上。然而,一旦我尝试与模拟器或 IDE 交互,我的计算机就会崩溃。

关于 Firebase,以下是一些关键细节:

  • 我使用电子邮件和密码激活了身份验证。
  • 我激活了 Google 身份验证。
  • 我的 Android 注册应用程序的 Android 包名称是
    com.mesak.done
    (正确的名称)。
  • 我正确配置了 Firebase Firestore 和 Firebase 存储。

在我看来,该错误不太可能与 Firebase 配置有关,而是源于 Android 设置。

这是我的

settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.0" apply false
    // START: FlutterFire Configuration
    id "com.google.gms.google-services" version "4.3.15" apply false
    // END: FlutterFire Configuration
    id "org.jetbrains.kotlin.android" version "2.0.20" apply false
}

include ":app"

这是我的

android/build.gradle

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = "../build"
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

这是我的

android/app/build.gradle

plugins {
    id "com.android.application"
    // START: FlutterFire Configuration
    id 'com.google.gms.google-services'
    // END: FlutterFire Configuration
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.mesak.done"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.mesak.done"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig = signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}

这是我的

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- File permission -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
    <!-- File permission (end) -->
    <application android:label="Done" android:name="${applicationName}" android:icon="@mipmap/launcher_icon">
        <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:taskAffinity="" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme"/>
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data android:name="flutterEmbedding" android:value="2"/>
    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

这是我的

android/app/src/main/kotlin/com/mesak/done/MainActivity.kt

package com.mesak.done

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity()

如果您需要有关代码的更多详细信息,请单击此处打开 GitHub 存储库(其中包含所有 Flutter 项目。

我真的需要你的帮助,祝你编码顺利:)

android flutter build crash package-name
1个回答
0
投票

@KULDEEPGUPTA 使用我的 GitHub 存储库解决了该问题。问题现已结束。他指出了我的错误:“你错过了 app/build.gradle 文件中的一个变量。例如,def localProperties = new Properties()”。祝你编码愉快:)

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