A, the premise

  • Bazel
  • Android Studio
  • Git (optional)

Second, the start

Download the source code

git clone [email protected]:bazelbuild/examples.git bazel-examples
cd bazel-examples/android/tutorial
Copy the code

Use the clone command corresponding to the project source code, clone completion, structure directory is as follows:

[min @ localhost:] $tree. The tutorial ├ ─ ─ the README. Md └ ─ ─ the SRC └ ─ ─ the main ├ ─ ─ AndroidManifest. XML └ ─ ─ Java └ ─ ─ com └ ─ ─ example ├─ ├─ Java ├─ Java ├─ ├─ Java ├─ Java ├─ Java ├─ Java ├─ Java ├─ Java ├─ Java ├─ ├─ ├─ exclude.xml 9 directories, 8 filesCopy the code

Initialize the workspace

A WORKSPACE is a directory containing one or more project source files, with a WORKSPACE file in its root directory.

The WORKSPACE file may be empty, or it may contain references to external dependencies needed to build the project. On macOS or Linux, you can use Touch WORKSPACE to create an empty WORKSPACE file.

After you have created the WORKSPACE file, check that Bazel is ready using the following command:

 bazel info workspace
Copy the code

If Bazel outputs the current directory as a path, the configuration is correct, as shown in the following example:

[min@bogon:] tutorial $ bazel info workspace
/Users/min/Desktop/workspace/android/min/bazel-examples/android/tutorial
Copy the code

Configuration tool chain

Android NDK is also required in some scenarios, so Bazel needs a place where you can configure the Path of the Android SDK and other tool chains. This is one of the functions of the WORKSPACE file mentioned above.

We need to add the following two lines to the WORKSPACE file:

# Configure the Android SDK
android_sdk_repository(name = "androidsdk")

# Configure Android NDK (optional)
android_ndk_repository(name = "androidndk")
Copy the code

ANDROID_NDK_HOME: Bazel automatically reads the ANDROID_HOME and ANDROID_NDK_HOME environment variables, and then automatically completes the configuration. The full version should look like this, using the Android SDK as an example:

android_sdk_repository(
    name = "androidsdk",
    path = "/path/to/Android/sdk",
    api_level = 25,
    build_tools_version = "26.0.1"
)
Copy the code

Bazel only recognizes the environment variables ANDROID_HOME and ANDROID_NDK_HOME, so if your Path is not configured this way, it is recommended to use the full version configuration or change the Env name.

Creating a build file

Build. gradle, cmakelists. TXT, etc. Bazel is no exception. It is a file called build, which can describe the relationship between various Android build intermediates. For example, aAPT compiled resource files, Javac compiled class files, etc., which are written in Starlark language, see Bazel website.

For Android, Bazel provides two basic compilation rules: android_library and android_binary.

  • Android_library: Declare an Android library module;
  • Android_binary: Declare an Android App;

Create android_library BUILD

In the SRC/main/Java/com/example/bazel directory to create a BUILD file, and add the following:

# src/main/java/com/example/bazel/BUILD

package(
    default_visibility = ["//src:__subpackages__"],
)

android_library(
    name = "greeter_activity",
    srcs = [
        "Greeter.java"."MainActivity.java",
    ],
    manifest = "AndroidManifest.xml",
    resource_files = glob(["res/**"]),Copy the code

Create android_binary BUILD

Create a BUILD file in the SRC /main directory and add the following:

# src/main/BUILD

android_binary(
    name = "app",
    manifest = "AndroidManifest.xml",
    deps = ["//src/main/java/com/example/bazel:greeter_activity"],)Copy the code

You can see that it relies on the greeter_activity defined by Android_Library above.

At this point, our configuration is complete.

To build the APP

Execute the following build commands in the current directory:

tutorial $ bazel build //src/main:app
Copy the code

Finally, the result is as follows (target APK path: bazel-bin/ SRC /main/app.apk):

The first execution may take a little longer

Perform installation

For this section, Bazel provides a complete process that you can install using the following command:

bazel mobile-install //src/main:app
Copy the code

Remark: If you don’t have the GMS suite on your phone, you are advised to use adb Install, because in my attempts, Bazel seems to modify the.apk, causing the Activity to go back to loading a com.google.**.Stub* class when it starts.

Third, summary

  • Install related tools, prepare project source code;
  • Initialize the workspace inWORKSPACETo configure related tool chains, such as Android SDK, Android NDK, etc.
  • createBUILDFile and configure the compilation rules for the response, mainly forandroid_libraryandandroid_binary;
  • Start building;

Four, reference

  • Introduction to Bazel: Building an Android App