Skip to content
This repository was archived by the owner on Feb 11, 2024. It is now read-only.

flutter macos support #31

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

unsuitable001
Copy link
Contributor

@unsuitable001 unsuitable001 commented Sep 7, 2021

This PR intends to add macOS support for flutter. macOS support for Dart CLI is already present.

Related #5

cc @dcharkes

@unsuitable001
Copy link
Contributor Author

Weirdly, while running in Flutter mode, macOS trying to locate the binary from:

libUri: file:///Users/unsuitable001/Library/Containers/dev.google.cronetExample/Data/libcronet.86.0.4240.198.dylib

hence, it fails to load the binary.

However, in Dart CLI mode it tries to locate from:

libUri: file:///Users/unsuitable001/development/projects/cronet.dart/libcronet.86.0.4240.198.dylib

which is the correct location.

@unsuitable001
Copy link
Contributor Author

Reference: flutter/flutter#61950

Copy link
Member

@dcharkes dcharkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind mentioning which files are generated by flutter create and were not modified to ease the reviewing? :)

@@ -37,6 +37,15 @@ Currently, Android and Desktop Platforms (Linux, Windows and MacOS) are supporte

Optionally, enable cleartext traffic by adding `android:usesCleartextTraffic="true"` to `AndroidManifest.xml` file.

***Note for MacOS (Flutter):** Remember to add the following lines to `macos/Runner/Release.entitlements` and `macos/Runner/DebugProfile.entitlements` to enable networking in MacOS (Flutter).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*** -> **

@unsuitable001
Copy link
Contributor Author

Oh right 😅. I forgot. Doing that in an hour.

Copy link
Member

@dcharkes dcharkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a macos folder outside the example. (Apparently I need better eyes!) The way you have set it up requires all your users on MacOS to repeat your steps.

Instead, run flutter create -t plugin --platforms=macos . on the root and modify the podspec to include the dylibs. For example for mylib_dylib including two dylibs:

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint mylib_dylib.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
  s.name             = 'mylib_dylib'
  s.version          = '0.0.1'
  s.summary          = 'A new flutter plugin project.'
  s.description      = <<-DESC
A new flutter plugin project.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => '[email protected]' }
  s.source           = { :path => '.' }
  s.source_files     = 'Classes/**/*'
  s.dependency 'FlutterMacOS'

  s.platform = :osx, '10.11'
  s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
  s.vendored_libraries = 'Frameworks/libmylib_dylib.dylib', 'Frameworks/libmylib_dylib_dependency.dylib'
  s.swift_version = '5.0'
end

Note that the dylibs must be in the macos/Frameworks folder, podspec does not allow them to be in a parent folder of macos.

That way we do not modify any other changes in the example project. (Other than maybe flutter create --platforms=macos . and flutter clean.) So your users on MacOS can just import your library and be done with it.


s.platform = :osx, '10.11'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.swift_version = '5.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing s.vendored_libraries here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oow! Now I got it. My first time using mac's build system. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that and is getting copied into the Framework subdirectory under the app's installation directory. But, it seems like mac is trying to find the dylib from some other path dyld: Library not loaded: @executable_path/libcronet.86.0.4240.198.dylib. I'm yet to figure out how to change it. I tried setting DYLD_LIBRARY_PATH and LD_RUNPATH_SEARCH_PATHS. But, didn't work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try just libcronet.86.0.4240.198.dylib by name instead of with @executable_path/.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to load it without @executable_path. It is getting added by itself. And, this error is happening just before our app's launch. So, I guess podspec is maybe.. dynamically linking our dylibs?
(Even if in that case, I'm unsure why it will fail to locate itself).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My CMakeLists.txt:

add_library(mylib_dylib SHARED mylib_dylib.c)

set_target_properties(mylib_dylib PROPERTIES
    PUBLIC_HEADER mylib_dylib.h
    OUTPUT_NAME "mylib_dylib"
    XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "35FA16FED6F78C492D1D7DE64CB5DDDDC6A0C138"
    # Set the install name of the dylib to the path of where it will be in the app bundle.
    XCODE_ATTRIBUTE_LD_DYLIB_INSTALL_NAME "@executable_path/Frameworks/libmylib_dylib.dylib"
)

And then I open it by name in Dart when running in Flutter apps:

DynamicLibrary.open(dylibFileName(libName));` // libmylib_dylib.dylib

Maybe the combination of that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not building cronet from source files. Can we add it to cmake file?

Copy link
Contributor Author

@unsuitable001 unsuitable001 Sep 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcharkes
It seems like a widespread issue in a few of the latest MacOS/iOS versions. Reading other related issues, I tried to use use_frameworks! and use_modular_headers! in the Podfile. Though some of them got lucky with this hack, I wasn't one of them (along with few others).

I'm experimenting with the issue. Meanwhile, if you get time, can try running the build on your machine maybe..? If the same error happens in your machine too?

And, is there any way to just copy the dylib and not linking it? So that it doesn't get loaded automatically and we can get the scope of loading it from Dart (by experimenting with the path).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not building cronet from source files.

install_name_tool can be used to investigate the "install name" for dylibs which are already compiled.

https://stackoverflow.com/questions/33991581/install-name-tool-to-update-a-executable-to-search-for-dylib-in-mac-os-x

@unsuitable001
Copy link
Contributor Author

Just curious, if #32 can help me resolve this mess :)

@dcharkes
Copy link
Member

Yes, with my experience iOS and MacOS should be almost identical. :)

@Sunbreak
Copy link

Sunbreak commented Feb 5, 2022

Any update?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants