Blog Infos
Author
Published
Topics
,
Published
Topics
,
Building Android apps with Bazel build system
Creating a new module
bazel-for-android
├── app
│   
├── my-feature
│   └── src
│       └── io
│           └── morfly
│               └── bfa
│
└── WORKSPACE
package io.morfly.bfa;
public class MyFeature {
public String getInfo() {
return "🦑 My feature";
}
}

MyFeature class contents

 

Next, in the my-feature directory create a file named BUILD. It will contain all the build configurations for the new feature module. Add there the code below:

load("@rules_android//android:rules.bzl", "android_library")
android_library(
name = "my-feature",
srcs = ["src/io/morfly/bfa/MyFeature.java"],
)
view raw BUILD hosted with ❤ by GitHub

my-feature/BUILD file contents

 

bazelisk build //my-feature:my-feature

If all is done correctly, the build should be successfully completed. Below is the file structure of our project so far.

bazel-for-android
├── app
│   
├── my-feature
│   ├── src
│   │   └── io/morfly/bfa
│   │       └── MyFeature.java
│   │
│   └── BUILD
│
└── WORKSPACE
Multiple modules in a single BUILD file
package io.morfly.bfa;
import android.content.Context;
public class MyFeatureWithRes {
public String getInfo(Context context) {
return context.getString(R.string.my_feature_info);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="my_feature_info">🥞 My feature with resources</string>
</resources>
view raw strings.xml hosted with ❤ by GitHub

MyFeatureWithRes class that uses string resources

 

Now we need to configure this module with Bazel. Open my-feature/BUILD file and add there a new my-feature-with-res target as shown below:

load("@rules_android//android:rules.bzl", "android_library")
android_library(
name = "my-feature",
...
)
android_library(
name = "my-feature-with-res",
srcs = ["src/io/morfly/bfa/MyFeatureWithRes.java"],
resource_files = ["res/values/strings.xml"],
manifest = "AndroidManifest.xml",
custom_package = "io.morfly.bfa",
)
view raw BUILD hosted with ❤ by GitHub

Adding another target to the same my-feature/BUILD file

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

Jobs

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.morfly.bfa">
</manifest>

my-feature/AndroidManifest.xml file content

 

bazelisk build //my-feature:my-feature-with-res

If all is done correctly, the build should be successfully completed. Below you can find an updated file structure of the project.

bazel-for-android
├── app
│   
├── my-feature
│   ├── res
│   │   └── values
│   │       └── strings.xml
│   ├── src
│   │   └── io/morfly/bfa
│   │       ├── MyFeature.java
│   │       └── MyFeatureWithRes.java
│   │
│   ├── AndroidManifest.xml
│   └── BUILD
│
└── WORKSPACE
Running the app
load("@rules_android//android:rules.bzl", "android_binary")
android_binary(
name = "bin",
custom_package = "io.morfly.bfa",
manifest = "AndroidManifest.xml",
manifest_values = {
"minSdkVersion": "21",
"targetSdkVersion": "29",
},
srcs = ["src/io/morfly/bfa/MainActivity.java"],
resource_files = glob(["res/**"]),
deps = [
"//my-feature:my-feature",
"//my-feature:my-feature-with-res",
]
)
view raw BUILD hosted with ❤ by GitHub

Updated app/BUILD file contents

 

Now, let’s use our newly created modules in the source code of the app. Open app/src/io/morfly/bfa/MainActivity.java file and update its source code as shown below.

package io.morfly.bfa;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
// Instaitiate features.
MyFeature myFeature = new MyFeature();
MyFeatureWithRes myFeatureWithRes = new MyFeatureWithRes();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
// Display info about included features.
String appInfo = textView.getText()
+ "\n\nIncluded features:\n"
+ myFeature.getInfo()
+ "\n"
+ myFeatureWithRes.getInfo(this);
textView.setText(appInfo);
}
}

Updated MainActivity.java file contents

 

bazelisk mobile-install //app:bin --start_app
ERROR: /.../bazel-for-android/app/BUILD:3:15: in android_binary rule //app:bin: target '//my-feature:my-feature' is not visible from target '//app:bin'. Check the visibility declaration of the former target if you think the dependency is legitimate
ERROR: /.../bazel-for-android/app/BUILD:3:15: in android_binary rule //app:bin: target '//my-feature:my-feature-with-res' is not visible from target '//app:bin'. Check the visibility declaration of the former target if you think the dependency is legitimate
package(default_visibility = ["//visibility:public"])
load("@rules_android//android:rules.bzl", "android_library")
load("@rules_java//java:defs.bzl", "java_library")
package(default_visibility = ["//visibility:public"])
java_library(
name = "my-feature",
srcs = ["src/io/morfly/bfa/MyFeature.java"],
)
android_library(
name = "my-feature-with-res",
srcs = ["src/io/morfly/bfa/MyFeatureWithRes.java"],
resource_files = ["res/values/strings.xml"],
manifest = "AndroidManifest.xml",
custom_package = "io.morfly.bfa",
)
view raw BUILD hosted with ❤ by GitHub

Complete contents of my-feature/BUILD file

 

Now, we are finally ready to build and launch the app:

bazelisk mobile-install //app:bin --start_app
What about JVM-only modules?
load("@rules_java//java:defs.bzl", "java_library")
java_library(
name = "my-feature",
srcs = ["src/io/morfly/bfa/MyFeature.java"],
)
view raw BUILD hosted with ❤ by GitHub

Making my-feature a Java-only library

 

You can find a complete source code of a my-feature/BUILD file below.

load("@rules_android//android:rules.bzl", "android_library")
load("@rules_java//java:defs.bzl", "java_library")
package(default_visibility = ["//visibility:public"])
java_library(
name = "my-feature",
srcs = ["src/io/morfly/bfa/MyFeature.java"],
)
android_library(
name = "my-feature-with-res",
srcs = ["src/io/morfly/bfa/MyFeatureWithRes.java"],
resource_files = ["res/values/strings.xml"],
manifest = "AndroidManifest.xml",
custom_package = "io.morfly.bfa",
)
view raw BUILD hosted with ❤ by GitHub

Complete contents of my-feature/BUILD file

 

bazelisk mobile-install //app:bin --start_app
Conclusion

This article was originally published on proandroiddev.com on February 21, 2021

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu