Search code examples
androidflutterbuildcrashpackage-name

Flutter - Modified Android package name = app not working (crashes)


A few weeks ago, I developed an app that utilized Firebase as its backend. However, I found the setup to be messy, and I wanted to change the package name of my Android app. When I attempted to reconfigure Firebase with a new Firebase project for the app, I encountered errors while trying to use flutterfire configure to overwrite the previous configuration.

To resolve this, I decided to create a new Flutter project using flutter create, copied the old lib folder to replace the one in the new Flutter project, and did the same for the pubspec.yaml file. After that, I set up the new Firebase project using flutterfire configure, specifying com.mesak.done as the Android package name.

While my IDE didn't flag any errors in the code, I encountered numerous errors when trying to launch the app on my Android emulator. Most of these errors were related to the Android package name modification not being done correctly.

It's possible that I didn't fix the issues properly because now, when I attempt to launch the app on my Android emulator, the app builds successfully and the landing page appears on the screen. However, as soon as I try to interact with my emulator or my IDE, my computer crashes.

Regarding Firebase, here are some key details:

  • I activated authentication with email and password.
  • I activated authentication with Google.
  • The Android package name for my Android registered app is com.mesak.done (the correct one).
  • I configured Firebase Firestore and Firebase Storage correctly.

In my view, the error is unlikely to be related to the Firebase configuration but instead stems from the Android setup.

Here is my 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"

Here is my 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
}

Here is my 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 = "../.."
}

Here is my 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>

Here is my android/app/src/main/kotlin/com/mesak/done/MainActivity.kt :

package com.mesak.done

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity()

If you need any more details about the code, click here to open the GitHub repo (that contains all the Flutter project.

I really need your help, have a great coding :)


Solution

  • @KULDEEPGUPTA resolved the issue by using my GitHub repository. The question is now closed. He pointed out my mistake: "You missed a variable in the app/build.gradle file. For example, def localProperties = new Properties()". Have a great time coding :)