36

I used some packages that I no longer need in my flutter project, namely the wilddog_auth and wilddog_sync, I can remove the imports from pubspec.yaml file, and my dart files (aka removing import 'package:wilddog_sync/wilddog_sync.dart' etc.) and remove imports in MainActivity.java as well as in Xcode project but I can't purge the unused files installed by flutter, cocoapod and gradle. Now is there a unified command in flutter where I can remove all unused packages at once?

I am pretty sure using flutter clean only removes build folder and using flutter packages get after removing packages from pubspec.yaml doesn't remove packages from cocoapod or gradle either.

For example, after flutter clean and flutter packages get I rebuilt the project:

Launching lib/main.dart on Android SDK built for x86 64 in debug mode...
Initializing gradle...
Resolving dependencies...
Running 'gradlew assembleDebug'...
Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk...
I/FlutterActivityDelegate(11331): onResume setting current activity to this
I/Choreographer(11331): Skipped 107 frames!  The application may be doing too much work on its main thread.
D/EGL_emulation(11331): eglMakeCurrent: 0x79f21b4dbec0: ver 2 0 (tinfo 0x79f219b4f160)
I/OpenGLRenderer(11331): Davey! duration=1903ms; Flags=1, IntendedVsync=7544454683637, Vsync=7546238016899, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=7546253803603, AnimationStart=7546253996603, PerformTraversalsStart=7546254171603, DrawStart=7546279222603, SyncQueued=7546288526603, SyncStart=7546295569603, IssueDrawCommandsStart=7546296644603, SwapBuffers=7546334455603, FrameCompleted=7546365204603, DequeueBufferDuration=2892000, QueueBufferDuration=199000, 
Syncing files to device Android SDK built for x86 64...
D/        (11331): HostConnection::get() New Host Connection established 0x79f219ab81e0, tid 11383
D/EGL_emulation(11331): eglMakeCurrent: 0x79f21b5e26e0: ver 2 0 (tinfo 0x79f219aa66c0)
W/IInputConnectionWrapper(11331): getCursorCapsMode on inactive InputConnection

cocoapods gradle

Yet all the package files remained.

Of course I could go into ./ios/ to run pods install to remove pods:

pod install
Analyzing dependencies
Fetching podspec for `Flutter` from `.symlinks/flutter/ios`
Removing Wilddog
Removing WilddogCore
Removing WilddogSync
Downloading dependencies
Using Flutter (1.0.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Automatically assigning platform `ios` with version `8.0` on target `Runner` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
Aero Wang
  • 8,382
  • 14
  • 63
  • 99
  • 5
    I don't know about Java/Swift/ObjectC dependencies, but for Dart dependencies it is enough to remove them from `pubspec.yaml` and run `flutter packages get`. – Günter Zöchbauer Aug 20 '18 at 08:31
  • 5
    Flutter has tree shaking compiler. so unused packages shouldn't be included in the bundle file. try running `flutter clean` before build. – Yamin Aug 20 '18 at 08:33
  • @Yamin it doesn't seem to remove gradle or cocoapod packages tho. – Aero Wang Aug 20 '18 at 08:34
  • If it's not working the problem should be with the plugin. the plugin's docs are in Mandarin (I think), so I couldn't follow the issue there. – Yamin Aug 20 '18 at 08:41
  • 1
    Removing the plugin from the pubspec.yaml is all you need to do. Then run `flutter packages get`. Flutter will automatically remove the native plugin code when you rebuild your project. – boformer Aug 20 '18 at 11:34
  • @boformer what do you mean by when I rebuild the project? – Aero Wang Aug 22 '18 at 07:05
  • Do you mean using `flutter build` command? I rebuilt successfully yet all the files in gradle and cocoapods remained. – Aero Wang Aug 22 '18 at 07:11
  • Did you remove the entries from `pubspec.yaml`? Does running `pod install` in `ios/` remove them for iOS? See also https://stackoverflow.com/a/13759016/217408 – Günter Zöchbauer Aug 22 '18 at 07:17
  • Yes remove the entries from `pubspec.yaml` is the first thing I did. Running running `pod install` in `./ios/` remove unused packages for Xcode project is obviously working (but you must modify the Xcode project first). My goal is to reduce the steps I need to take to clean up packages. – Aero Wang Aug 22 '18 at 07:21
  • https://github.com/flutter/flutter/issues/13638#issuecomment-413026066 might help here as well – Günter Zöchbauer Aug 22 '18 at 08:59
  • @GünterZöchbauer that's for cocoapods, how about gradle? – Aero Wang Aug 22 '18 at 09:25
  • @Aero "Rebuild" means running `flutter run` or `flutter build apk` – boformer Aug 22 '18 at 09:50
  • @boformer then I don't think it removes packages from cocoapods or gradle... – Aero Wang Aug 22 '18 at 12:11
  • I don't know if it is necessary for gradle as well or if gradle works differently. – Günter Zöchbauer Aug 22 '18 at 12:37
  • what do you mean remove packages? When the plugin is disabled, it's manifest and `build.gradle` are no longer merged and no longer added to your apk. No action is required – boformer Aug 22 '18 at 12:59
  • Gradle keeps an internal package cache, and packages are kept there to reduce network usage. but those packages are not merged into your apk! – boformer Aug 22 '18 at 13:01
  • I see @boformer does it automatically removed unused packages after a while or will I need to remove them manually? – Aero Wang Aug 23 '18 at 03:29
  • Once again, as long as you have enough hard drive space, there is hardly a reason to clear it. This might help you: https://stackoverflow.com/questions/23025433/how-to-clear-gradle-cache – boformer Aug 23 '18 at 08:10
  • @boformer oh I am just curious because it seems reasonable to have a package/cache control feature built it. – Aero Wang Aug 23 '18 at 10:27

10 Answers10

24

There is an intuitive way for it. Install this package

dev_dependencies:
  dependency_validator: ^x.x.x

Then run flutter pub run dependency_validator It will show all the details about the packages installed. Example: =>

These packages are pinned in pubspec.yaml:

  • dependency_validator: null -- This is a direct pin.
  • permission_handler: null -- This is a direct pin.
  • swipe_to: null -- This is a direct pin.

These packages are used in lib/ but are not dependencies:

  • connectivity
  • meta
  • path

The following packages contain executables, they are assumed to be used:

  • flutter_native_splash

These packages may be unused, or you may be using assets from these packages:

  • cupertino_icons
  • firebase_analytics
  • http
  • loading
  • sign_in_with_apple
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
3

Sometimes there might be packages that is not used in the project but you have installed sometime during the development stage. To find out those, First go to each dart file and remove the unused imports, then go to pubspec.yaml and comment the packages one by one and save the project. And then refresh the application(NOT HOT RELOAD). if the package us used in the project then it will throw you an error. After repeating the steps on all the packages you will be able to remove all the unwanted packages from your pubspec.

Now, delete the pubspec.lock file, and run the pub get again. Your project will now be having only the required packages and this could be the minimum size achievable.

enter image description here

Anees Hameed
  • 5,916
  • 1
  • 39
  • 43
3

delete the hosted folder of .pub-cache in flutter SDK directory

cd path/to/flutter_sdk_directory

rm -rf .pub-cache/hosted

delete the .packages file in the project root directory

 cd path/to/project_root_directory

 rm -rf .packages

get packages for the project

cd path/to/project_root_directory

flutter pub get

open the project in Android Studio

done.

2

There's an obvious way which is easy to overlook or think is not feasible.

If using IntelliJ, simply do "Find in files" (SHIFT+CTRL+F on PC, SHIFT+CMD+F on Mac), select "Directory" and choose the lib-folder of your project.

Then go through your list of plugins and search for the name of each one in turn. The plugin isn't used if

  • there are no search results for its name
  • it's only used in "generated_plugin_registrant.dart"
  • the only hits are import statements that are grey (marked as unused by IntelliJ)

Took me less than a minute to eliminate 8 unused plugins using this method.

Magnus
  • 17,157
  • 19
  • 104
  • 189
1

You can use this three steps to clear this issue

  1. 'flutter clean'
  2. Remove the unwanted dependencies in pubspec.yaml
  3. Delete the pubspec.lock

Reload the project and again perform 'pub get' and run the iOS/Android project

0

This behavior seems to be a bug. You can track it here https://github.com/flutter/flutter/issues/65718

Omatt
  • 8,564
  • 2
  • 42
  • 144
0

The best way to do this is

flutter clean

If this doesn't work just create a new project copy your old code into the new project and here you can add only the dependencies you want before running pub get.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

You can remove by typing in terminal dart pub remove <packagename>

Example : To remove flutter_svg:^0.22.0 , type dart pub remove flutter_svg

For more informations, check https://dart.dev/tools/pub/cmd/pub-remove

Jouby
  • 2,196
  • 2
  • 21
  • 33
0

Flutter takes care of that by running this command:

$ flutter pub cache repair

I tried at first to just remove webview_flutter: 4.1.0 from pubspec.yaml and run

$ flutter clean
$ flutter pub get

but Flutter keeps a cached version of popular packages in .pub-cache/hosted directory so you must either remove the cached packages manually or preferably by running the first command.

I'm using Flutter: 3.7.10 and Dart: 2.19.6

AMR SAMY
  • 61
  • 2
0

To remove a package in flutter you can use the command

dart pub remove package_name

Ref: https://dart.dev/tools/pub/cmd/pub-remove

innocent
  • 864
  • 6
  • 17