简体   繁体   中英

Flutter App doesn't work when changing package name

When I tried to change the package name in MainActivity.kt inside kotlin the app doesn't run

This error:

e: /Users/dma/development/flutter_share/android/app/src/main/kotlin/com/example/souqmubtath/MainActivity.kt: (8, 7): Redeclaration: MainActivity
e: /Users/dma/development/flutter_share/android/app/src/main/kotlin/dev/dhari/souqmubtath/MainActivity.kt: (8, 7): Redeclaration: MainActivity

FAILURE: Build failed with an exception.                                

* What went wrong:                                                      
Execution failed for task ':app:compileDebugKotlin'.                    
> Compilation error. See log for more details                           

* Try:                                                                  
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org                              

BUILD FAILED in 27s                                                     
Running Gradle task 'assembleDebug'...                                  
Running Gradle task 'assembleDebug'... Done                        27.6s
Gradle task assembleDebug failed with exit code 1

You haven't changed your package name correctly,

Just go to the following path

android > app > src >main > kotlin

Inside that you will find one more package like your_package_name containing MainActivity file.

In your case it is like, "com.example.souqmubtath "

So to change it properly, just go to your android manifest file, from there just find package="com.example.souqmubtath"

Just select com.example.souqmubtath and refactor it.

For refactor either select the package name -> right click on it -> Refactor -> Rename . OR Just select the package name and use refactor rename shortcut which is Shift+F6

You can rename it to your desired package name. In your case it is like "dev.dhari.souqmubtath"

Also, go to your android > app > build.gradle file path. Inside the file and defaultConfig{} find out applicationId and refactor it as well to the same name as your package name.

You have to update the package name inside the MainActivity.kt on the top.

If this is correct then check your manifest file, is there package name updated or not?

Only the two reasons that your app not working.

Problem encountered on Flutter

packagename: com.[companyname].[binairyname]

In my case there were two folders in android/app/src/main/kotlin/com/

In this folder there were:

  • example/binary/*
  • companyname/binary/*

Since after a flutter create command the companyname in packagename are init to example, I deduced that the example folder should be deleted

Problem solved

flutter --version
Flutter 2.2.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f4abaa0735 (9 weeks ago) • 2021-07-01 12:46:11 -0700
Engine • revision 241c87ad80
Tools • Dart 2.13.4

There's a useful package for changing it but it works well with android, I didn't try for IOS.

Firstly install the package:

change_app_package_name: ^1.0.0

Then in the terminal in your project paths do these commands:

flutter pub get

enter code here

Then write the line below and use the unique name you want after com. then press enter:

flutter pub run change_app_package_name:main com.demo.flutter_app will update files and delete old package names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM