Importing Objective C Framework into Swift File

341 views
Skip to first unread message

856...@gmail.com

unread,
Sep 24, 2018, 2:47:23 PM9/24/18
to bazel-discuss
I am having trouble trying to import an Objective C framework into my swift View Controller. I took the following steps to import the framework into my view controller.

1. First I created a http_archive in my WORKSPACE file pointing at the artifact:
new_http_archive(
name = "MyApi",
url = URL + "MyApi.zip",
build_file = "framework_build.BUILD",
sha256 = "1234567891011121314151617181920",
)

2. Next I created a third party build file for the framework:
objc_framework(
name = "MyApi",
framework_imports = glob(["MyApi.framework/**"]),
is_dynamic = 1,
visibility = ["//visibility:public"],
sdk_dylibs = [
"libc++",
"libz",
"libbz2",
"libiconv",
],
sdk_frameworks = [
"SdkFramework1",
"SdkFramework2",
"SdkFramework3",
],
weak_sdk_frameworks = [
"SdkFramework4",
],
)

3. Then I added an objc_library in the BUILD file for my swift project, which my ios_application rule depends on:
objc_library(
name = "ObjcLib",
copts = [
"-isystem src/main/cc",
"-std=c++11",
],
deps = ["//third_party:MyApi"],
)

This builds the framework with my application. I can verify this by removing the SDK frameworks from my third party build file which causes a compilation error. How can I import this framework in my View Controller? When I put 'import MyApi' in my View Controller, I get the following error message: "error: no such module 'MyApi'".
Could you please advise me on how I can fix this problem.

Oscar Bonilla

unread,
Sep 24, 2018, 4:39:15 PM9/24/18
to 856...@gmail.com, bazel-discuss
You probably have to add 

enable_modules = 1

to your objc_library rule (the one being used by Swift), and maybe run the build with  --experimental_objc_enable_module_maps.



--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/538e3fea-b4b4-49aa-9195-f65634151b07%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

856...@gmail.com

unread,
Sep 24, 2018, 8:27:49 PM9/24/18
to bazel-discuss
I added enable_modules = 1 to my objc_library rule and also ran the build with  --experimental_objc_enable_module_maps, however, I still got the same error: "error: no such module 'MyApi"
I am not exactly sure what my import statement should be. I also tried using the module_name attribute in my objc_library, (I am using bazel version 17.2), so I would know exactly what the name is that I need to import but I get this error: "no such attribute 'module_name' in 'objc_library' rule". How can I use the module_name attribute?

6f6...@gmail.com

unread,
Sep 24, 2018, 9:26:21 PM9/24/18
to 856...@gmail.com, bazel-discuss
I believe module_name is scheduled for Bazel 0.18.1. You might have to use the release-0.18.0 branch if you want to use that attribute.

In the absence of the module_name attribute, Bazel will name the modules with the full path from the root of the workspace. So if you library is in foo/bar/baz, the import will be “import foo_bar_baz” instead of just “import baz”.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/d762784e-a690-43f9-84b8-2405a0414794%40googlegroups.com.

856...@gmail.com

unread,
Sep 26, 2018, 6:22:13 PM9/26/18
to bazel-discuss
I updated to Bazel version 0.18.0 and added the module_name attribute to my objc_library and set it to "MyApi". I then put 'import MyApi' in my View Controller and I still got the same error message: "error: no such module 'MyApi'". Am I using this attribute correctly?
Reply all
Reply to author
Forward
0 new messages