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 Jetpack Compose example #450

Merged
merged 3 commits into from
Jan 15, 2021
Merged
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
44 changes: 44 additions & 0 deletions examples/jetpack_compose/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "define_kt_toolchain", "kt_compiler_plugin", "kt_javac_options", "kt_kotlinc_options")
load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain")

# Java Toolchain

default_java_toolchain(
name = "java_toolchain",
visibility = ["//visibility:public"],
)

# Kotlin Toolchain

kt_kotlinc_options(
name = "kt_kotlinc_options",
x_allow_jvm_ir_dependencies = True,
x_use_ir = True,
)

kt_javac_options(
name = "kt_javac_options",
)

define_kt_toolchain(
name = "kotlin_toolchain",
api_version = "1.4",
experimental_use_abi_jars = False,
javac_options = "//:kt_javac_options",
jvm_target = "1.8",
kotlinc_options = "//:kt_kotlinc_options",
language_version = "1.4",
)

# Define the compose compiler plugin
# Used by referencing //:jetpack_compose_compiler_plugin

kt_compiler_plugin(
name = "jetpack_compose_compiler_plugin",
id = "androidx.compose.compiler",
target_embedded_compiler = True,
visibility = ["//visibility:public"],
deps = [
"@maven//:androidx_compose_compiler_compiler",
],
)
19 changes: 19 additions & 0 deletions examples/jetpack_compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# bazel_jetpack_compose_example

Forked from https://github.com/Bencodes/bazel_jetpack_compose_example
## Building

`$ bazel build //compose-app:compose_example_app`

```console
❯ bazel build //compose-app:compose_example_app
INFO: Analyzed target //compose-app:compose_example_app (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //compose-app:compose_example_app up-to-date:
bazel-bin/compose-app/compose_example_app_deploy.jar
bazel-bin/compose-app/compose_example_app_unsigned.apk
bazel-bin/compose-app/compose_example_app.apk
INFO: Elapsed time: 0.771s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
```
147 changes: 147 additions & 0 deletions examples/jetpack_compose/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
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),
],
)

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",
],
repositories = [
"https://jcenter.bintray.com/",
"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),
],
)

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),
],
)

## 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),
],
)

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

android_sdk_repository(
name = "androidsdk",
api_level = 29,
)

## Kotlin

local_repository(
name = "io_bazel_rules_kotlin",
path = "../..",
)

load("@io_bazel_rules_kotlin//kotlin:dependencies.bzl", "kt_download_local_dev_dependencies")

kt_download_local_dev_dependencies()

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")

kotlin_repositories()

register_toolchains("//:kotlin_toolchain")
6 changes: 6 additions & 0 deletions examples/jetpack_compose/compose-app/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cm.ben.android.bazel.compose.example">

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
</manifest>
14 changes: 14 additions & 0 deletions examples/jetpack_compose/compose-app/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")

# An app that consumes android-kt deps
android_binary(
name = "compose_example_app",
custom_package = "cm.ben.android.bazel.compose.example",
incremental_dexing = 1,
manifest = "AndroidManifest.xml",
multidex = "native",
visibility = ["//visibility:public"],
deps = [
"//compose-ui:lib",
],
)
19 changes: 19 additions & 0 deletions examples/jetpack_compose/compose-ui/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cm.ben.android.bazel.compose.example.ui">

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />

<application
android:label="Bazel Jetpack Compose Example"
android:theme="@style/Theme.AppCompat.Light">
<activity
android:name=".MainActivity"
android:label="Bazel Jetpack Compose Example">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
19 changes: 19 additions & 0 deletions examples/jetpack_compose/compose-ui/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library")

kt_android_library(
name = "lib",
srcs = ["MainActivity.kt"],
custom_package = "cm.ben.android.bazel.compose.example.ui",
manifest = "AndroidManifest.xml",
plugins = ["//:jetpack_compose_compiler_plugin"],
visibility = ["//visibility:public"],
deps = [
"@maven//:androidx_appcompat_appcompat",
"@maven//:androidx_compose_material_material",
"@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",
],
)
19 changes: 19 additions & 0 deletions examples/jetpack_compose/compose-ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cm.ben.android.bazel.compose.example.ui

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.setContent

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Greeting("world")
}
}
}

@Composable
fun Greeting(name: String) = Text(text = "Hello $name!")