Integrate flutter modules with Cocoapods

Soumya Mahunt
ITNEXT
Published in
3 min readMar 21, 2022

--

Announcing cocoapods plugin: cocoapods-embed-flutter, providing a way to declare flutter modules as target dependency in Podfile.

Currently, if you want to integrate a flutter module in an existing iOS project you can follow the official guide. Then, what’s the need for a separate plugin you ask? cocoapods-embed-flutter addresses the complexity of the official solution while keeping your Podfile really simple.

One thing you have to make sure with the official solution is to run flutter pub get, before running pod install. And if your flutter module isn’t in the same repository as your iOS project then you have to dive into git submodules.

Solution?

If you aren’t familiar with Gemfile I will highly suggest to checkout cocopods’ guide which goes into much depth. For the scope of this article, you can just declare cocoapods-embed-flutter as your dependency in Gemfile like this:

gem 'cocoapods-embed-flutter'

and run bundle install. Alternatively, if you don’t want to use Gemfile (even though I would highly suggest doing so), you can use sudo gem install cocoapods-embed-flutter to install this plugin.

Now that you’re all set up, add cocoapods-embed-flutter as a plugin at the top of your Podfile:

plugin 'cocoapods-embed-flutter'

After this, for any target, you can declare a flutter module dependency with the same syntax you use for pods by replacing the keyword pod with pub. To use a flutter module named my_cool_module present locally use:

target 'my_app' do
pub 'my_cool_module', :path => 'path/to/module'
end

Here :path can be directly pointing to the pubspec.yaml file or the directory containing pubspec.yaml. If your flutter module has the same name as the project folder then you can also provide the parent directory of your flutter module project.

Now for the more complex scenario, where the flutter module might be hosted in an external git repository, you can use:

target 'my_app' do
pub 'my_cool_module', :git => 'https://github.com/me/my_cool_module.git', :branch => 'dev'
pub 'flutter_module', :git => 'https://github.com/me/my_cool_module.git', :tag => '0.7.0'
pub 'flutter_module', :git => 'https://github.com/me/my_cool_module.git', :commit => '082f8319af'
end

From version 0.6 onwards, if your flutter module project isn’t at the root of your repository, you can also specify the relative path in your repository:

pub 'flutter_module', :git => 'https://github.com/me/my_cool_module.git', :tag => '0.7.0', :path => 'custom/path

You have full flexibility to provide a branch or tag or a specific commit to use in the git source. After declaring all dependencies, you can just run pod install and let cocoapods do all the work for you. For a more detailed example or a working sample check out the example app in the GitHub repo.

Links

Thanks for reading! Hopefully, you found this helpful, in case you need additional features or have some suggestions, please engage in the GitHub repo.

DartBuild/cocoapods-embed-flutter: Cocoapods plugin to allow declaring flutter module as dependency (github.com)

--

--

Senior Software Engineer at MoEngage | System architecture enthusiast | Ex Tataneu