| <html devsite><head> |
| <title>原生库的命名空间</title> |
| <meta name="project_path" value="/_project.yaml"/> |
| <meta name="book_path" value="/_book.yaml"/> |
| </head> |
| <body> |
| <!-- |
| Copyright 2017 The Android Open Source Project |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <p> |
| Android 7.0 推出了针对原生库的命名空间,旨在限制内部 API 可见性,并解决应用在使用平台库(而非它们自己的库)时意外终止的问题。请参阅<a href="http://android-developers.blogspot.com/2016/06/improving-stability-with-private-cc.html">通过 Android 7.0 中的私有 C/C++ 符号限制提升稳定性</a>这篇 Android 开发者博文,了解针对应用的更改。</p> |
| |
| <h2 id="architecture">架构</h2> |
| |
| <p>这项更改会将系统库与应用库分离开来,这样可以极大地避免意外使用内部系统库(反之亦然)。 |
| </p> |
| |
| <img src="images/namespace-libraries.png" alt="原生库的命名空间" id="namespace-libraries"/> |
| <p class="img-caption"> |
| <strong>图 1.</strong> 原生库的命名空间</p> |
| |
| <p> |
| 原生库的命名空间可防止应用使用私有平台的原生 API(例如使用 OpenSSL)。它还可以避免应用在使用平台库(而非它们自己的库)时出现意外终止的情况(如使用 <code>libpng</code> 时)。 |
| </p> |
| |
| <h2 id="adding-additional-native-libraries">添加其他原生库</h2> |
| |
| <p> |
| 除了标准的公共原生库之外,供应商还可以选择提供可供应用访问的其他原生库,方法是将它们放在 <code>/vendor</code> 库文件夹(如果是 32 位的库,则为 /vendor/lib;如果是 64 位的库,则为 /vendor/lib64)下,并将其列于 <code>/vendor/etc/public.libraries.txt</code> 中 |
| </p> |
| |
| <p> |
| 从 Android 8.0 开始,供应商的公共库需要遵循以下额外限制,并需要进行相应的设置: |
| </p> |
| |
| <ol> |
| <li>供应商的原生库必须添加适当的标签,以供应用访问。如有任何应用(包括第三方应用)要求访问原生库,则该库必须在供应商特定的 <code>file_contexts</code> 文件中标记为 <code>same_process_hal_file</code>,具体如下所示: |
| <pre class="devsite-click-to-copy">/vendor/lib(64)?/libnative.so u:object_r:same_process_hal_file:s0</pre> |
| 其中,<code>libnative.so</code> 为原生库的名称。 |
| </li> |
| <li>库不得依赖(无论是直接依赖,还是通过其依赖关系间接依赖)VNDK-SP 库和 LLNDK 库之外的任何系统库。您可以在 <code>development/vndk/tools/definition/tool/datasets/eligible-list-<version>-release.csv</code> 中找到 VNDK-SP 库和 LLNDK 库的列表。 |
| </li> |
| <ol> |
| |
| <h2 id="updating-app-non-public">将应用更新为不使用非公共原生库</h2> |
| |
| <p> |
| 该功能仅适用于 SDK 版本为 24 或更高版本的应用;要了解向后兼容性,请参阅<a href="http://android-developers.blogspot.com/2016/06/improving-stability-with-private-cc.html">表 1. 当您的应用链接到非公共原生库时会发生什么情况</a>。可供应用访问的 Android 原生库(又称为公共原生库)列表列于 CDD 部分 3.1.1。适用于版本 24 或更高版本且使用任何非公共库的应用应进行更新。如需了解详情,请参阅<a href="https://developer.android.com/preview/behavior-changes.html#ndk">链接到平台库的 NDK 应用</a>。 |
| </p> |
| |
| </ol></ol></body></html> |