Mercurial
comparison gara/android/firebase-cloud-messaging/README.md @ 47:829623189a57
[Gara] Android commit. Bazelfied it.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sat, 13 Dec 2025 14:20:34 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 46:b9a40c633c93 | 47:829623189a57 |
|---|---|
| 1 | |
| 2 # Bazel Firebase Cloud Messaging (FCM) example | |
| 3 | |
| 4 FCM requires certain information about your app (API key, app ID, project id, | |
| 5 etc) to be present in the `res/values/values.xml` resource file. This example | |
| 6 shows how to use the tools provided in the | |
| 7 [bazelbuild/tools_android](https://github.com/bazelbuild/tools_android) repo to | |
| 8 generate the `values.xml` file from the `google-services.json` file from your | |
| 9 Firebase console. | |
| 10 | |
| 11 ## Building the Example | |
| 12 | |
| 13 To build the example: | |
| 14 | |
| 15 1. Make sure the `ANDROID_HOME` environment variable is set to the absolute path | |
| 16 of your Android SDK. | |
| 17 | |
| 18 2. Go to the Firebase console for your project, and in Settings, download | |
| 19 `google-service.json`, and replace the sample file in the `app` directory. | |
| 20 | |
| 21 3. Run `bazel build //app` in the project. | |
| 22 | |
| 23 ## Applying the Example to Your Code | |
| 24 | |
| 25 To apply this example to your code: | |
| 26 | |
| 27 1. Add the following to your `WORKSPACE` file: | |
| 28 ```python | |
| 29 TOOLS_ANDROID_VERSION = "0.1" | |
| 30 http_archive( | |
| 31 name = "tools_android", | |
| 32 strip_prefix = "tools_android-" + TOOLS_ANDROID_VERSION, | |
| 33 url = "https://github.com/bazelbuild/tools_android/archive/%s.tar.gz" % TOOLS_ANDROID_VERSION, | |
| 34 ) | |
| 35 load("@tools_android//tools/googleservices:defs.bzl", "google_services_workspace_dependencies") | |
| 36 google_services_workspace_dependencies() | |
| 37 ``` | |
| 38 | |
| 39 2. Add the following to your `BUILD` file: | |
| 40 ```python | |
| 41 load("@tools_android//tools/googleservices:defs.bzl", "google_services_xml") | |
| 42 | |
| 43 GOOGLE_SERVICES_XML = google_services_xml( | |
| 44 package_name = "com.example.myapplication", | |
| 45 google_services_json = "google-services.json" | |
| 46 ) | |
| 47 ``` | |
| 48 | |
| 49 3. Add `GOOGLE_SERVICES_XML` to the `resource_files` attribute of your | |
| 50 `android_binary` rule. For example: | |
| 51 ```python | |
| 52 android_binary( | |
| 53 ... | |
| 54 resource_files = glob(["src/main/res/**"]) + GOOGLE_SERVICES_XML, | |
| 55 ... | |
| 56 ) | |
| 57 ``` | |
| 58 | |
| 59 4. Bazel's `AndroidManifest.xml` merging logic does not merge permissions from | |
| 60 dependent libraries (see issue [#5411](https://github.com/bazelbuild/bazel/issues/5411)). | |
| 61 You may need to add the following permissions to the `AndroidManifest.xml` of | |
| 62 your top-level `android_binary` rule: | |
| 63 ```xml | |
| 64 <uses-permission android:name="android.permission.INTERNET" /> | |
| 65 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
| 66 <uses-permission android:name="android.permission.WAKE_LOCK" /> | |
| 67 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> | |
| 68 ``` | |
| 69 | |
| 70 ## Manual Integration | |
| 71 | |
| 72 It's also possible to run the Google Services values.xml generator manually and | |
| 73 add the results to your project: | |
| 74 | |
| 75 1. Go to the Firebase console for your project, and in Settings, download | |
| 76 `google-service.json`. | |
| 77 | |
| 78 2. From the workspace root of the `tools_android` project, run the Google | |
| 79 Services XML generator: | |
| 80 ``` | |
| 81 bazel run //third_party/googleservices:GenerateGoogleServicesXml -- \ | |
| 82 com.example.myapplication \ | |
| 83 /absolute/path/to/google-services.json \ | |
| 84 /tmp/values.xml | |
| 85 ``` | |
| 86 The arguments are the package name for your app, the absolute file path to | |
| 87 the `google-services.json` file, and finally the file path for `values.xml`. | |
| 88 | |
| 89 3. Merge the resulting `values.xml` file into your `values.xml` file (or put the | |
| 90 file into your `res/values` directory if you don't already have a | |
| 91 `values.xml` file). Alternatively, the `values.xml` file can be put into a | |
| 92 separate `res/values` directory and added to the `resource_files`. For the | |
| 93 example here, if `values.xml` is in | |
| 94 `app/src/main/google_services_xml/res/values/values.xml`, the `BUILD` file | |
| 95 would have | |
| 96 `resource_files = glob(["src/main/res/**"]) + ["src/main/google_services_xml/res/values/values.xml"],`. |