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

mobile-install: App on architecture arm64 with arm64-v8a abi could not find native library #2239

Closed
nxty2011 opened this issue Dec 14, 2016 · 5 comments
Assignees
Labels
P2 We'll consider working on this in future. (Assignee optional) team-Android Issues for Android team type: bug under investigation

Comments

@nxty2011
Copy link

nxty2011 commented Dec 14, 2016

Description of the problem

I want to build an android binary which rely on a native library, and expect it to run on an android 5.1 system whose architecture is arm64, A53 CPU.

So, I build this apk with commands:
bazel mobile-install --fat_apk_cpu=arm64-v8a --compilation_mode=opt <build file location>:<target name> --start_app -s

With -s option, every build command and its flags seem to be reasonable. But after building, the android device run the target apk with failure. From adb tool, I got the error log, which is:

E/AndroidRuntime( 5399): FATAL EXCEPTION: main
E/AndroidRuntime( 5399): Process: , PID: 5399
E/AndroidRuntime( 5399): java.lang.UnsatisfiedLinkError: com.google.devtools.build.android.incrementaldeployment.IncrementalClassLoader$DelegateClassLoader[DexPathList[[dex file "/data/local/tmp/incrementaldeployment//dex/incremental_classes1.dex"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]] couldn't find ".so"

As far as I have known, the built apk file would contains native libs in separate directories which would be named according to the abis used by the native library. So I checked location <apk name>/lib/arm64/ of installed android device for native library I built with bazel. In fact, I found the desired native dynamic library at this position, but I don't know why the intalled apk on android device could not found it.

After I got the error log information, I knew it is because the apk did not find the native dynamic library in the directory where it should be. I manually cp the dynamic library into /system/lib64, which is system library directory, and as the error log indicated. Then the apk could run and work on android device!

So this is quite wired? is it because bazel pack the apk in a way that the android system could not recognize?

Minimal example

I built examples/android/java/bazel:hello_world in bazel source code directory with command:

bazel mobile-install --fat_apk_cpu=arm64-v8a --compilation_mode=opt examples/android/java/bazel:hello_world --start_app -s

It reproduced the same situation as I have encountered. The App failed to run with log information:

E/AndroidRuntime( 5609): FATAL EXCEPTION: main
E/AndroidRuntime( 5609): Process: bazel.android, PID: 5609
E/AndroidRuntime( 5609): java.lang.UnsatisfiedLinkError: com.google.devtools.build.android.incrementaldeployment.IncrementalClassLoader$DelegateClassLoader[DexPathList[[dex file "/data/local/tmp/incrementaldeployment/bazel.android/dex/incremental_classes1.dex"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]] couldn't find "libhello_world.so"

I looked into the apk, and found the native dynamic library at position:

/data/app/bazel.android-1/lib/arm64/libhello_world.so

After I cp libhello_world.so /system/lib64/, I re-run the App, and it worked.

Environment info

  • Operating System:
    Ubuntu 14.04

  • NDK,SDK Info:
    android_sdk_repository(
    name = "androidsdk",
    path = "/usr/local/android/sdk",
    api_level = 22,
    build_tools_version="22.0.1",
    )
    android_ndk_repository(
    name = "androidndk",
    path = "/usr/local/android/ndk",
    api_level = 22,
    )

  • Bazel version (output of bazel info release):
    release 0.4.0

Thank you, any suggestion would be appreciated.

@nxty2011 nxty2011 changed the title Apk on architecture arm64-v8a built by bazel could not find native library. App on architecture arm64-v8a built by bazel could not find native library. Dec 14, 2016
@nxty2011 nxty2011 changed the title App on architecture arm64-v8a built by bazel could not find native library. App on architecture arm64 with arm64-v8a abi could not find native library. Dec 14, 2016
@katre katre added category: rules > android P2 We'll consider working on this in future. (Assignee optional) labels Dec 14, 2016
@aj-michael
Copy link
Contributor

aj-michael commented Dec 14, 2016

Thanks for the detailed report, I'm investigating now. For reference, does this occur with any Bazel app with JNI that you mobile-install, or just one particular app? Oops, totally blanked here, your report includes the hello world sample app. This is certainly a bug.

@aj-michael
Copy link
Contributor

Actually, would you mind trying to run your app via bazel build and adb install? I would be curious to know if the error persists, because it appears this may be a bug in our incremental deployment code that mobile-install uses.

@nxty2011
Copy link
Author

nxty2011 commented Dec 15, 2016

Hello, I understand it's quite a dirty work to deal with so many issues, so I try my best to describe it clear.

I have checked with bazel build and adb install with both my project and hello_world example project, as a result, the error is gone for both projects. So I guess it is the problem of installation process in bazel mobile-install.

Two more things hope to provide more info.

  • I have tried use default parameter for --fat_apk_cpu, which is set to armeabi-v7a I guess from commands bazel processed. Then the app could successfully run on android device following the same process(except without setting the --fat_apk_cpu option).

  • When looking into the generated apk with a archive tool, I found the structure is like:

app.apk/lib/arm64-v8a/native.so

But after I check the app directory on android device via adb shell, I found this:

app/lib/arm64/native.so

Notice the different names of abi directories under lib directory, either bazel mobile-install or bazel build, adb install would produce this. The difference is former method worked, while the latter one threw out lib not found error.

That's about it, thank you.

@aj-michael
Copy link
Contributor

Thanks for all of the details! I suspect the problem is due to a bug in Bazel using special code to handle native libs during mobile-install here:

private String copyNativeLibs(String dataDir) throws IOException {

It is very strange that this works for armeabi-v7a but not arm64-v8a.

@ahumesky ahumesky assigned aj-michael and unassigned ahumesky Jan 10, 2017
@jin jin assigned jin and unassigned aj-michael Apr 10, 2018
@jin jin added team-Android Issues for Android team and removed category: rules > android labels Aug 11, 2018
@jin jin changed the title App on architecture arm64 with arm64-v8a abi could not find native library. mobile-install: App on architecture arm64 with arm64-v8a abi could not find native library Dec 21, 2018
@jin
Copy link
Member

jin commented Dec 21, 2018

Turns out that the native libs were never incrementally installed because --android_incremental_native_libs is off by default.

By switching it on, you can now use --fat_apk_cpu=arm64-v8a correctly, and you'll get the correct error message while trying to install a .so built for armeabi-v7a on an arm64-v8a device:

12-21 14:30:16.272 10596 10596 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.example.android.bazel/incrementallib/libapp.so" is 32-bit instead of 64-bit

I'll send a change to switch it on by default.

bazel-io pushed a commit that referenced this issue Jan 28, 2019
Baseline: deb028e

Cherry picks:

   + a3a5975:
     Fix a race condition in remote cache
   + b8d0e1b:
     Use a new GitHub token and KMS key for the release process.
   + 3759e38:
     remote: fix unexpected IO error (not a directory)
   + 4473bb1:
     Fix a race condition in Bazel's Windows process management.
   + 9137fb9:
     undo flag flip of --incompatible_strict_action_env
   + 12ab12e:
     Revert "Enabling Bazel to generate input symlinks as defined by
     RE AP?
   + 6345c74:
     Automated rollback of commit
     30536ba.

New features:

  - Add inputs filtering for aquery
  - https://docs.bazel.build now supports versioned
    documentation. Use the selector at the top of the navigation bar
    to
    switch between documentation for different Bazel releases.
  - build_tar.py in tools/build_defs/pkg now supports a json manifest
    that can be used to add paths that have symbols that can't be
    specified via the command line

Important changes:

  - Added `--incompatible_dont_emit_static_libgcc`
    (#6825)
    Added `--incompatible_linkopts_in_user_link_flags`
    (#6826)
  - mobile-install now works with aapt2. Try it out with `bazel
    mobile-install --android_aapt=aapt2 //my:target`
  - Fixed a mobile-install v1 bug when deploying to Android 9 Pie
    devices. #6814
  - Add a new option --xbinary_fdo to pass xbinary profile.
  - --runs_per_test: place in TESTING documentation category.
  - Adds a clarifying message to test case summary output when all
    test cases pass but the target fails.
  - Fixed mobile-install v1 error when installing an app with native
    libraries onto an Android 9 (Pie) device. See
    bazelbuild/examples#77
  - Fixed issue where error messages from Android manifest merging
    actions were not propagated fully.
  - Add outputs and mnemonic filtering to aquery
  - New incompatible change flag for defaulting to aapt2 in Android
    builds: `--incompatible_use_aapt2_by_default`. To build with
    aapt2 today, pass the flag
    `--incompatible_use_aapt2_by_default=true` or
    `--android_aapt=aapt2`, or set the `aapt_version`  to `aapt2` on
    your `android_binary` or `android_local_test` target.
  - set projectId in all PublishBuildToolEventStreamRequest
  - Flip flag --incompatible_string_is_not_iterable
    (#5830)
  - cc_toolchain.(static|dynamic)_runtime_libs attributes are now
    optional
  - Added --incompatible_disable_runtimes_filegroups
    (#6942).
  - objc_bundle has been removed. Please migrate to rules_apple's
    [apple_bundle_import](https://github.com/bazelbuild/rules_apple/bl
    ob/master/doc/rules-resources.md#apple_bundle_import).
  - The apple_stub_binary rule has been deleted.
  - Incompatible flag `--incompatible_dont_emit_static_libgcc` has
    been flipped (#6825)
  - Incompatible flag `--incompatible_linkopts_in_user_link_flags`
    has been flipped (#6826)
  - Open source aquery & cquery query2 tests
  - Fixed a mobile-install bug where `arm64-v8a` libraries were not
    deployed correctly on `arm64` devices. This was done by enabling
    incremental native lib deployment by default. A previously
    undocumented `--android_incremental_native_libs` flag is removed,
    and is now the regular behavior. See
    #2239
  - Incompatible flag `--incompatible_linkopts_in_user_link_flags`
    has been flipped (#6826)
  - Incompatible flag `--incompatible_dont_emit_static_libgcc` has
    been flipped (#6825)
  - Added --incompatible_disable_legacy_crosstool_fields. See the
    migration notes at
    #6861.
  - In the Query HowTo, recommend ":*" instead of ":all". "all" might
    be the name of a target.
  - The default value for --incompatible_strict_action_env has been
    flipped to 'false' again, as we discovered breakages for local
    execution users. We'll need some more time to figure out the best
    way to make this work for local and remote execution. Follow
    #7026 for more details.

This release contains contributions from many people at Google, as well as Benjamin Peterson, Dave Lee, George Gensure, Gert van Dijk, Gustavo Storti Salibi, Keith Smiley, Loo Rong Jie, Lukasz Tekieli, Mikhail Mazurskiy, Thi, Travis Cline, Vladimir Chebotarev, Yannic.
weixiao-huang pushed a commit to weixiao-huang/bazel that referenced this issue Jan 31, 2019
Baseline: deb028e

Cherry picks:

   + a3a5975:
     Fix a race condition in remote cache
   + b8d0e1b:
     Use a new GitHub token and KMS key for the release process.
   + 3759e38:
     remote: fix unexpected IO error (not a directory)
   + 4473bb1:
     Fix a race condition in Bazel's Windows process management.
   + 9137fb9:
     undo flag flip of --incompatible_strict_action_env
   + 12ab12e:
     Revert "Enabling Bazel to generate input symlinks as defined by
     RE AP?
   + 6345c74:
     Automated rollback of commit
     30536ba.

New features:

  - Add inputs filtering for aquery
  - https://docs.bazel.build now supports versioned
    documentation. Use the selector at the top of the navigation bar
    to
    switch between documentation for different Bazel releases.
  - build_tar.py in tools/build_defs/pkg now supports a json manifest
    that can be used to add paths that have symbols that can't be
    specified via the command line

Important changes:

  - Added `--incompatible_dont_emit_static_libgcc`
    (bazelbuild#6825)
    Added `--incompatible_linkopts_in_user_link_flags`
    (bazelbuild#6826)
  - mobile-install now works with aapt2. Try it out with `bazel
    mobile-install --android_aapt=aapt2 //my:target`
  - Fixed a mobile-install v1 bug when deploying to Android 9 Pie
    devices. bazelbuild#6814
  - Add a new option --xbinary_fdo to pass xbinary profile.
  - --runs_per_test: place in TESTING documentation category.
  - Adds a clarifying message to test case summary output when all
    test cases pass but the target fails.
  - Fixed mobile-install v1 error when installing an app with native
    libraries onto an Android 9 (Pie) device. See
    bazelbuild/examples#77
  - Fixed issue where error messages from Android manifest merging
    actions were not propagated fully.
  - Add outputs and mnemonic filtering to aquery
  - New incompatible change flag for defaulting to aapt2 in Android
    builds: `--incompatible_use_aapt2_by_default`. To build with
    aapt2 today, pass the flag
    `--incompatible_use_aapt2_by_default=true` or
    `--android_aapt=aapt2`, or set the `aapt_version`  to `aapt2` on
    your `android_binary` or `android_local_test` target.
  - set projectId in all PublishBuildToolEventStreamRequest
  - Flip flag --incompatible_string_is_not_iterable
    (bazelbuild#5830)
  - cc_toolchain.(static|dynamic)_runtime_libs attributes are now
    optional
  - Added --incompatible_disable_runtimes_filegroups
    (bazelbuild#6942).
  - objc_bundle has been removed. Please migrate to rules_apple's
    [apple_bundle_import](https://github.com/bazelbuild/rules_apple/bl
    ob/master/doc/rules-resources.md#apple_bundle_import).
  - The apple_stub_binary rule has been deleted.
  - Incompatible flag `--incompatible_dont_emit_static_libgcc` has
    been flipped (bazelbuild#6825)
  - Incompatible flag `--incompatible_linkopts_in_user_link_flags`
    has been flipped (bazelbuild#6826)
  - Open source aquery & cquery query2 tests
  - Fixed a mobile-install bug where `arm64-v8a` libraries were not
    deployed correctly on `arm64` devices. This was done by enabling
    incremental native lib deployment by default. A previously
    undocumented `--android_incremental_native_libs` flag is removed,
    and is now the regular behavior. See
    bazelbuild#2239
  - Incompatible flag `--incompatible_linkopts_in_user_link_flags`
    has been flipped (bazelbuild#6826)
  - Incompatible flag `--incompatible_dont_emit_static_libgcc` has
    been flipped (bazelbuild#6825)
  - Added --incompatible_disable_legacy_crosstool_fields. See the
    migration notes at
    bazelbuild#6861.
  - In the Query HowTo, recommend ":*" instead of ":all". "all" might
    be the name of a target.
  - The default value for --incompatible_strict_action_env has been
    flipped to 'false' again, as we discovered breakages for local
    execution users. We'll need some more time to figure out the best
    way to make this work for local and remote execution. Follow
    bazelbuild#7026 for more details.

This release contains contributions from many people at Google, as well as Benjamin Peterson, Dave Lee, George Gensure, Gert van Dijk, Gustavo Storti Salibi, Keith Smiley, Loo Rong Jie, Lukasz Tekieli, Mikhail Mazurskiy, Thi, Travis Cline, Vladimir Chebotarev, Yannic.
luca-digrazia pushed a commit to luca-digrazia/DatasetCommitsDiffSearch that referenced this issue Sep 4, 2022
…oyment of all native libs.

    This is the *correct* behavior for incremental deployment. This is already done for the dex and resource APK, now we're enabling it for native libs. Previously, the native libs were bundled directly into the incremental APK.

    The --android_incremental_native_libs flag was never documented, so we can safely remove it.

    Fixes bazelbuild/bazel#2239

    RELNOTES: Fixed a mobile-install bug where `arm64-v8a` libraries were not deployed correctly on `arm64` devices. This was done by enabling incremental native lib deployment by default. A previously undocumented `--android_incremental_native_libs` flag is removed, and is now the regular behavior. See bazelbuild/bazel#2239
    PiperOrigin-RevId: 226529911
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) team-Android Issues for Android team type: bug under investigation
Projects
None yet
Development

No branches or pull requests

5 participants