Saturday, February 25, 2023

How to change package name in flutter

In some cases, for the android application it may be required to change the package name of the application. It's very easy to do it in Android studio. But when we are using flutter to develop the hybrid application, it is required to use some packages to do this job for us.

To achieve this we have to download a package : change_package_name. It does all the below changes:

  • Update AndroidManifest.xml files for release, debug & profile
  • Update build.gradle file
  • Update MainActivity file. Both java & kotlin supported.
  • Move MainActivity file to new package directory structure.
  • Delete old package name directory structure.
We can use this package as below:

First we need to add this package name to pubspec.yaml file as the dev_dependency

dev_dependencies: 
  change_app_package_name: ^1.1.0

Not migrated to null safety yet? use old version like this

dev_dependencies: 
  change_app_package_name: ^0.1.3
You can see this how i have added  it to my pubspec.yaml file


Save this file and Run the below command :

flutter pub get


And it gets all the dependencies. and now using this package we need to change the application's package name. Now run the below command to change the package name

flutter pub run change_app_package_name:main com.new.package.name
com.new.package.name is the new package name to be changed. 

This will automatically updates all the required package name and it's references moves all the required files to the new package And deletes the new package name directory structure.

That's all. Happy coding..