diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index b1a8c4fa0..f1600c598 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -79,6 +79,12 @@ tasks: build_targets: - //coroutines-helloworld/... - //express/... + example-jetpack-compose: + name: "Example - Jetpack Compose" + platform: ubuntu1804 + working_directory: examples/jetpack_compose + test_targets: + - //... stardoc: name: Stardoc api documentation platform: ubuntu1804 diff --git a/examples/jetpack_compose/.bazelrc b/examples/jetpack_compose/.bazelrc new file mode 100644 index 000000000..e11a6c594 --- /dev/null +++ b/examples/jetpack_compose/.bazelrc @@ -0,0 +1,7 @@ +# Enable d8 merger +build --define=android_dexmerger_tool=d8_dexmerger + +# Flags for the D8 dexer +build --define=android_incremental_dexing_tool=d8_dexbuilder +build --define=android_standalone_dexing_tool=d8_compat_dx +build --nouse_workers_with_dexbuilder diff --git a/examples/jetpack_compose/BUILD b/examples/jetpack_compose/BUILD index 5ceb5f11d..c17e4a80d 100644 --- a/examples/jetpack_compose/BUILD +++ b/examples/jetpack_compose/BUILD @@ -1,5 +1,5 @@ load("@io_bazel_rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_compiler_plugin", "kt_kotlinc_options") -load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_javac_options") +load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_javac_options", "kt_jvm_import") load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain") # Java Toolchain @@ -41,3 +41,16 @@ kt_compiler_plugin( "@maven//:androidx_compose_compiler_compiler", ], ) + +# Add missing 'sun.misc' files to coroutines artifact +# 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.5.1/kotlinx-coroutines-core-jvm-1.5.1.jar"], + srcjar = "@maven_secondary//:v1/https/repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.5.1/kotlinx-coroutines-core-jvm-1.5.1-sources.jar", + visibility = ["//visibility:public"], + deps = [ + "//stub:sun_misc", + "@maven//:org_jetbrains_kotlin_kotlin_stdlib", + ], +) diff --git a/examples/jetpack_compose/WORKSPACE b/examples/jetpack_compose/WORKSPACE index 344889636..c3e0e475c 100644 --- a/examples/jetpack_compose/WORKSPACE +++ b/examples/jetpack_compose/WORKSPACE @@ -1,3 +1,13 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +_COMPOSE_VERSION = "1.1.0-beta03" + +_KOTLIN_COMPILER_VERSION = "1.5.31" + +_KOTLIN_COMPILER_SHA = "661111286f3e5ac06aaf3a9403d869d9a96a176b62b141814be626a47249fe9e" + +# Setup Kotlin + local_repository( name = "release_archive", path = "../../src/main/starlark/release_archive", @@ -9,29 +19,24 @@ archive_repository( name = "io_bazel_rules_kotlin", ) -load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories") +load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories", "kotlinc_version", "versions") -kotlin_repositories() +kotlin_repositories( + compiler_release = kotlinc_version( + release = _KOTLIN_COMPILER_VERSION, + sha256 = _KOTLIN_COMPILER_SHA, + ), +) register_toolchains("//:kotlin_toolchain") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -_KOTLIN_COMPILER_VERSION = "1.4.21" - ## JVM External -_RULES_JVM_EXTERNAL_VERSION = "4.0" - -_RULES_JVM_EXTERNAL_SHA = "31701ad93dbfe544d597dbe62c9a1fdd76d81d8a9150c2bf1ecf928ecdf97169" - http_archive( name = "rules_jvm_external", - sha256 = _RULES_JVM_EXTERNAL_SHA, - strip_prefix = "rules_jvm_external-{}".format(_RULES_JVM_EXTERNAL_VERSION), - urls = [ - "https://github.com/bazelbuild/rules_jvm_external/archive/{}.zip".format(_RULES_JVM_EXTERNAL_VERSION), - ], + sha256 = versions.RULES_JVM_EXTERNAL_SHA, + strip_prefix = "rules_jvm_external-%s" % versions.RULES_JVM_EXTERNAL_TAG, + url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % versions.RULES_JVM_EXTERNAL_TAG, ) load("@rules_jvm_external//:defs.bzl", "maven_install") @@ -39,108 +44,57 @@ load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( artifacts = [ "org.jetbrains.kotlin:kotlin-stdlib:{}".format(_KOTLIN_COMPILER_VERSION), - "androidx.core:core-ktx:1.3.2", - "androidx.appcompat:appcompat:1.2.0", - "com.google.android.material:material:1.2.1", - "androidx.compose.material:material:1.0.0-alpha09", - "androidx.compose.ui:ui:1.0.0-alpha09", - "androidx.compose.ui:ui-tooling:1.0.0-alpha09", - "androidx.compose.compiler:compiler:1.0.0-alpha09", + "androidx.core:core-ktx:1.6.0", + "androidx.appcompat:appcompat:1.3.0", + "androidx.activity:activity-compose:1.3.0", + "androidx.compose.material:material:{}".format(_COMPOSE_VERSION), + "androidx.compose.ui:ui:{}".format(_COMPOSE_VERSION), + "androidx.compose.ui:ui-tooling:{}".format(_COMPOSE_VERSION), + "androidx.compose.compiler:compiler:{}".format(_COMPOSE_VERSION), + "androidx.compose.runtime:runtime:{}".format(_COMPOSE_VERSION), ], + override_targets = { + "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm": "@//:kotlinx_coroutines_core_jvm", + }, repositories = [ "https://maven.google.com", "https://repo1.maven.org/maven2", ], ) -## Stardoc - -_STARDOC_VERSION = "0.4.0" - -_STARDOC_SHA = "36b8d6c2260068b9ff82faea2f7add164bf3436eac9ba3ec14809f335346d66a" - -http_archive( - name = "io_bazel_stardoc", - sha256 = _STARDOC_SHA, - strip_prefix = "stardoc-{}".format(_STARDOC_VERSION), - urls = [ - "https://github.com/bazelbuild/stardoc/archive/{}.zip".format(_STARDOC_VERSION), +# Secondary maven repository used mainly for workarounds +maven_install( + name = "maven_secondary", + artifacts = [ + # Workaround to add missing 'sun.misc' dependencies to 'kotlinx-coroutines-core-jvm' artifact + # Check root BUILD file and 'override_targets' arg of a primary 'maven_install' + "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.5.2", ], + fetch_sources = True, + repositories = ["https://repo1.maven.org/maven2"], ) -load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories") - -stardoc_repositories() - -## Import Skylib - -_SKYLIB_VERSION = "1.0.2" - -_SKYLIB_SHA = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44" - http_archive( name = "bazel_skylib", - sha256 = _SKYLIB_SHA, - urls = [ - "https://github.com/bazelbuild/bazel-skylib/releases/download/{0}/bazel-skylib-{0}.tar.gz".format(_SKYLIB_VERSION), - ], -) - -load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") - -bazel_skylib_workspace() - -## Protobuf - -_PROTOBUF_VERSION = "3.14.0" - -_PROTOBUF_SHA = "bf0e5070b4b99240183b29df78155eee335885e53a8af8683964579c214ad301" - -http_archive( - name = "com_google_protobuf", - sha256 = _PROTOBUF_SHA, - strip_prefix = "protobuf-{}".format(_PROTOBUF_VERSION), - urls = [ - "https://github.com/protocolbuffers/protobuf/archive/v{}.zip".format(_PROTOBUF_VERSION), - ], -) - -load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") - -protobuf_deps() - -## Rules PKG - -_RULES_PKG_VERSION = "0.2.4" - -_RULES_PKG_SHA = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a" - -http_archive( - name = "rules_pkg", - sha256 = _RULES_PKG_SHA, - urls = [ - "https://github.com/bazelbuild/rules_pkg/releases/download/{0}/rules_pkg-{0}.tar.gz".format(_RULES_PKG_VERSION), - ], + sha256 = versions.SKYLIB_SHA, + urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/%s/bazel-skylib-%s.tar.gz" % ( + versions.SKYLIB_VERSION, + versions.SKYLIB_VERSION, + )], ) ## Android -_RULES_ANDROID_VERSION = "0.1.1" - -_RULES_ANDROID_SHA = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806" - http_archive( - name = "build_bazel_rules_android", - sha256 = _RULES_ANDROID_SHA, - strip_prefix = "rules_android-{}".format(_RULES_ANDROID_VERSION), - urls = [ - "https://github.com/bazelbuild/rules_android/archive/v{}.zip".format(_RULES_ANDROID_VERSION), - ], + name = "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], ) -load("@build_bazel_rules_android//android:rules.bzl", "android_sdk_repository") +load("@rules_android//android:rules.bzl", "android_sdk_repository") android_sdk_repository( name = "androidsdk", - api_level = 29, + build_tools_version = versions.ANDROID.BUILD_TOOLS, # versions > 30.0.3 do not have the dx.jar anymore. ) diff --git a/examples/jetpack_compose/app/AndroidManifest.xml b/examples/jetpack_compose/app/AndroidManifest.xml new file mode 100644 index 000000000..f3eb21691 --- /dev/null +++ b/examples/jetpack_compose/app/AndroidManifest.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/examples/jetpack_compose/compose-app/BUILD b/examples/jetpack_compose/app/BUILD similarity index 57% rename from examples/jetpack_compose/compose-app/BUILD rename to examples/jetpack_compose/app/BUILD index 773a1c1ed..c8f2824f5 100644 --- a/examples/jetpack_compose/compose-app/BUILD +++ b/examples/jetpack_compose/app/BUILD @@ -1,9 +1,11 @@ -load("@build_bazel_rules_android//android:rules.bzl", "android_binary") +load("@rules_android//android:rules.bzl", "android_binary") +load("@bazel_skylib//rules:build_test.bzl", "build_test") # An app that consumes android-kt deps android_binary( name = "compose_example_app", custom_package = "cm.ben.android.bazel.compose.example", + dex_shards = 5, incremental_dexing = 1, manifest = "AndroidManifest.xml", multidex = "native", @@ -12,3 +14,10 @@ android_binary( "//compose-ui:lib", ], ) + +build_test( + name = "force_build_apks_test", + targets = [ + ":compose_example_app", + ], +) diff --git a/examples/jetpack_compose/compose-app/AndroidManifest.xml b/examples/jetpack_compose/compose-app/AndroidManifest.xml deleted file mode 100644 index 8466a912b..000000000 --- a/examples/jetpack_compose/compose-app/AndroidManifest.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/examples/jetpack_compose/compose-ui/AndroidManifest.xml b/examples/jetpack_compose/compose-ui/AndroidManifest.xml index 830c62504..5edbb6300 100644 --- a/examples/jetpack_compose/compose-ui/AndroidManifest.xml +++ b/examples/jetpack_compose/compose-ui/AndroidManifest.xml @@ -1,19 +1,28 @@ + xmlns:tools="http://schemas.android.com/tools" + package="cm.ben.android.bazel.compose.example.ui"> - + + android:label="Bazel Jetpack Compose Example" + android:theme="@style/Theme.AppCompat.Light.NoActionBar"> + android:name=".MainActivity" + android:label="Bazel Jetpack Compose Example"> + + + - + \ No newline at end of file diff --git a/examples/jetpack_compose/compose-ui/BUILD b/examples/jetpack_compose/compose-ui/BUILD index 208587b8a..cc5b55465 100644 --- a/examples/jetpack_compose/compose-ui/BUILD +++ b/examples/jetpack_compose/compose-ui/BUILD @@ -8,12 +8,13 @@ kt_android_library( plugins = ["//:jetpack_compose_compiler_plugin"], visibility = ["//visibility:public"], deps = [ + "@maven//:androidx_activity_activity_compose", "@maven//:androidx_appcompat_appcompat", "@maven//:androidx_compose_material_material", + "@maven//:androidx_compose_runtime_runtime", "@maven//:androidx_compose_ui_ui", "@maven//:androidx_compose_ui_ui_tooling", "@maven//:androidx_core_core_ktx", - "@maven//:com_google_android_material_material", "@maven//:org_jetbrains_kotlin_kotlin_stdlib", ], ) diff --git a/examples/jetpack_compose/compose-ui/MainActivity.kt b/examples/jetpack_compose/compose-ui/MainActivity.kt index c88efffb1..76524bf59 100644 --- a/examples/jetpack_compose/compose-ui/MainActivity.kt +++ b/examples/jetpack_compose/compose-ui/MainActivity.kt @@ -1,19 +1,21 @@ package cm.ben.android.bazel.compose.example.ui import android.os.Bundle +import androidx.activity.compose.setContent import androidx.appcompat.app.AppCompatActivity import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.ui.platform.setContent +import androidx.compose.ui.tooling.preview.Preview class MainActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContent { - Greeting("world") - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContent { + Greeting("world") } + } } +@Preview @Composable fun Greeting(name: String) = Text(text = "Hello $name!") diff --git a/examples/jetpack_compose/stub/BUILD b/examples/jetpack_compose/stub/BUILD new file mode 100644 index 000000000..99d1ea12a --- /dev/null +++ b/examples/jetpack_compose/stub/BUILD @@ -0,0 +1,11 @@ +load("@rules_java//java:defs.bzl", "java_library") + +java_library( + name = "sun_misc", + srcs = [ + "sun/misc/Signal.java", + "sun/misc/SignalHandler.java", + ], + neverlink = True, + visibility = ["//visibility:public"], +) diff --git a/examples/jetpack_compose/stub/sun/misc/Signal.java b/examples/jetpack_compose/stub/sun/misc/Signal.java new file mode 100644 index 000000000..fea83346d --- /dev/null +++ b/examples/jetpack_compose/stub/sun/misc/Signal.java @@ -0,0 +1,4 @@ +package sun.misc; + +public final class Signal { +} \ No newline at end of file diff --git a/examples/jetpack_compose/stub/sun/misc/SignalHandler.java b/examples/jetpack_compose/stub/sun/misc/SignalHandler.java new file mode 100644 index 000000000..169980480 --- /dev/null +++ b/examples/jetpack_compose/stub/sun/misc/SignalHandler.java @@ -0,0 +1,4 @@ +package sun.misc; + +public interface SignalHandler { +} \ No newline at end of file