Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,21 @@ android_library(
...
)
```

## Available Rules

- **`android_binary`** - Builds an Android APK
- **`android_library`** - Builds an Android library
- **`android_application`** - Builds an Android application bundle (AAB)
- **`fat_aar`** - Bundles multiple Android libraries into a single AAR file (see [rules/fat_aar](rules/fat_aar/))
- **`fat_aar_pom`** - Generates a Maven POM from a fat_aar's excluded dependencies

For a complete list of rules, see the [rules.bzl](rules/rules.bzl) file.

## Examples

See the [examples](examples/) directory for sample projects:

- **[basicapp](examples/basicapp/)** - Basic Android application
- **[bundle](examples/bundle/)** - Android application bundle (AAB)
- **[fat_aar](examples/fat_aar/)** - Fat AAR with resources, assets, and native libraries
4 changes: 4 additions & 0 deletions examples/fat_aar/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fat_aar">
</manifest>
44 changes: 44 additions & 0 deletions examples/fat_aar/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
load("//rules/android_library:rule.bzl", "android_library")
load("//rules/fat_aar:rule.bzl", "fat_aar")

# Library with resources and assets
android_library(
name = "lib1",
srcs = ["Lib1.java"],
manifest = "AndroidManifest.xml",
resource_files = glob(["res/**"]),
assets = glob(["assets/**"]),
assets_dir = "assets",
)

# Library depending on lib1 (transitive dependency test)
android_library(
name = "lib2",
srcs = ["Lib2.java"],
manifest = "AndroidManifest.xml",
deps = [":lib1"],
)

# Native library
cc_library(
name = "native_lib",
srcs = ["jni/arm64-v8a/libnative.so"],
)

# Fat AAR bundling all transitive dependencies
fat_aar(
name = "bundled",
min_sdk_version = "21",
deps = [
":lib2",
":native_lib",
],
)

# Fat AAR with exclusions (e.g., exclude external dependencies)
fat_aar(
name = "bundled_filtered",
min_sdk_version = "21",
exclude = ["@maven//"],
deps = [":lib2"],
)
7 changes: 7 additions & 0 deletions examples/fat_aar/Lib1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.fat_aar;

public class Lib1 {
public String getMessage() {
return "Hello from Lib1";
}
}
9 changes: 9 additions & 0 deletions examples/fat_aar/Lib2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.fat_aar;

public class Lib2 {
private Lib1 lib1 = new Lib1();

public String getCombinedMessage() {
return "Lib2 says: " + lib1.getMessage();
}
}
60 changes: 60 additions & 0 deletions examples/fat_aar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Fat AAR Example

This example demonstrates how to use the `fat_aar` rule to bundle multiple Android libraries and their transitive dependencies into a single AAR file.

## Structure

- **lib1**: Android library with resources, layouts, and assets
- **lib2**: Android library that depends on lib1 (demonstrates transitive bundling)
- **native_lib**: Native library (demonstrates native library bundling)

## Targets

### `bundled`
A fat AAR that bundles all transitive dependencies including:
- Java/Kotlin classes from lib1 and lib2
- Resources from all libraries
- Assets from all libraries
- Native libraries
- Merged AndroidManifest.xml

```bash
bazel build //examples/fat_aar:bundled
```

### `bundled_filtered`
A fat AAR with exclusions - demonstrates filtering out external dependencies:
```bash
bazel build //examples/fat_aar:bundled_filtered
```

## What Gets Bundled

The `fat_aar` rule automatically collects and bundles:

1. **Classes (classes.jar)**: All Java/Kotlin bytecode from transitive dependencies
2. **Resources (res/)**: All resources from transitive android_library targets
3. **Assets (assets/)**: All assets from transitive dependencies
4. **Native Libraries (jni/)**: Native .so files for all architectures
5. **AndroidManifest.xml**: Merged manifest from all libraries
6. **R.txt**: Combined R.txt for resource IDs
7. **proguard.txt**: Merged ProGuard rules

## Excluding Dependencies

Use the `exclude` attribute to filter out dependencies (e.g., external Maven dependencies):

```python
fat_aar(
name = "my_aar",
exclude = ["@maven//"], # Exclude all Maven dependencies
deps = [":my_lib"],
)
```

## Output

The fat AAR is a standard Android AAR file that can be:
- Published to Maven/Artifactory
- Consumed by other Android projects (Gradle or Bazel)
- Distributed as a reusable component
1 change: 1 addition & 0 deletions examples/fat_aar/assets/sample_asset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a sample asset file to test asset bundling in fat_aar.
1 change: 1 addition & 0 deletions examples/fat_aar/jni/arm64-v8a/libnative.so
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FAKE_NATIVE_LIB_FOR_TESTING
12 changes: 12 additions & 0 deletions examples/fat_aar/res/layout/lib_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
5 changes: 5 additions & 0 deletions examples/fat_aar/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Fat AAR Example</string>
<string name="hello">Hello from resources</string>
</resources>
1 change: 1 addition & 0 deletions rules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ bzl_library(
],
visibility = [
"//mobile_install:__pkg__",
"//rules/fat_aar:__pkg__",
"//stardoc:__pkg__",
"//test/rules/android_binary/r8_integration:__pkg__",
"//test/rules/android_sdk_repository:__pkg__",
Expand Down
18 changes: 18 additions & 0 deletions rules/fat_aar/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

package(default_visibility = ["//visibility:public"])

exports_files([
"add_native_libs.sh",
"rule.bzl",
"aspect.bzl",
])

bzl_library(
name = "bzl",
srcs = glob(["*.bzl"]),
visibility = ["//visibility:public"],
deps = [
"//rules:bzl",
],
)
Loading