How to remove or identify unused packages from flutter to reduce size of the project? - flutter

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
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`.

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

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.

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.

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.

You can use this three steps to clear this issue
'flutter clean'
Remove the unwanted dependencies in pubspec.yaml
Delete the pubspec.lock
Reload the project and again perform 'pub get' and run the iOS/Android project

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

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.

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

Related

purchases_flutter minimum deployment target error

I am receiving the following error when I compile my FLutter project after adding package:
purchases_flutter: ^4.0.1 to it ....
[!] CocoaPods could not find compatible versions for pod "purchases_flutter":
In Podfile:
purchases_flutter (from .symlinks/plugins/purchases_flutter/ios)
Specs satisfying the purchases_flutter (from .symlinks/plugins/purchases_flutter/ios)
dependency were found, but they required a higher minimum deployment target.
I have checked the Readme in PubDev for this package which says:
minimum targets iOS 9.0+ is required.
However when I look in my PoDFile I have platform :ios, '10.0'. And in Xcode:
Am I missing something here. I haven't seen any other Deployment targets in Xcode or Podfile.
Would really appreciate it, if someone could point me in the correct direction with this.
Many thanks.
I have also tried creating a new Flutter project to test this from the start again and used the terminal commands as suggested below.
I still get the same error... here is a full screen shot of my Android Studio window. Showing the error, my IOS drive and Podfile.
You need to prioritize your dependencies. If the problem is only with this package or plugin, just add it with flutter pub add [dependency name], avoiding directly writing pubspec.yaml It prevents conflict between package versions, automatically finding appropriate version. Then in Podfile top define minimum version. Delete pubspec.lock file, ios/Podfile.lock file and ios/Pods directory. Execute flutter pub get. Enter ios directory via Terminal, execute pod install and try to run application again. If your Mac has apple silicon chip pod install command will be a bit different. Let me know, it worked or not.

Flutter - Cannot find package in .pub-cache reference

I am trying to use tflite_flutter plugin for running a custom ml function from a tflite model. It is working fine on android but for the iOS setup, it requires us to add TensorFlowLiteC.framework on ~/.pub-cache/hosted/pub.dartlang.org/tflite_flutter-<plugin-version>/ios/ folder but I cannot find this folder no matter what I do. It is not just because it is hidden, the tflite_flutter-<plugin-version> folder is not there at all.
I tried:
Adding dependency directly from git.
tflite_flutter:
git:
url: git://github.com/am15h/tflite_flutter_plugin.git
ref: v0.9.0
Running
flutter pub cache repair
flutter pub cache add tflite_flutter
Help would be much appreciated.
use_frameworks!
pod 'TensorFlowLiteSwift'
followed by running pod install in your ios folder.
From official doc
https://www.tensorflow.org/lite/guide/ios
As a developer, you are not bound to using flutter packages only. You can directly add dependencies to your podfile or build.gradle as well.
You'll find your package in ~/development/tools/flutter/.pub-cache/hosted/pub.dartlang.org/

Flutter Xcode build failed due to missing dependencies even when those have been deleted from pubspec.yalm

When I add a dependency to the pubspec.yaml file no matter if the IDE runs the pub get command or if I run it manually, it seems like the packages are being imported correctly (or that is what I think), however, if I "change my mind" and delete the package from the pubspec.yaml file, and again pub get, I am unable to build the app anymore, seems like the packages are still linked somehow to the app, even when there is no code related to them, the only way I can get back to work is by cleaning up all... e.g.
cd ios
cd ..
pod deintegrate
flutter clean
flutter pub cache repair
flutter pub get
cd ios
Pod install
Pod repo update
Maybe I am missing a step when working with dependencies.

Flutter web cannot use packages from mobile project

A few months ago I made an android application using flutter. The application requires various dependencies such as intl, cached network image, stopper, carousel_slider, etc.
Then right now I want to convert the project to flutter_web. Here are the steps that I did:
clone github web flutter
I run the command "flutter packages pub global activate webdev"
Then I edited pubspec.yaml and I adjusted it to pubspec.yaml in the previous project (android project)
Here is the contents of pubspec.yaml on the web flutter project:
name: flutter_web.examples.hello_world
environment:
  # You must be using Flutter> = 1.5.0 or Dart> = 2.3.0
  sdk: '> = 2.3.0-dev.0.1 <3.0.0'
dependencies:
  flutter_web: any
  flutter_web_ui: any
  stopper: ^ 1.0.1
dev_dependencies:
  build_runner: ^ 1.4.0
  build_web_compilers: ^ 2.0.0
dependency_overrides:
  flutter_web:
    git:
      url: https://github.com/flutter/flutter_web
      path: packages / flutter_web
  flutter_web_ui:
    git:
      url: https://github.com/flutter/flutter_web
      path: packages / flutter_web_ui
  provider:
    git:
      url: https://github.com/kevmoo/provider
      ref: flutter_web
When I run 'pub dev', an error occurs while resolving the package stopper. Then I tried to open github from the package stopper (https://github.com/aryzhov/flutter-stopper), after that I checked the files in the lib folder. There I found a file called stopper.dart. It turns out that in the file, still using
 import 'package: flutter / material.dart';
So that makes my pubspec error. Because the code should be replaced with
import 'package: flutter_web / material.dart';
Therefore, I tried to outsmart the problem by removing the dependency stopper on pubspec.yaml, then I created a stopper.dart file manually, then I saved it in the lib/mypackages folder. After that, I import stopper.dart from the lib/mypackages folder (it's no longer through the package).
The package stopper is just one of the packages that I use on my Android project. Actually there are many other packages that have the same problem as the package stopper. The point is I have to create the package files manually, then I change "package: flutter / ..." to "package: flutter_web /" manually, then I can import them.
What I want to ask, is there a more elegant and simple way?
Hi the instruction you are following is from an old repository here. As you can see this is discontinued. You can find the informaiton in the REadme.
Instead you should follow the instructions in this page. Read it once carefully and I would suggest to keep a backup of your main project and work on a copy.
First you will have to enable the flutter-web using flutter config --enable-web.
Then you will have to run flutter create .

producing flutter (Xcode) testflight archive fails with multiple commands produce

need to produce an archive so we can upload to TestFlight so followed instructions in https://flutter.dev/docs/deployment/ios . my flutter runs on both real iPhone and simulator with no problem. so I produce a release version as required then open the resultant Xcode project (runner). immediately see the error
:-1: Multiple commands produce '/Users/jmcfet/Library/Developer/Xcode/DerivedData/Runner-cwxykfeiijpirmgbtrkzktdrahbt/ArchiveIntermediates/Runner/InstallationBuildProductsLocation/Applications/Runner.app/Frameworks/Flutter.framework':
1) Target 'Runner' has copy command from '/Users/jmcfet/AndroidStudioProjects/sis/ios/Flutter/Flutter.framework' to '/Users/jmcfet/Library/Developer/Xcode/DerivedData/Runner-cwxykfeiijpirmgbtrkzktdrahbt/ArchiveIntermediates/Runner/InstallationBuildProductsLocation/Applications/Runner.app/Frameworks/Flutter.framework'
2) That command depends on command in Target 'Runner': script phase “[CP] Embed Pods Frameworks”
now this does not happen when I do the same against the "Flutter Hello world" app so wondering what is messed up in my Flutter app
no luck :
johns-MacBook-Air:ios jmcfet$ pod install
Analyzing dependencies
Fetching podspec for Flutter from .symlinks/flutter/ios
Fetching podspec for video_player from .symlinks/plugins/video_player/ios
Downloading dependencies
Using Flutter (1.0.0)
Using video_player (0.0.1)
Generating Pods project
Integrating client project
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods 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.
[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target Runner to Pods/Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig or include the Pods/Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig in your build configuration (Flutter/Release.xcconfig).
not an IOS expert but looks like the install was not successful. little confused as the Xcode project was generated by Flutter so why is Flutter generating a bad config file. if so looks like a bug to me