2017-04-20 172 views
2

我將cc_library鏈接到android_binary並且出現命名問題。有人能告訴我如何解決它嗎?Android二進制文件中的.so文件的名稱衝突

cc_library

cc_library(
    name = "native_library", 
    srcs = glob(["libs/**/*.so"]) 
) 

庫目錄的內容:

libs 
├── armeabi 
│   ├── libSound.so 
│   ├── libSec.so 
│   ├── libWatch.so 
│   └── libTec.so 
├── armeabi-v7a 
│   ├── libSound.so 
│   ├── libSec.so 
│   └── libWatch.so 
├── x86 
│   ├── libSound.so 
│   ├── libSec.so 
│   ├── libWatch.so 
│   └── libTec.so 
|—— other jars 

和錯誤信息是這樣的:

ERROR: /the/path/to/BUILD:10:1: in android_binary rule //:debug_apk: Each library in the transitive closure must have a unique basename to avoid name collisions when packaged into an apk, but two libraries have the basename 'libSound.so': libs/armeabi/libSound.so and libs/armeabi-v7a/libSound.so. 
... 

回答

0

我可能是錯的,但它的侷限性的APK佈局,所以我怕你只是不能有一個脂肪apk的命名庫。重命名庫爲libSound-armeabi.so等你的選擇?

+0

好吧,明白了,謝謝你的迴應! – ldjhust

2

另一種方法,它利用的android_binary是--fat_apk_cpu標誌,並且不需要重新命名庫:

android_binary將打造每cc_library一次通過--fat_apk_cpu指定的每個架構。 --fat_apk_cpu的默認值只是armeabi-v7a。這被稱爲「Android拆分轉換」。當它構建每個cc_library時,cc_library會從--fat_apk_cpu的列表中傳遞一個--cpu標誌。我們可以定義讀取這些標誌的config_setting規則,並在cc_library中使用select語句,以便cc_library包含不同的.so文件,具體取決於其構建的體系結構。

例如:

# BUILD 
CPUS = ["armeabi", "armeabi-v7a", "x86"] 
[config_setting(name = cpu, values = {"cpu": cpu}) for cpu in CPUS] 

cc_library(
    name = "native_library", 
    srcs = select(
     {":%s" % cpu : glob(["libs/%s/*.so" % cpu]) for cpu in CPUS} 
    ), 
) 

android_binary(
    name = "app", 
    srcs = glob(["*.java"]), 
    manifest = "AndroidManifest.xml", 
    deps = [":native_library"], 
) 

然後在命令行中,你可以指定要在最終APK其架構。

$ bazel build --fat_apk_cpu=armeabi,armeabi-v7a,x86 //:app 
$ zipinfo -1 bazel-bin/app.apk | grep \.so$ 
lib/x86/libWatch.so 
lib/x86/libSound.so 
lib/x86/libSec.so 
lib/x86/libTec.so 
lib/armeabi-v7a/libWatch.so 
lib/armeabi-v7a/libSound.so 
lib/armeabi-v7a/libSec.so 
lib/armeabi-v7a/libTec.so 
lib/armeabi/libWatch.so 
lib/armeabi/libSound.so 
lib/armeabi/libSec.so 
lib/armeabi/libTec.so 

$ bazel build --fat_apk_cpu=x86 //:app 
$ zipinfo -1 bazel-bin/app.apk | grep \.so$ 
lib/x86/libWatch.so 
lib/x86/libSound.so 
lib/x86/libSec.so 
lib/x86/libTec.so 

僅指定一個架構搭建可以加快你的開發版本。例如,如果您在開發時使用x86模擬器,則不需要armeabi和armeabi-v7a .so文件。