Changing the Package Name in a Flutter Project

Overview

Changing the package name of a Flutter project involves updating identifiers for both Android and iOS platforms. This guide provides step-by-step instructions to ensure that your app’s package name is updated correctly and that the application continues to function as expected.


Changing the Package Name

1. Change the Package Name for Android

  1. Open the Project in Your IDE

    Open your Flutter project in Android Studio or Visual Studio Code.

  2. Update AndroidManifest.xml

    • Navigate to android/app/src/main/AndroidManifest.xml.
    • Find the package attribute in the manifest tag and update it to the new package name:
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.new.package.name">
  3. Update build.gradle

    • Open android/app/build.gradle.
    • Update the applicationId in the defaultConfig block to the new package name:
      defaultConfig {
      applicationId "com.new.package.name"
      ...
      }
  4. Rename Package Directories

    • Navigate to android/app/src/main/java/com/old/package/name/.
    • Right-click on the com directory and select Refactor -> Rename in Android Studio. Rename it to match the new package name’s structure.
    • Ensure that all package names and import statements in Java/Kotlin files are updated accordingly.
  5. Update MainActivity.java or MainActivity.kt

    • If your project uses Java, open android/app/src/main/java/com/new/package/name/MainActivity.java and verify the package statement at the top matches the new package name.
    • If your project uses Kotlin, do the same for MainActivity.kt.

2. Change the Package Name for iOS

  1. Open the Project in Xcode

    • Navigate to the ios directory of your Flutter project.
    • Open the .xcworkspace file in Xcode.
  2. Update the Bundle Identifier

    • In Xcode, select the project file in the Project Navigator.
    • Select the target (usually named after your project) under the "Targets" section.
    • Go to the General tab.
    • Find the Bundle Identifier field and update it to the new package name (usually formatted as com.new.package.name).
  3. Update Info.plist

    • Open ios/Runner/Info.plist.
    • Find any occurrences of the old package name and update them if necessary.
  4. Update Project Settings

    • In Xcode, navigate to Project -> Build Settings.
    • Ensure that the Product Bundle Identifier is updated to the new package name.

3. Update Dart Code

  1. Search for Old Package Name

    • Perform a global search in your IDE for any occurrences of the old package name within the Dart code and comments.
    • Replace them with the new package name if necessary.

4. Clean and Rebuild the Project

  1. Clean the Project

    Open your terminal or command prompt and navigate to your project directory. Run:

    flutter clean
  2. Get Dependencies

    Run:

    flutter pub get
  3. Rebuild the Project

    Run:

    flutter build

5. Test the Application

  1. Run on Emulator or Device

    Ensure that the app builds and runs successfully on your emulator or physical device.

  2. Check for Errors

    Verify that no errors related to the package name persist and that the app functions as expected.


Summary

You have successfully changed the package name of your Flutter project for both Android and iOS platforms. By following these steps, you ensured that the new package name is correctly reflected in your project’s configuration files and codebase.

For more detailed information, refer to the official Flutter documentation and platform-specific guides for Android and iOS.