Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic bzlmod example #148

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ bazel_binaries = use_extension(
)
bazel_binaries.download(version = "last_green")
use_repo(bazel_binaries, "bazel_binaries")

android_sdk_extension = use_extension("//rules/android_sdk_repository:rule.bzl", "android_sdk_extension")
use_repo(android_sdk_extension, "androidsdk")

register_toolchains("@androidsdk//:all")
5 changes: 0 additions & 5 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load(":android_sdk_supplemental_repository.bzl", "android_sdk_supplemental_repository")

maybe(
android_sdk_repository,
name = "androidsdk",
)

maybe(
android_ndk_repository,
name = "androidndk",
Expand Down
9 changes: 9 additions & 0 deletions examples/basicappbzlmod/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
common --enable_bzlmod --lockfile_mode=off

# Disabled to avoid //external:android deps in bazel itself
common --android_sdk=@androidsdk//:sdk
common --optimizing_dexer=//:optimizing_dexer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a workaround to avoid having this target in bazel reference //external:android


# Flags needed while the Android rules are being migrated to Starlark.
build --experimental_google_legacy_api
build --experimental_enable_android_migration_apis
1 change: 1 addition & 0 deletions examples/basicappbzlmod/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rolling
6 changes: 6 additions & 0 deletions examples/basicappbzlmod/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Empty build file to satisfy gazelle for rules_go.

cc_binary(
name = "optimizing_dexer",
srcs = ["main.cc"],
)
13 changes: 13 additions & 0 deletions examples/basicappbzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module(name = "basic-example")

bazel_dep(name = "rules_java", version = "6.4.0")
bazel_dep(name = "rules_android", version = "1")
local_path_override(
module_name = "rules_android",
path = "../../",
)

android_sdk_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_extension")
use_repo(android_sdk_extension, "androidsdk")

register_toolchains("@androidsdk//:all")
7 changes: 7 additions & 0 deletions examples/basicappbzlmod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To build, ensure the `ANDROID_HOME` and `ANDROID_NDK_HOME` environment variables are set, and run:

```
bazel build java/com/basicapp:basic_app
```

See the `.bazelrc` file for flags needed to build the app.
1 change: 1 addition & 0 deletions examples/basicappbzlmod/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 12 additions & 0 deletions examples/basicappbzlmod/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_android//:android_sdk_supplemental_repository.bzl", "android_sdk_supplemental_repository")

maybe(
android_ndk_repository,
name = "androidndk",
)

# This can be removed once https://github.com/bazelbuild/bazel/commit/773b50f979b8f40e73cf547049bb8e1114fb670a
# is released, or android_sdk_repository is properly Starlarkified and dexdump
# added there.
android_sdk_supplemental_repository(name = "androidsdk-supplemental")
22 changes: 22 additions & 0 deletions examples/basicappbzlmod/java/com/basicapp/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.basic" >

<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="30" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.basicapp.BasicActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
14 changes: 14 additions & 0 deletions examples/basicappbzlmod/java/com/basicapp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@rules_android//android:rules.bzl", "android_binary", "android_library")

android_binary(
name = "basic_app",
manifest = "AndroidManifest.xml",
deps = [":basic_lib"],
)

android_library(
name = "basic_lib",
srcs = ["BasicActivity.java"],
manifest = "AndroidManifest.xml",
resource_files = glob(["res/**"]),
)
59 changes: 59 additions & 0 deletions examples/basicappbzlmod/java/com/basicapp/BasicActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2022 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.basicapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
* The main activity of the Basic Sample App.
*/
public class BasicActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_activity);

final Button buttons[] = {
findViewById(R.id.button_id_fizz), findViewById(R.id.button_id_buzz),
};

for (Button b : buttons) {
b.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
TextView tv = findViewById(R.id.text_hello);
if (v.getId() == R.id.button_id_fizz) {
tv.setText("fizz");
} else if (v.getId() == R.id.button_id_buzz) {
tv.setText("buzz");
}
}
});
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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_hello"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button_id_fizz"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="fizz" />
<Button
android:id="@+id/button_id_buzz"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="buzz" />

</LinearLayout>
8 changes: 8 additions & 0 deletions examples/basicappbzlmod/java/com/basicapp/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.basicapp.BasicActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100" />
</menu>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name" translatable="false">basicapp</string>
<string name="hello_world" translatable="false">Hello world!</string>
<string name="action_settings" translatable="false">Settings</string>

</resources>
3 changes: 3 additions & 0 deletions examples/basicappbzlmod/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
return 0;
}
2 changes: 1 addition & 1 deletion mobile_install/tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ TOOL_ATTRS = dict(
executable = True,
),
_d8 = attr.label(
default = Label("@bazel_tools//src/tools/android/java/com/google/devtools/build/android/r8:r8"),
default = Label("//tools/android:r8"),
allow_files = True,
cfg = "exec",
executable = True,
Expand Down
38 changes: 38 additions & 0 deletions rules/android_sdk_repository/helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,42 @@ def _create_config_setting_rule():
flag_values = {":allow_proguard": "false"},
)

def create_dummy_sdk_toolchain():
native.toolchain(
name = "sdk-dummy-toolchain",
toolchain = ":sdk-dummy",
toolchain_type = "@bazel_tools//tools/android:sdk_toolchain_type",
)

native.filegroup(name = "jar-filegroup", srcs = ["dummy.jar"])

native.genrule(
name = "genrule",
srcs = [],
outs = ["empty.sh"],
cmd = "echo '' >> \"$@\"",
executable = 1,
)

native.sh_binary(name = "empty-binary", srcs = [":genrule"])

native.android_sdk(
name = "sdk-dummy",
aapt = ":empty-binary",
adb = ":empty-binary",
aidl = ":empty-binary",
android_jar = ":jar-filegroup",
apksigner = ":empty-binary",
dx = ":empty-binary",
framework_aidl = "dummy.jar",
main_dex_classes = "dummy.jar",
main_dex_list_creator = ":empty-binary",
proguard = ":empty-binary",
shrinked_android_jar = "dummy.jar",
tags = ["__ANDROID_RULES_MIGRATION__"],
zipalign = ":empty-binary",
)

def create_android_sdk_rules(
name,
build_tools_version,
Expand Down Expand Up @@ -235,6 +271,8 @@ def create_android_sdk_rules(
],
)

create_dummy_sdk_toolchain()

native.alias(
name = "org_apache_http_legacy",
actual = ":org_apache_http_legacy-%d" % default_api_level,
Expand Down
32 changes: 32 additions & 0 deletions rules/android_sdk_repository/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,35 @@ def android_sdk_repository(
_bind(name, "android_sdk_for_testing", ":files")
_bind(name, "has_android_sdk", ":has_android_sdk")
native.register_toolchains("@%s//:all" % name)

def _android_sdk_extension_impl(module_ctx):
root_modules = [m for m in module_ctx.modules if m.is_root and m.tags.configure]
if len(root_modules) > 1:
fail("Expected at most one root module, found {}".format(", ".join([x.name for x in root_modules])))

if root_modules:
module = root_modules[0]
else:
module = module_ctx.modules[0]

kwargs = {}
if module.tags.configure:
kwargs["api_level"] = module.tags.configure[0].api_level
kwargs["build_tools_version"] = module.tags.configure[0].build_tools_version
kwargs["path"] = module.tags.configure[0].path

_android_sdk_repository(
name = "androidsdk",
**kwargs
)

android_sdk_extension = module_extension(
implementation = _android_sdk_extension_impl,
tag_classes = {
"configure": tag_class(attrs = {
"path": attr.string(),
"api_level": attr.int(),
"build_tools_version": attr.string(),
}),
},
)