規則
- android_binary
- aar_import
- android_library
- android_instrumentation_test
- android_local_test
- android_device
- android_ndk_repository
- android_sdk_repository
android_binary
查看規則來源android_binary(name, deps, srcs, assets, assets_dir, compatible_with, crunch_png, custom_package, debug_key, debug_signing_keys, debug_signing_lineage_file, densities, deprecation, dex_shards, dexopts, distribs, enable_data_binding, exec_compatible_with, exec_properties, features, incremental_dexing, instruments, javacopts, key_rotation_min_sdk, licenses, main_dex_list, main_dex_list_opts, main_dex_proguard_specs, manifest, manifest_values, multidex, nocompress_extensions, package_id, plugins, proguard_apply_dictionary, proguard_apply_mapping, proguard_generate_mapping, proguard_specs, resource_configuration_filters, resource_files, restricted_to, shrink_resources, tags, target_compatible_with, testonly, visibility)
產生 Android 應用程式套件檔案 (.apk)。
隱含輸出目標
name.apk
:以偵錯金鑰簽署並完成 zipaligned 的 Android 應用程式套件檔案,可用於開發及偵錯應用程式。使用偵錯金鑰簽署應用程式時,無法發布應用程式。name_unsigned.apk
:上述檔案的未簽署版本,可使用發布金鑰簽署,然後向大眾發布。name_deploy.jar
:包含這個目標的轉換閉包的 Java 封存檔。部署 JAR 包含所有類別,這些類別會由從頭到尾搜尋這個目標的執行階段類別路徑的類別載入器找到。
name_proguard.jar
:Java 封存檔,內含在name_deploy.jar
上執行 ProGuard 的結果。只有在指定 proguard_specs 屬性時,才會產生這項輸出內容。name_proguard.map
:在name_deploy.jar
上執行 ProGuard 後產生的對應檔。只有在指定 proguard_specs 屬性,並設定 proguard_generate_mapping 或 shrink_resources 時,才會產生這項輸出內容。
範例
您可以在 Bazel 原始碼樹的 examples/android
目錄中找到 Android 規則範例。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 android_library 、java_library (含 android 限制) 和 cc_library ,可包裝或產生 Android 目標平台的 .so 原生程式庫。
|
srcs
|
標籤清單;預設值為 已編譯類型為「 系統會解壓縮並編譯 |
assets
|
標籤清單;預設值為 assets 目錄下所有檔案的 glob 。您也可以參照其他規則 (產生檔案的任何規則) 或其他套件中匯出的檔案,前提是所有這些檔案都位於相應套件的 assets_dir 目錄下。
|
assets_dir
|
字串;預設值為 assets 中檔案的路徑。
assets 和 assets_dir 這兩個屬性用來描述封裝資產,且必須同時提供或都不提供。
|
crunch_png
|
布林值;預設值為 |
custom_package
|
字串;預設值為 |
debug_key
|
標籤;預設值為 警告:請勿使用正式版金鑰,這類金鑰應嚴格保管,且不應存放在來源樹狀結構中。 |
debug_signing_keys
|
標籤清單;預設值為 警告:請勿使用正式版金鑰,這類金鑰應嚴格保管,且不應存放在來源樹狀結構中。 |
debug_signing_lineage_file
|
標籤;預設值為 警告:請勿使用正式版金鑰,這類金鑰應嚴格保管,且不應存放在來源樹狀結構中。 |
densities
|
字串清單;預設值為 |
dex_shards
|
整數;預設值為 請注意,每個分片都會在最終應用程式中產生至少一個 DEX。因此,建議不要將此值設為 1 以上,以免發布二進位檔。 |
dexopts
|
字串清單;預設值為 |
enable_data_binding
|
布林值;預設值為 如要使用資料繫結建構 Android 應用程式,您也必須執行下列操作:
|
incremental_dexing
|
整數;無法設定;預設值為 |
instruments
|
標籤;預設值為 要插碼的 如果設定了這項屬性,這個 |
javacopts
|
字串清單;預設值為 這些編譯器選項會在全域編譯器選項之後傳遞至 javac。 |
key_rotation_min_sdk
|
字串;預設值為 |
main_dex_list
|
標籤;預設值為 android/support/multidex/MultiDex$V19.class android/support/multidex/MultiDex.class android/support/multidex/MultiDexApplication.class com/google/common/base/Objects.class multidex="manual_main_dex" 一併使用。
|
main_dex_list_opts
|
字串清單;預設值為 |
main_dex_proguard_specs
|
標籤清單;預設值為 multidex 屬性設為 legacy 時,才能使用這項屬性。
|
manifest
|
標籤 (必填) Android 資訊清單檔案的名稱,通常為AndroidManifest.xml 。
如果定義 resource_files 或 assets,則必須定義。
|
manifest_values
|
字典:字串 -> 字串;預設值為
資訊清單中的所有
系統會忽略
如果將 |
multidex
|
字串;預設值為 可能的值包括:
|
nocompress_extensions
|
字串清單;預設值為 |
package_id
|
整數;預設值為 詳情請參閱 AAPT2 的 |
plugins
|
標籤清單;預設值為 java_plugin 。外掛程式產生的資源會納入目標的結果 JAR 中。
|
proguard_apply_dictionary
|
標籤;預設值為 |
proguard_apply_mapping
|
標籤;預設值為 proguard_generate_mapping 產生的對應檔,可重複使用,將相同對應套用至新版本。
|
proguard_generate_mapping
|
布林值;無法設定;預設值為 proguard_specs 時,系統才會產生對應檔案。這個檔案會列出原始和經過模糊處理的類別、方法和欄位名稱之間的對應關係。
警告:如果使用這項屬性,Proguard 規格不得包含 |
proguard_specs
|
標籤清單;預設值為 |
resource_configuration_filters
|
字串清單;預設值為 en_XA 和/或 ar_XB 偽本地化設定。
|
resource_files
|
標籤清單;預設值為 res 目錄下所有檔案的 glob 。產生的檔案 (來自 genrules) 也可以在此處透過標籤參照。唯一限制是產生的輸出內容必須與任何其他納入的資源檔案位於相同的「 res 」目錄下。
|
shrink_resources
|
整數;預設值為 manifest 和 resource_files 屬性),且需要 ProGuard。運作方式與 Gradle 資源縮減器 (https://developer.android.com/studio/build/shrink-code.html#shrink-resources) 大致相同。重要差異:
name_files/resource_shrinker.log ,詳細說明執行的分析和刪除作業。
可能的值:
|
aar_import
查看規則來源aar_import(name, deps, data, aar, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, exports, features, licenses, restricted_to, srcjar, tags, target_compatible_with, testonly, visibility)
這項規則允許使用 .aar
檔案做為 android_library
和 android_binary
規則的程式庫。
範例
aar_import( name = "google-vr-sdk", aar = "gvr-android-sdk/libraries/sdk-common-1.10.0.aar", ) android_binary( name = "app", manifest = "AndroidManifest.xml", srcs = glob(["**.java"]), deps = [":google-vr-sdk"], )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
aar
|
標籤 (必填) 要提供給依附於這個目標的 Android 目標的.aar 檔案。
|
exports
|
標籤清單;預設值為 |
srcjar
|
標籤;預設值為 |
android_library
查看規則來源android_library(name, deps, srcs, data, assets, assets_dir, compatible_with, custom_package, deprecation, distribs, enable_data_binding, exec_compatible_with, exec_properties, exported_plugins, exports, exports_manifest, features, idl_import_root, idl_parcelables, idl_preprocessed, idl_srcs, javacopts, licenses, manifest, neverlink, plugins, proguard_specs, resource_files, restricted_to, tags, target_compatible_with, testonly, visibility)
這項規則會將來源編譯並封存至 .jar
檔案。
Android 執行階段程式庫 android.jar
會隱含地放在編譯類別路徑上。
隱含輸出目標
libname.jar
:Java 封存檔。libname-src.jar
:包含來源 (「來源 JAR」) 的封存檔。name.aar
:Android「aar」套件,內含這個目標的 Java 封存檔和資源。不含遞移閉包。
範例
您可以在 Bazel 原始碼樹的 examples/android
目錄中找到 Android 規則範例。
以下範例說明如何設定 idl_import_root
。讓 //java/bazel/helloandroid/BUILD
包含:
android_library( name = "parcelable", srcs = ["MyParcelable.java"], # bazel.helloandroid.MyParcelable # MyParcelable.aidl will be used as import for other .aidl # files that depend on it, but will not be compiled. idl_parcelables = ["MyParcelable.aidl"] # bazel.helloandroid.MyParcelable # We don't need to specify idl_import_root since the aidl file # which declares bazel.helloandroid.MyParcelable # is present at java/bazel/helloandroid/MyParcelable.aidl # underneath a java root (java/). ) android_library( name = "foreign_parcelable", srcs = ["src/android/helloandroid/OtherParcelable.java"], # android.helloandroid.OtherParcelable idl_parcelables = [ "src/android/helloandroid/OtherParcelable.aidl" # android.helloandroid.OtherParcelable ], # We need to specify idl_import_root because the aidl file which # declares android.helloandroid.OtherParcelable is not positioned # at android/helloandroid/OtherParcelable.aidl under a normal java root. # Setting idl_import_root to "src" in //java/bazel/helloandroid # adds java/bazel/helloandroid/src to the list of roots # the aidl compiler will search for imported types. idl_import_root = "src", ) # Here, OtherInterface.aidl has an "import android.helloandroid.CallbackInterface;" statement. android_library( name = "foreign_interface", idl_srcs = [ "src/android/helloandroid/OtherInterface.aidl" # android.helloandroid.OtherInterface "src/android/helloandroid/CallbackInterface.aidl" # android.helloandroid.CallbackInterface ], # As above, idl_srcs which are not correctly positioned under a java root # must have idl_import_root set. Otherwise, OtherInterface (or any other # interface in a library which depends on this one) will not be able # to find CallbackInterface when it is imported. idl_import_root = "src", ) # MyParcelable.aidl is imported by MyInterface.aidl, so the generated # MyInterface.java requires MyParcelable.class at compile time. # Depending on :parcelable ensures that aidl compilation of MyInterface.aidl # specifies the correct import roots and can access MyParcelable.aidl, and # makes MyParcelable.class available to Java compilation of MyInterface.java # as usual. android_library( name = "idl", idl_srcs = ["MyInterface.aidl"], deps = [":parcelable"], ) # Here, ServiceParcelable uses and thus depends on ParcelableService, # when it's compiled, but ParcelableService also uses ServiceParcelable, # which creates a circular dependency. # As a result, these files must be compiled together, in the same android_library. android_library( name = "circular_dependencies", srcs = ["ServiceParcelable.java"], idl_srcs = ["ParcelableService.aidl"], idl_parcelables = ["ServiceParcelable.aidl"], )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 android_library 、java_library (含 android 限制) 和 cc_library ,可包裝或產生 Android 目標平台的 .so 原生程式庫。 |
srcs
|
標籤清單;預設值為 .java 或 .srcjar 檔案清單,以建立目標。已編譯類型為「 系統會解壓縮並編譯 如果省略 |
assets
|
標籤清單;預設值為 assets 目錄下所有檔案的 glob 。您也可以參照其他規則 (產生檔案的任何規則) 或其他套件中匯出的檔案,前提是所有這些檔案都位於相應套件的 assets_dir 目錄下。
|
assets_dir
|
字串;預設值為 assets 中檔案的路徑。
assets 和 assets_dir 這兩個屬性用來描述封裝資產,且必須同時提供或都不提供。
|
custom_package
|
字串;預設值為 |
enable_data_binding
|
布林值;預設值為 如要使用資料繫結建構 Android 應用程式,您也必須執行下列操作:
|
exported_plugins
|
標籤清單;預設值為 java_plugin 清單 (例如註解處理器)。
指定的 |
exports
|
標籤清單;預設值為 exports 屬性到達的所有規則的閉包,都會視為直接依附於目標的任何規則的直接依附元件 (目標具有 exports )。
|
exports_manifest
|
整數;預設值為 android_binary 目標。uses-permissions 屬性一律不會匯出。
|
idl_import_root
|
字串;預設值為 處理依附於這個程式庫的 IDL 來源時,系統會將這個路徑做為匯入根目錄。 指定 請參閱 範例。 |
idl_parcelables
|
標籤清單;預設值為 android_library 目標 (直接或透過其遞移閉包),但不會翻譯成 Java 或編譯。這個程式庫中只能包含直接對應至 這些檔案必須放置在適當位置,aidl 編譯器才能找到。 如要瞭解這項設定的意義,請參閱 idl_import_root 的說明。 |
idl_preprocessed
|
標籤清單;預設值為 android_library 目標 (直接或透過其遞移閉包),但不會翻譯成 Java 或編譯。只能加入與這個程式庫中 |
idl_srcs
|
標籤清單;預設值為 srcs 的內容一起編譯。這些檔案會以匯入的形式提供給任何依附於這個程式庫的 這些檔案必須放置在適當位置,aidl 編譯器才能找到。 如要瞭解這項設定的意義,請參閱 idl_import_root 的說明。 |
javacopts
|
字串清單;預設值為 這些編譯器選項會在全域編譯器選項之後傳遞至 javac。 |
manifest
|
標籤;預設值為 AndroidManifest.xml 。
如果定義 resource_files 或 assets,則必須定義。
|
neverlink
|
布林值;預設值為 .apk 建立作業中使用標示為 neverlink 的規則輸出內容。如果程式庫會在執行期間由執行階段環境提供,這個選項就非常實用。 |
plugins
|
標籤清單;預設值為 java_plugin 。外掛程式產生的資源會納入目標的結果 JAR 中。
|
proguard_specs
|
標籤清單;預設值為 android_binary 目標。這裡包含的檔案只能有等冪規則,也就是 -dontnote、-dontwarn、assumenosideeffects,以及以 -keep 開頭的規則。其他選項只能顯示在 android_binary 的 proguard_specs 中,確保合併作業不會出現同義反覆的情況。 |
resource_files
|
標籤清單;預設值為 res 目錄下所有檔案的 glob 。產生的檔案 (來自 genrules) 也可以在此處透過標籤參照。唯一限制是產生的輸出內容必須與任何其他納入的資源檔案位於相同的「 res 」目錄下。
|
android_instrumentation_test
查看規則來源android_instrumentation_test(name, data, args, compatible_with, deprecation, distribs, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, licenses, local, restricted_to, shard_count, size, support_apks, tags, target_compatible_with, target_device, test_app, testonly, timeout, toolchains, visibility)
android_instrumentation_test
規則會執行 Android 檢測設備測試。這會啟動模擬器、安裝受測應用程式、測試應用程式和任何其他必要應用程式,並執行測試套件中定義的測試。
test_app 屬性會指定包含測試的 android_binary
。這個 android_binary
會透過 instruments 屬性,指定受測的 android_binary
應用程式。
範例
# java/com/samples/hello_world/BUILD android_library( name = "hello_world_lib", srcs = ["Lib.java"], manifest = "LibraryManifest.xml", resource_files = glob(["res/**"]), ) # The app under test android_binary( name = "hello_world_app", manifest = "AndroidManifest.xml", deps = [":hello_world_lib"], )
# javatests/com/samples/hello_world/BUILD android_library( name = "hello_world_test_lib", srcs = ["Tests.java"], deps = [ "//java/com/samples/hello_world:hello_world_lib", ... # test dependencies such as Espresso and Mockito ], ) # The test app android_binary( name = "hello_world_test_app", instruments = "//java/com/samples/hello_world:hello_world_app", manifest = "AndroidManifest.xml", deps = [":hello_world_test_lib"], ) android_instrumentation_test( name = "hello_world_uiinstrumentation_tests", target_device = ":some_target_device", test_app = ":hello_world_test_app", )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
support_apks
|
標籤清單;預設值為 |
target_device
|
標籤 (必填) 測試應執行的 android_device。 如要在已執行的模擬器或實體裝置上執行測試,請使用下列引數:
|
test_app
|
標籤 (必填) 包含測試類別的 android_binary 目標。android_binary 目標必須透過 instruments 屬性指定要測試的目標。
|
android_local_test
查看規則來源android_local_test(name, deps, srcs, data, args, compatible_with, custom_package, densities, deprecation, enable_data_binding, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, javacopts, jvm_flags, licenses, local, manifest, manifest_values, nocompress_extensions, plugins, resource_configuration_filters, resource_jars, resource_strip_prefix, restricted_to, runtime_deps, shard_count, size, stamp, tags, target_compatible_with, test_class, testonly, timeout, toolchains, use_launcher, visibility)
這項規則適用於在本機進行單元測試 android_library
規則 (而非在裝置上)。可搭配 Android Robolectric 測試架構使用。
如要進一步瞭解如何編寫 Robolectric 測試,請參閱 Android Robolectric 網站。
隱含輸出目標
name.jar
:測試的 Java 封存檔。name-src.jar
:包含來源的封存檔 (「來源 JAR」)。name_deploy.jar
:適合部署的 Java 部署封存檔 (僅在明確要求時建構)。
範例
如要搭配 android_local_test
使用 Robolectric,請將 Robolectric 的存放區新增至 WORKSPACE
檔案:
http_archive( name = "robolectric", urls = ["https://github.com/robolectric/robolectric-bazel/archive/<COMMIT>.tar.gz"], strip_prefix = "robolectric-bazel-<COMMIT>", sha256 = "<HASH>", ) load("@robolectric//bazel:robolectric.bzl", "robolectric_repositories") robolectric_repositories()
maven_jar
規則。
然後,每個 android_local_test
規則都應依附 @robolectric//bazel:robolectric
。請參閱下方的範例。
android_local_test( name = "SampleTest", srcs = [ "SampleTest.java", ], manifest = "LibManifest.xml", deps = [ ":sample_test_lib", "@robolectric//bazel:android-all", ], ) android_library( name = "sample_test_lib", srcs = [ "Lib.java", ], resource_files = glob(["res/**"]), manifest = "AndroidManifest.xml", )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為
|
srcs
|
標籤清單;預設值為 已編譯類型為「 系統會解壓縮並編譯 只要有至少一個上述檔案類型,系統就會忽略所有其他檔案。否則會引發錯誤。
除非指定 |
custom_package
|
字串;預設值為 test_class 。
|
densities
|
字串清單;預設值為 |
enable_data_binding
|
布林值;預設值為 |
javacopts
|
字串清單;預設值為 這些編譯器選項會在全域編譯器選項之後傳遞至 javac。 |
jvm_flags
|
字串清單;預設值為 Java 二進位檔的包裝函式指令碼包含 CLASSPATH 定義 (可找出所有依附 JAR),並叫用正確的 Java 解譯器。包裝函式指令碼產生的指令列包含主要類別的名稱,後方加上 請注意,這個屬性不會影響 |
manifest
|
標籤;預設值為 AndroidManifest.xml 。
如果定義了 resource_files 或 assets,或受測程式庫的任何資訊清單含有 minSdkVersion 標記,就必須定義這個屬性。
|
manifest_values
|
字典:字串 -> 字串;預設值為 applicationId 、versionCode 、versionName 、minSdkVersion 、targetSdkVersion 和 maxSdkVersion 也會覆寫資訊清單和 uses-sdk 標記的對應屬性。系統會忽略 packageName ,並從 applicationId (如有指定) 或資訊清單中的套件設定 packageName 。您不需要在規則中加入資訊清單,即可使用 manifest_values。
|
nocompress_extensions
|
字串清單;預設值為 |
plugins
|
標籤清單;預設值為 java_plugin 。程式庫也可能從使用 exported_plugins 的依附元件繼承外掛程式。外掛程式產生的資源會納入這項規則產生的 JAR 中。 |
resource_configuration_filters
|
字串清單;預設值為 |
resource_jars
|
標籤清單;預設值為 |
resource_strip_prefix
|
字串;預設值為
如有指定,系統會從 |
runtime_deps
|
標籤清單;預設值為 deps 類似,這些項目會出現在執行階段類別路徑中,但與一般 deps 不同,這些項目不會出現在編譯階段類別路徑中。這裡應列出僅在執行階段需要的依附元件。出現在 runtime_deps 和 deps 中的目標應由依附元件分析工具忽略。
|
stamp
|
整數;預設值為
除非依附元件有所變更,否則系統不會重建蓋章的二進位檔。 |
test_class
|
字串;預設值為
這個屬性會指定要由這項測試執行的 Java 類別名稱。通常不需要設定這項屬性。如果省略這個引數,系統會使用名稱對應於這項 |
use_launcher
|
布林值;預設值為 如果將這項屬性設為 false,系統會忽略這個目標的 launcher 屬性和相關的 |
android_device
查看規則來源android_device(name, cache, compatible_with, default_properties, deprecation, distribs, exec_compatible_with, exec_properties, features, horizontal_resolution, licenses, platform_apks, ram, restricted_to, screen_density, system_image, tags, target_compatible_with, testonly, vertical_resolution, visibility, vm_heap)
這項規則會建立 Android 模擬器,並根據指定規格進行設定。您可以透過 bazel run 指令啟動這個模擬器,也可以直接執行產生的指令碼。建議您依附現有的 android_device 規則,而非自行定義。
這項規則是 --run_under 旗標的合適目標,適用於 bazel test 和 blaze run。這個指令會啟動模擬器、將要測試/執行的目標複製到模擬器,並視情況測試或執行目標。
如果基礎 system_image 是以 X86 為基礎,且最多可針對 I686 CPU 架構進行最佳化,android_device
就支援建立 KVM 映像檔。如要使用 KVM,請在 android_device
規則中加入 tags = ['requires-kvm']
。
隱含輸出目標
name_images/userdata.dat
: 包含啟動模擬器的圖片檔案和快照name_images/emulator-meta-data.pb
:包含必要序列化資訊,可傳遞至模擬器以重新啟動。
範例
以下範例說明如何使用 android_device。
//java/android/helloandroid/BUILD
包含
android_device( name = "nexus_s", cache = 32, default_properties = "nexus_s.properties", horizontal_resolution = 480, ram = 512, screen_density = 233, system_image = ":emulator_images_android_16_x86", vertical_resolution = 800, vm_heap = 32, ) filegroup( name = "emulator_images_android_16_x86", srcs = glob(["androidsdk/system-images/android-16/**"]), )
//java/android/helloandroid/nexus_s.properties
包含:
ro.product.brand=google ro.product.device=crespo ro.product.manufacturer=samsung ro.product.model=Nexus S ro.product.name=soju
這項規則會生成圖片和啟動指令碼。您可以執行 bazel run :nexus_s -- --action=start,在本機啟動模擬器。指令碼會公開下列標記:
- --adb_port:公開 adb 的通訊埠。如要對模擬器發出 adb 指令,請透過這個通訊埠發出 adb connect 指令。
- --emulator_port:用來公開模擬器 Telnet 管理主控台的通訊埠。
- --enable_display:如果為 true,則啟動模擬器時會顯示畫面 (預設為 false)。
- --action:啟動或終止。
- --apks_to_install:要在模擬器上安裝的 APK 清單。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
cache
|
整數;必要 模擬器快取分區的大小 (以 MB 為單位)。 最小值為 16 MB。 |
default_properties
|
標籤;預設值為 |
horizontal_resolution
|
整數;必要 要模擬的水平螢幕解析度 (以像素為單位)。 最小值為 240。 |
platform_apks
|
標籤清單;預設值為 |
ram
|
整數;必要 要為裝置模擬的 RAM 容量 (以 MB 為單位)。 這項設定會套用至整部裝置,而不只是裝置上安裝的特定應用程式。最小值為 64 MB。 |
screen_density
|
整數;必要 模擬螢幕的密度 (以每英吋的像素為單位)。 這個值的最小值為 30 ppi。 |
system_image
|
標籤 (必填) 包含下列檔案的檔案群組:
|
vertical_resolution
|
整數;必要 要模擬的垂直螢幕解析度 (以像素為單位)。 最小值為 240。 |
vm_heap
|
整數;必要 以 MB 為單位設定 Android 要為每個程序使用的虛擬機器堆積大小。 最小值為 16 MB。 |
android_ndk_repository
查看規則來源android_ndk_repository(name, api_level, path, repo_mapping)
設定 Bazel 使用 Android NDK,支援以原生程式碼建構 Android 目標。
請注意,這個 android_ndk_repository
實作項目將由 Starlark 中的實作項目取代。日後將在 Starlark 版 android_ndk_repository
中,實作對 NDK 25 以上版本等未來版本的支援。如要瞭解 Starlark 版本,請參閱 rules_android_ndk。
請注意,如要建構 Android 專用版本,也需要在 WORKSPACE
檔案中加入 android_sdk_repository
規則。
詳情請參閱 使用 Bazel 搭配 Android NDK 的完整說明文件。
範例
android_ndk_repository( name = "androidndk", )
上述範例會從 $ANDROID_NDK_HOME
找出 Android NDK,並偵測其支援的最高 API 級別。
android_ndk_repository( name = "androidndk", path = "./android-ndk-r20", api_level = 24, )
上述範例會使用工作區內 ./android-ndk-r20
的 Android NDK。編譯 JNI 程式碼時,系統會使用 API 級別 24 的程式庫。
cpufeatures
Android NDK 包含 cpufeatures 程式庫,可用於在執行階段偵測裝置的 CPU。以下範例說明如何搭配 Bazel 使用 cpufeatures。
# jni.cc #include "ndk/sources/android/cpufeatures/cpu-features.h" ...
# BUILD cc_library( name = "jni", srcs = ["jni.cc"], deps = ["@androidndk//:cpufeatures"], )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
api_level
|
整數;無法設定;預設值為 |
path
|
字串;不可設定;預設值為 $ANDROID_NDK_HOME 環境變數。
您可以從Android 開發人員網站 下載 Android NDK。 |
repo_mapping
|
字典:字串 -> 字串;預設值為 舉例來說,項目 |
android_sdk_repository
查看規則來源android_sdk_repository(name, api_level, build_tools_version, path, repo_mapping)
設定 Bazel 使用本機 Android SDK,支援建構 Android 目標。
範例
如要為 Bazel 設定 Android SDK,最簡單的方法是在WORKSPACE
檔案中加入名為「androidsdk」的 android_sdk_repository
規則,並將 $ANDROID_HOME
環境變數設為 Android SDK 的路徑。根據預設,Bazel 會使用 Android SDK 中安裝的最高 Android API 級別和建構工具版本。android_sdk_repository( name = "androidsdk", )
為確保建構作業可重現,path
、api_level
和 build_tools_version
屬性可以設為特定值。如果 Android SDK 未安裝指定的 API 級別或建構工具版本,建構作業就會失敗。
android_sdk_repository( name = "androidsdk", path = "./sdk", api_level = 19, build_tools_version = "25.0.0", )
上述範例也示範如何使用與工作區相關的路徑,指向 Android SDK。如果 Android SDK 是 Bazel 工作區的一部分 (例如已簽入版本控管),這就很有用。
支援程式庫
支援程式庫位於 Android SDK 管理工具的「Android Support Repository」中。這是常見 Android 程式庫 (例如 Support 和 AppCompat 程式庫) 的版本化集合,以本機 Maven 存放區的形式封裝。android_sdk_repository
會為每個程式庫產生 Bazel 目標,這些目標可用於 android_binary
和 android_library
目標的依附元件中。
產生的目標名稱衍生自 Android 支援存放區中程式庫的 Maven 座標,格式為 @androidsdk//${group}:${artifact}-${version}
。以下範例說明 android_library
如何依附於 v7 appcompat 程式庫的 25.0.0 版。
android_library( name = "lib", srcs = glob(["*.java"]), manifest = "AndroidManifest.xml", resource_files = glob(["res/**"]), deps = ["@androidsdk//com.android.support:appcompat-v7-25.0.0"], )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
api_level
|
整數;無法設定;預設值為 您可以使用 如要查看 |
build_tools_version
|
字串;不可設定;預設值為 Bazel 需要 30.0.0 以上版本的建構工具。 |
path
|
字串;不可設定;預設值為 $ANDROID_HOME 環境變數。
您可以從 Android 開發人員網站下載 Android SDK。 |
repo_mapping
|
字典:字串 -> 字串;預設值為 舉例來說,項目 |