Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android NDK: Issue with external dependencies #357

Closed
XperielIndigo opened this issue Aug 4, 2015 · 32 comments
Closed

Android NDK: Issue with external dependencies #357

XperielIndigo opened this issue Aug 4, 2015 · 32 comments
Assignees
Labels
P2 We'll consider working on this in future. (Assignee optional) type: bug

Comments

@XperielIndigo
Copy link

Hi everyone,

I've got an android app that primarily uses the NDK. Almost everything is working. The current issue happens when loading the library (System.loadLibrary) it throws the error: java.lang.UnsatisifedLinkError: dlopen failed: could not load library "libfreetype.so" needed by "libwrapper.so"; caused by library "libfreetype.so" not found.

My android_binary is called wrapper and I've got a dependency in a cc_library to //external:freetype which imports libfreetype.so.

Any thoughts?

Cheers
Indigo

@XperielIndigo
Copy link
Author

My freetype artifact is:

# In artifact BUILD
cc_library(
  name = "freetype-cc",
  srcs = ["android/libfreetype.so"],
  hdrs = glob(["include/**/*.h"]),
  includes = ["include"],
  visibility = ["//visibility:public"],
)

# In my WORKSPACE
http_archive(
  name = "freetype-archive",
  url = "URL/HERE",
  sha256 = "SHA",
)
bind(
  name = "freetype-cc",
  actual = "@freetype-archive//:freetype-cc",
)

Thanks

@kchodorow
Copy link
Contributor

Could you run bazel build --verbose_failures //your:target and paste the output? That should give us the full command line it's trying to run.

@XperielIndigo
Copy link
Author

It doesn't fail at compile, it fails on System.loadLibrary(). Is there some other output I can provide?

@mrdomino
Copy link
Contributor

mrdomino commented Aug 5, 2015

Sounds like a duplicate of issue #335. Workaround here:

#335 (comment)

@XperielIndigo
Copy link
Author

I updated to that hash and even tried giving the --fat_apk_cpu=armeabi-v7a flag. It still fails.

@mrdomino
Copy link
Contributor

mrdomino commented Aug 5, 2015

D'oh, ok, never mind.

@damienmg
Copy link
Contributor

damienmg commented Aug 7, 2015

Closing, please follow-up on #335

@XperielIndigo
Copy link
Author

I don't think this is a duplicate, #335 has the issue of not finding a symbol, I have the issue of not finding the library.

@damienmg damienmg added type: bug P2 We'll consider working on this in future. (Assignee optional) and removed duplicate labels Aug 7, 2015
@damienmg
Copy link
Contributor

damienmg commented Aug 7, 2015

Right sorry, Adding alex for insight on android

@damienmg
Copy link
Contributor

damienmg commented Aug 7, 2015

+Alex Humesky

Alex, can you have a look at this bug?

On Fri, Aug 7, 2015 at 4:14 PM Indigo Orton notifications@github.com
wrote:

I don't think this is a duplicate, #335
#335 has the issue of not finding
a symbol, I have the issue of not finding the library.


Reply to this email directly or view it on GitHub
#357 (comment).

@ahumesky
Copy link
Contributor

ahumesky commented Aug 7, 2015

Could you do unzip -l on the bulit apk to see if it ends up there?

@XperielIndigo
Copy link
Author

Output of unzip -l bazel-myproject/bazel-out/local_darwin-fastbuild/bin/src/main/java/com/mycompany/android/api/wrapper_incremental.apk

     2188  08-07-15 17:49   AndroidManifest.xml
    16028  08-07-15 17:46   classes.dex
       13  08-07-15 17:46   nativedeps
       47  08-07-15 17:46   stub_application_data.txt
 15420488  08-07-15 17:49   lib/armeabi-v7a/libwrapper.so
      411  08-07-15 17:49   META-INF/MANIFEST.MF
      464  08-07-15 17:49   META-INF/CERT.SF
     1203  08-07-15 17:49   META-INF/CERT.RSA

That is the only apk that was discovered from a find bazel-myproject/ -name "*.apk"

@ahumesky
Copy link
Contributor

ahumesky commented Aug 8, 2015

Looks like you're using bazel mobile-install, which is fine, but could you try "bazel build" of the same target? That should give you a regular apk, at bazel-bin/src/main/java/com/yourcompany/android/api/wrapper.apk That might not fix it, but it at least would eliminate mobile-install as a culprit.

And to make sure I understand your setup, I see that the apk has "libwrapper.so". Is that from a rule in a different package as the android_binary? libwrapper.so would be the output of a cc_library named "wrapper", and your android binary is already named "wrapper", so they couldn't be from the same package. Also you mentioned "//external:freetype", but did you mean "//external:freetype-cc"?

So you have an android_binary named "wrapper" that depends on a cc_library named "wrapper" in a different package that depends on "//external:freetype-cc", which is bound to "@freetype-archive//:freetype-cc", which is the cc_library rule named "freetype-cc" in the BUILD file in the zip file from the http_archive rule, right?

@XperielIndigo
Copy link
Author

I can't seem to do a bazel build because I need the configuration of a mobile-install for some select dependency statements.

As for my setup I have:

android_binary(
  name = "wrapper",
  srcs = ["MainActivity.java"],
  manifest = "AndroidManifest.xml",
  legacy_native_support = 0,
  deps = [
    ":api",
    ":jni",
  ],
)

android_library(
  name = "api",
  srcs = glob(["*.java"], exclude = ["MainActivity.java"]),
)

cc_library(
  name = "jni",
  srcs = glob(["*.cc"]),
  hdrs = glob(["*.h"]),
  deps = [
    "//src/main/cc/com/mycompany/api",
  ],
  linkopts = ["-lGLESv2", "-lEGL", "-llog"],
)

and the external http_archive above. I've tried naming my external archive library freetype and freetype-cc to test whether it was something to do with that name (it doesn't appear so).

The only rule named wrapper is the android_binary

@ahumesky
Copy link
Contributor

ahumesky commented Aug 8, 2015

I see you have legacy_native_support = 0, which means that all of the native libraries will be linked together into one .so:
https://github.com/google/bazel/blob/49543429f56cfe9c90562a1e9f9c66a9eabb5385/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java#L682
which explains why there is no libfreetype.so in the apk.

Try doing System.loadLibrary for just libwrapper.so

And to get bazel build to work, you might just need the right set of command line flags which get implied by mobile-install

@XperielIndigo
Copy link
Author

I'm only calling loadlibrary on libwrapper. The error is it wanting
libfreetype.

On Friday, August 7, 2015, ahumesky notifications@github.com wrote:

I see you have legacy_native_support = 0, which means that all of the
native libraries will be linked together into one .so:

https://github.com/google/bazel/blob/49543429f56cfe9c90562a1e9f9c66a9eabb5385/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java#L682
which explains why there is no libfreetype.so in the apk.

Try doing System.loadLibrary for just libwrapper.so

And to get bazel build to work, you might just need the right set of
command line flags which get implied by mobile-install


Reply to this email directly or view it on GitHub
#357 (comment).

@XperielIndigo
Copy link
Author

Could the problem be legacy_native_support only combines all bazel generated .so files, without including source .so files, or without killing linking of source .so files (i.e. cc_library bar has source libfoo.so which means bar must be linked to foo in the final binary, but when legacy_native_support is added the requirement of linking libfoo.so is not killed, and perhaps the contents of libfoo.so is not added).

@ahumesky
Copy link
Contributor

ahumesky commented Aug 8, 2015

You could try setting legacy_native_support to 1. Or do you need it set to 0 for something?

My guess is that the .so being in srcs and/or it being behind a bind / http_archive is causing some problem. To test this, you could temporarily unzip the archive into your workspace and then depend on it as a regular part of your code. If that makes things work, then bind / http_archive is probably causing the problem. If that's still broken, then there's probably a bug with legacy_native_support.

@XperielIndigo
Copy link
Author

I need legacy_native_support as I have conflicting library names. I'll try unzipping the archive.

@XperielIndigo
Copy link
Author

Ok, I've tried unzipping the archive and depending on the inner rule and the same issue occurs. I believe it is using a .so as a source file. The rule is "freetype-cc" so the generated lib is "libfreetype-cc.so", the underlying lib is "libfreetype.so" and that is the one missing. I'm trying to get bazel build to work now.

@XperielIndigo
Copy link
Author

When I compile with --cpu=armeabi-v7a it starts compiling and crashes with execvpe /bin/false failed --- No such file or directory on the first source file compilation.

@ahumesky
Copy link
Contributor

I spent some time changing the bazel android example to a similar setup that you have, and I was able to reproduce the problem, and I couldn't get it to work with legacy_native_support set to 1 or 0, with and without the http_archive/bind, so it looks like the android rules just don't know how to properly handle a .so file being in the sources of a cc_library. While we figure that out, is it possible for you to compile the freetype library from source? You should still be able to put the source behind an http_archive rule.

Do you have /bin/false on your system? If you do then that sounds like another problem worth filing a bug about.

@XperielIndigo
Copy link
Author

Well I'm pleased it isn't something crazy about my setup. I have a number of these types of artifacts which can't easily be compiled from source (many different compile flags/setups that would take some time to reproduce). I'll look into the /bin/false issue a bit more and I'm taking a look at the android linking problem in source now.

@XperielIndigo
Copy link
Author

Any update on the progress? This is starting to get quite urgent for my project.

@ahumesky
Copy link
Contributor

I've forwarded the issue to the person who wrote a lot of our android + cc support, but unfortunately I don't have an ETA for a fix

@XperielIndigo
Copy link
Author

Do you have any idea where we might fix the problem yet? This is getting very tight for me now.

@programmablereya
Copy link

After some digging, I think I may have a fix for this. The key is that in modern native support mode, pre-compiled .so files are not added to the APK, even though they are used as input to the linker. Thus they're not available at runtime. Simply filtering these out and adding these to the APK should fix this; I'll write some tests first thing tomorrow and should be able to get this in soon. There's a chance I've misunderstood how things work and it will be more of a pain, but we'll find out soon enough!

@XperielIndigo
Copy link
Author

Great, thanks Michael. I look forward to seeing it.

@XperielIndigo
Copy link
Author

Any update Michael?

@programmablereya
Copy link

I've confirmed that my fix works; I should have it in Monday or Tuesday!

@XperielIndigo
Copy link
Author

Hi Michael,

That may have fixed it, but now I'm getting an undefined symbol error at runtime for std::allocator<char>::allocator() (as mangled symbol _ZNSaIcEC1Ev). I would think this is just another error in my program, but it now occurs even when I'm not linking my primary library (i.e. just a basic c++ file that was compiling previously).

Any thoughts?

@ulfjack
Copy link
Contributor

ulfjack commented Sep 2, 2015

See #392 for the STL issue.

bazel-io pushed a commit that referenced this issue Aug 12, 2021
  - 3c7ec07fe0418446ffdb1a04c671a3810d74ae30 [cas] Add cas package (#300) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 28fa42989a6c2d05cddb6b42494f34ef742c3de9 [cas] Implement file reading (#302) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 69c6642c3a006636256c1d2e591899f07d4c74bf Simplify caching packages (#303) by nodirg <56001730+nodirg@users.noreply.github.com>
  - f7087af662fe5481f2e75abd652af46d9a376534 [cas] Implement presence check (#304) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 847bca232f884b1e7ee059c64317a5140f11477e Simplify caching packages further (#306) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 989513ef4a567a812f21b27f2a5d83f1a2145600 Rename singleflightcache.Cache (#310) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 7eceb37537dec7c50e52d4f1af1a5d671c267144 [cas] Fix the build (#308) by nodirg <56001730+nodirg@users.noreply.github.com>
  - fd877b05ba2ed611f6d34e711da05917b729eff9 [cas] Add retries (#311) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 5bc303584ef03ded33f03fa2976015e38da9c050 [cas] Add support for Symlinks. (#309) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 4cfed65947cba54af1b2259d9e3facf7dd3007b9 [cas] Implement batch upload (#307) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 144126c43e73aeeabdbeb8bc4ef0290fa7a91ba7 [cas] Move file IO semaphore to Client (#313) by nodirg <56001730+nodirg@users.noreply.github.com>
  - e1da041171a715b7bab3178acfe1cd774b9f5019 [cas] Reuse file read buffers (#317) by nodirg <56001730+nodirg@users.noreply.github.com>
  - e9184e44947661852a8887f3183e00c346208aaa [cas] Add UploadOptions (#314) by nodirg <56001730+nodirg@users.noreply.github.com>
  - e2bd6c8e2d6bc183ff059c688a604ba1ac18b840 [cas] Implement ServerCapabilities check (#315) by nodirg <56001730+nodirg@users.noreply.github.com>
  - b00d91e726265f23ad0a88730545831b8d5519ef [cas] Add RPCConfig (#318) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 1c678dec65b62e49840419ab777c7b6ce65cfd76 [cas] Improve error messages (#321) by nodirg <56001730+nodirg@users.noreply.github.com>
  - ad8d2cfffe1f3728469a8dd5a7531b9157280ecd [cas] Limit FindMissingBlobs concurrency (#319) by nodirg <56001730+nodirg@users.noreply.github.com>
  - b1b54ee4d55b5d5bd71ca2df4353058515581697 [cas] Increase FindMissingBlobs concurrency to 256 (#323) by nodirg <56001730+nodirg@users.noreply.github.com>
  - b4a0e12d87c946ced360d723d743be1f57a47995 [cas] Move file IO buffering deeper (#322) by nodirg <56001730+nodirg@users.noreply.github.com>
  - abb14633e09633368f06d8e4fd28bd1553509061 [cas] Add filtering/callback (#316) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 7447b28dd69e22848ee850936a1cdd28e2d0e20b Add Mtime to the file metadata cache. (#326) by ramymedhat <abdelaal@google.com>
  - 7182b476eb6260fae9ba2bd8995b97a7096e340c [cas] Implement streaming (#320) by nodirg <56001730+nodirg@users.noreply.github.com>
  - b0605647bbe2ff7d046a286c3023e7714376fb83 include file path in upload error (#327) by Takuto Ikuta <tikuta@google.com>
  - 3b602dd48f7f63a76cb1087a10355b3c668d583f [cas] Implement per-request timeouts in a stream (#325) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 395c674af7a9cd696dfd1f2b4a950f6899ccb3a0 remove unused variables (#332) by Takuto Ikuta <tikuta@google.com>
  - 45f49a9529f755fb586fe2f8bf3e78b1eae39e81 [cas] Unembed cas.Client.ClientConfig. (#333) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 2a9b29928abe867026e37833fa480ed16238df7a [cas] Rename UploadOptions.Callback to Prelude (#334) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 80ea864b211ee3d87e14f4be0ca8d17e5917062c [cas] Read files once (#335) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 0e577525a2dce2d0e7bddd80927c8901d28bf0fb [cas] PathExclude: use forward-slash-separated paths (#341) by nodirg <56001730+nodirg@users.noreply.github.com>
  - e155d015bcc4c9eb9978572422e3404f26220700 Add useful error message for uploading files. (#339) by bansalvinayak <vinayakbansal@google.com>
  - 3dfb518d390280a2ffef5a85ab1852a2a526a983 [cas] Rename UploadInput to PathSpec (#342) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 752e4efb2631b45f5c27120f83d517cc2b2846d2 google/uuid -> pborman/uuid (#344) by Rubens Farias <rubensf@google.com>
  - d94f8a8ba888d384686a7cd74d8a9d0795ba4b6d Small tweaks to appease internal import checks. (#346) by Rubens Farias <rubensf@google.com>
  - f9d52cdef1c3aa8612d9c3ed7a0b65f96e55d870 Catch another pool check (#347) by Rubens Farias <rubensf@google.com>
  - ead1458eda2b7c756138429121a3e22bd5c9aa5a Preserving symlink or not can be configured from Command.... by Yoshisato Yanagisawa <yoshisato.yanagisawa@gmail.com>
  - f831c118b9c9e1dd3e857fbe43da7c992c91b20f make (*Chunker).Reset returns error (#348) by Takuto Ikuta <tikuta@google.com>
  - 1a7d2a4198fa0eb8515593b5259f495cdacf75ab [cas] Require PathSpec.Path to be absolute (#345) by nodirg <56001730+nodirg@users.noreply.github.com>
  - dd2d3976ed7c6482f361de7d24e24d5a8683c56c Upgrade zstdpool-syncpool for DecoderWrapper.Close bugfix... by Mostyn Bramley-Moore <mostyn@antipode.se>
  - 8544bdc0f3112900675a72decdb7a978cece0be7 [cas] Clean PathSpec.Path (#352) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 882e3342509eb038dc04bf44581e5085c0320d16 [cas] Add UploadResult.Digest() (#340) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 5d4d813411299a285f113cb541f2c5750746cdfa chunker: remove unused field from Chunker (#355) by Takuto Ikuta <tikuta@google.com>
  - a5af2d4316599a3fea87a459290e5f88a022a43c Add return value names in singleflightcache (#356) by nodirg <56001730+nodirg@users.noreply.github.com>
  - e7ea26b93b496d4d30e98390682faf6f69f84cb7 [cas] Fix joinFilePathsFast (#361) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 5a8daf747858747b2bf6bdd59e7b07dedc17a244 [cas] Refactor code (#359) by nodirg <56001730+nodirg@users.noreply.github.com>
  - dd6c290b2ce791f3a3e56583a9632f7b538e8a05 [cas] Simplify UploadResult.Digest() signature (#362) by nodirg <56001730+nodirg@users.noreply.github.com>
  - 05222e7e8939959878a5798e51cf52e911feafaf Fix lint (#357) by nodirg <56001730+nodirg@users.noreply.github.com>
  - b2689fabc306d2cd20356f30b7f99eec445d9212 [cas] Refactor Digest() (#363) by nodirg <56001730+nodirg@users.noreply.github.com>
  - c672e5baca9280d181c7e2e87b3d9afd01650893 [cas] Add PathSpec.Allowlist (#360) by nodirg <56001730+nodirg@users.noreply.github.com>
  - f9e6595d5634ac4d4221ba6dca9f5c2e64114b3f add size check (#365) by Takuto Ikuta <tikuta@google.com>
  - 3d0cf1be08dd52d77204ad1be7e70fdd1e48c206 Revert "Add useful error message for uploading files. (#3... by Rubens Farias <rubensf@google.com>
  - 3ddc89f3e2b39308101060b3eeaa10a919cae13e "Wrap" gRPC error codes. (#367) by Rubens Farias <rubensf@google.com>
  - d965bf95d0af9d88d90e9723aee6b332dc3ef93e cas: fix deadlock (#368) by Takuto Ikuta <tikuta@google.com>
  - 3c4ce9170b6c5a5d64bcc4feecc5bcdf5dd1f101 allow to use streamBufSize larger than 32KiB (#369) by Takuto Ikuta <tikuta@google.com>
  - 21d6adc44e550f7aa5c4e9b3fedab838920a9632 use semaphore for large file upload (#370) by Takuto Ikuta <tikuta@google.com>
  - 1cec173a5bf76c02f435050d6fc9a02e1ccea637 update remote-apis (#371) by Takuto Ikuta <tikuta@google.com>
  - e96eb06339fb616167ee02535ab54d9c3a382232 add more log around upload (#372) by Takuto Ikuta <tikuta@google.com>
  - 3f34e744d83161ddcb602121b45b0b33185a36c5 Make glog import consistent. (#373) by Rubens Farias <rubensf@google.com>
  - 3db822c86088434a4d2d53ec2d3b889b9f8cf331 Remove typos (#374) by Rubens Farias <rubensf@google.com>

PiperOrigin-RevId: 390319167
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 We'll consider working on this in future. (Assignee optional) type: bug
Projects
None yet
Development

No branches or pull requests

7 participants