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

[WIP][POC] Starlark implementation of kt_android_* rules #868

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ build --strategy=KotlinCompile=worker
build --test_output=all
build --verbose_failures

# Expose (not really) experimental Android providers need by rules_android-
# AndroidLibraryResourceClassJarProvider, AndroidIdeInfo, ...
build --experimental_google_legacy_api

3 changes: 3 additions & 0 deletions examples/android/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Expose (not really) experimental Android providers need by rules_android-
# AndroidLibraryResourceClassJarProvider, AndroidIdeInfo, ...
build --experimental_google_legacy_api
18 changes: 13 additions & 5 deletions examples/android/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,26 @@ maven_install(
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
use_starlark_android_rules = True,
aar_import_bzl_label = "@io_bazel_rules_android//rules:rules.bzl",
)

# Pulling from personal branch since we need a few fixes in rules_android
# A non complete list can be seen here https://github.com/bazelbuild/rules_android/pulls
http_archive(
name = "build_bazel_rules_android",
name = "io_bazel_rules_android",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason for the name change here? I think that we should keep it consistent so this these repositories aren't downloaded multiple times. And a while back we intentionally switched to build_bazel_rules_android to align with the rest of the community and what rules_android recommends.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no reason I think I just copied the pattern from rules_kotlin but can switch it back to whatever

sha256 = versions.ANDROID.SHA,
strip_prefix = "rules_android-%s" % versions.ANDROID.VERSION,
urls = ["https://github.com/bazelbuild/rules_android/archive/v%s.zip" % versions.ANDROID.VERSION],
urls = ["https://github.com/mauriciogg/rules_android/archive/refs/tags/pre-alpha-01-11-2022-3.zip"],
)

load(
"@build_bazel_rules_android//android:rules.bzl",
"android_sdk_repository",
load("@io_bazel_rules_android//:defs.bzl", "rules_android_workspace")

rules_android_workspace()

register_toolchains(
"@io_bazel_rules_android//toolchains/android:android_default_toolchain",
"@io_bazel_rules_android//toolchains/android_sdk:android_sdk_tools",
)

android_sdk_repository(
Expand Down
17 changes: 16 additions & 1 deletion examples/android/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
load("@io_bazel_rules_android//rules:rules.bzl", "android_binary", "android_application")

# An app that consumes android-kt deps
android_binary(
Expand All @@ -16,6 +16,21 @@ android_binary(
],
)

android_application(
name = "application",
custom_package = "examples.android.app",
bundle_config_file = "bundleConfig.pb.json",
manifest = "src/main/AndroidManifest.xml",
manifest_values = {
"lib_name": "lib",
"applicationId": "examples.android.app",
},
multidex = "native",
deps = [
"//libKtAndroid:my_kt",
],
)

# An app that consumes jvm-kt libs
android_binary(
name = "app2",
Expand Down
46 changes: 46 additions & 0 deletions examples/android/app/bundleConfig.pb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"optimizations": {
"uncompressNativeLibraries": { "enabled": false },
"standaloneConfig": {
"dexMergingStrategy": "NEVER_MERGE"
}
},
"compression": {
"uncompressedGlob": [
"**/*.3g2",
"**/*.3gp",
"**/*.3gpp",
"**/*.3gpp2",
"**/*.aac",
"**/*.amr",
"**/*.awb",
"**/*.gif",
"**/*.imy",
"**/*.jet",
"**/*.jpeg",
"**/*.jpg",
"**/*.m4a",
"**/*.m4v",
"**/*.mid",
"**/*.midi",
"**/*.mkv",
"**/*.mp2",
"**/*.mp3",
"**/*.mp4",
"**/*.mpeg",
"**/*.mpg",
"**/*.ogg",
"**/*.png",
"**/*.rtttl",
"**/*.smf",
"**/*.so.xz",
"**/*.so.zst",
"**/*.wav",
"**/*.webm",
"**/*.wma",
"**/*.wmv",
"**/*.xmf",
"**/*.zst.so"
]
}
}
3 changes: 2 additions & 1 deletion examples/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
package="examples.android.app">
<uses-sdk
android:minSdkVersion="23"
Expand All @@ -21,4 +22,4 @@
</activity>
</application>

</manifest>
</manifest>
2 changes: 1 addition & 1 deletion examples/android/libAndroid/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_library")
load("@io_bazel_rules_android//rules:rules.bzl", "android_library")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

android_library(
Expand Down
18 changes: 11 additions & 7 deletions examples/android/libKtAndroid/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ kt_android_library(
tags = ["trace"],
visibility = ["//visibility:public"],
deps = [
":res2",
"@maven//:androidx_appcompat_appcompat",
"@maven//:com_google_auto_value_auto_value_annotations",
"@maven//:org_jetbrains_kotlinx_kotlinx_serialization_runtime",
],
)

android_library(
name = "res2",
custom_package = "examples.android.lib",
manifest = "src/main/AndroidManifest.xml",
resource_files = glob(["res2/**"]),
)
# Commenting out- rules_android resource processing wil produce
# two resource classes with the same package and upstream targets
# that depend on res and res2 will only see definitions from
# the jar that appears firs in the classpaht resulting in
# field not found exceptions.
#android_library(
# name = "res2",
# custom_package = "examples.android.lib",
# manifest = "src/main/AndroidManifest.xml",
# resource_files = glob(["res2/**"]),
#)
1 change: 1 addition & 0 deletions examples/android/libKtAndroid/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="where_you_at">Where you at?</string>
<string name="hello">hello?</string>
</resources>
3 changes: 0 additions & 3 deletions examples/android/libKtAndroid/res2/values/strings.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_local_test")
kt_android_local_test(
name = "SomeTest",
srcs = ["SomeTest.kt"],
associates = ["//libKtAndroid:my_kt_kt"],
associates = ["//libKtAndroid:my_kt"],
custom_package = "examples.android.lib",
manifest = "AndroidManifest.xml",
deps = [
Expand Down
3 changes: 3 additions & 0 deletions examples/anvil/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Expose (not really) experimental Android providers need by rules_android-
# AndroidLibraryResourceClassJarProvider, AndroidIdeInfo, ...
common --experimental_google_legacy_api
39 changes: 22 additions & 17 deletions examples/anvil/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ register_toolchains("//:kotlin_toolchain")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_android",
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
)

load(
"@build_bazel_rules_android//android:rules.bzl",
"android_sdk_repository",
)

android_sdk_repository(
name = "androidsdk",
build_tools_version = versions.ANDROID.BUILD_TOOLS,
)

# Skylib, for build_test, so don't bother initializing the unit test infrastructure.
http_archive(
name = "bazel_skylib",
Expand Down Expand Up @@ -115,4 +98,26 @@ maven_install(
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
use_starlark_android_rules = True,
aar_import_bzl_label = "@io_bazel_rules_android//rules:rules.bzl",
)

http_archive(
name = "io_bazel_rules_android",
sha256 = versions.ANDROID.SHA,
strip_prefix = "rules_android-%s" % versions.ANDROID.VERSION,
urls = ["https://github.com/mauriciogg/rules_android/archive/refs/tags/pre-alpha-01-11-2022-3.zip"],
)

load("@io_bazel_rules_android//:defs.bzl", "rules_android_workspace")

rules_android_workspace()

register_toolchains(
"@io_bazel_rules_android//toolchains/android:android_default_toolchain",
"@io_bazel_rules_android//toolchains/android_sdk:android_sdk_tools",
)
android_sdk_repository(
name = "androidsdk",
build_tools_version = versions.ANDROID.BUILD_TOOLS,
)
2 changes: 1 addition & 1 deletion examples/anvil/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
load("@io_bazel_rules_android//rules:rules.bzl", "android_binary")

android_binary(
name = "app",
Expand Down
1 change: 1 addition & 0 deletions examples/associates/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common --experimental_google_legacy_api
23 changes: 16 additions & 7 deletions examples/associates/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ kt_register_toolchains()

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_android",
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
)

http_archive(
name = "bazel_skylib",
sha256 = versions.SKYLIB_SHA,
Expand Down Expand Up @@ -53,6 +46,22 @@ maven_install(
],
)

http_archive(
name = "io_bazel_rules_android",
sha256 = versions.ANDROID.SHA,
strip_prefix = "rules_android-%s" % versions.ANDROID.VERSION,
urls = ["https://github.com/mauriciogg/rules_android/archive/refs/tags/pre-alpha-01-11-2022-3.zip"],
)

load("@io_bazel_rules_android//:defs.bzl", "rules_android_workspace")

rules_android_workspace()

register_toolchains(
"@io_bazel_rules_android//toolchains/android:android_default_toolchain",
"@io_bazel_rules_android//toolchains/android_sdk:android_sdk_tools",
)

http_archive(
name = "rules_pkg",
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
Expand Down
5 changes: 5 additions & 0 deletions examples/jetpack_compose/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ build --define=android_dexmerger_tool=d8_dexmerger
build --define=android_incremental_dexing_tool=d8_dexbuilder
build --define=android_standalone_dexing_tool=d8_compat_dx
build --nouse_workers_with_dexbuilder

# Expose (not really) experimental Android providers need by rules_android-
# AndroidLibraryResourceClassJarProvider, AndroidIdeInfo, ...
common --experimental_google_legacy_api

9 changes: 7 additions & 2 deletions examples/jetpack_compose/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ kt_compiler_plugin(
# Used in 'override_targets' by referencing @//:kotlinx_coroutines_core_jvm
kt_jvm_import(
name = "kotlinx_coroutines_core_jvm",
jars = ["@maven_secondary//:v1/https/repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.6.3/kotlinx-coroutines-core-jvm-1.6.3.jar"],
srcjar = "@maven_secondary//:v1/https/repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.6.3/kotlinx-coroutines-core-jvm-1.6.3-sources.jar",
jars = [":kotlinx_coroutines_core_jvm_jar"],
visibility = ["//visibility:public"],
deps = [
"//stub:sun_misc",
"@maven//:org_jetbrains_kotlin_kotlin_stdlib",
],
)

# HACK: for some reason the underlying jar is not visible when using starlark rules
filegroup(
name = "kotlinx_coroutines_core_jvm_jar",
srcs = ["@maven_secondary//:org_jetbrains_kotlinx_kotlinx_coroutines_core_jvm_1_6_3"],
)
18 changes: 15 additions & 3 deletions examples/jetpack_compose/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

_COMPOSE_VERSION = "1.2.1"

Expand Down Expand Up @@ -62,6 +63,8 @@ maven_install(
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
use_starlark_android_rules = True,
aar_import_bzl_label = "@io_bazel_rules_android//rules:rules.bzl",
)

# Secondary maven repository used mainly for workarounds
Expand All @@ -74,6 +77,8 @@ maven_install(
],
fetch_sources = True,
repositories = ["https://repo1.maven.org/maven2"],
use_starlark_android_rules = True,
aar_import_bzl_label = "@io_bazel_rules_android//rules:rules.bzl",
)

http_archive(
Expand All @@ -88,13 +93,20 @@ http_archive(
## Android

http_archive(
name = "build_bazel_rules_android",
name = "io_bazel_rules_android",
sha256 = versions.ANDROID.SHA,
strip_prefix = "rules_android-%s" % versions.ANDROID.VERSION,
urls = ["https://github.com/bazelbuild/rules_android/archive/v%s.zip" % versions.ANDROID.VERSION],
urls = ["https://github.com/mauriciogg/rules_android/archive/refs/tags/pre-alpha-01-11-2022-3.zip"],
)

load("@build_bazel_rules_android//android:rules.bzl", "android_sdk_repository")
load("@io_bazel_rules_android//:defs.bzl", "rules_android_workspace")

rules_android_workspace()

register_toolchains(
"@io_bazel_rules_android//toolchains/android:android_default_toolchain",
"@io_bazel_rules_android//toolchains/android_sdk:android_sdk_tools",
)

android_sdk_repository(
name = "androidsdk",
Expand Down
9 changes: 8 additions & 1 deletion examples/jetpack_compose/app/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="cm.ben.android.bazel.compose.example">

<uses-sdk android:minSdkVersion="24" android:targetSdkVersion="29" />
</manifest>
<application>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
</application>
</manifest>
2 changes: 1 addition & 1 deletion examples/jetpack_compose/app/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
load("@io_bazel_rules_android//rules:rules.bzl", "android_binary")

# An app that consumes android-kt deps
android_binary(
Expand Down
3 changes: 3 additions & 0 deletions examples/plugin/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Expose (not really) experimental Android providers need by rules_android-
# AndroidLibraryResourceClassJarProvider, AndroidIdeInfo, ...
common --experimental_google_legacy_api