From 6dfc09301cd83a6543c5da0162dc59ca91319609 Mon Sep 17 00:00:00 2001 From: Jonathan Gerrish Date: Thu, 12 Nov 2020 09:28:55 -0800 Subject: [PATCH] Make kt_compiler_plugin work with android_binary (#388) --- examples/android/WORKSPACE | 1 + examples/android/lib/BUILD.bazel | 13 ++++++++++++- .../src/main/kotlin/examples/android/lib/Data.kt | 6 ++++++ .../kotlin/examples/android/lib/MainActivity.kt | 2 ++ kotlin/internal/jvm/compile.bzl | 2 +- 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 examples/android/lib/src/main/kotlin/examples/android/lib/Data.kt diff --git a/examples/android/WORKSPACE b/examples/android/WORKSPACE index 01c672342..52ba5e9c2 100644 --- a/examples/android/WORKSPACE +++ b/examples/android/WORKSPACE @@ -19,6 +19,7 @@ maven_install( "junit:junit:4.12", "androidx.test.espresso:espresso-core:3.1.1", "org.hamcrest:hamcrest-library:1.3", + "org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc", ], repositories = [ "https://jcenter.bintray.com/", diff --git a/examples/android/lib/BUILD.bazel b/examples/android/lib/BUILD.bazel index 6f29d8523..8a77c3158 100644 --- a/examples/android/lib/BUILD.bazel +++ b/examples/android/lib/BUILD.bazel @@ -1,4 +1,11 @@ -load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library") +load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_compiler_plugin", "kt_android_library") + +kt_compiler_plugin( + name = "serialization_plugin", + deps = [ + "@com_github_jetbrains_kotlin//:kotlinx-serialization-compiler-plugin", + ], +) kt_android_library( name = "lib", @@ -7,6 +14,10 @@ kt_android_library( manifest = "src/main/AndroidManifest.xml", visibility = ["//visibility:public"], deps = [ + "@maven//:org_jetbrains_kotlinx_kotlinx_serialization_runtime", "@maven//:androidx_appcompat_appcompat", ], + plugins = [ + ":serialization_plugin", + ], ) diff --git a/examples/android/lib/src/main/kotlin/examples/android/lib/Data.kt b/examples/android/lib/src/main/kotlin/examples/android/lib/Data.kt new file mode 100644 index 000000000..88f81eefc --- /dev/null +++ b/examples/android/lib/src/main/kotlin/examples/android/lib/Data.kt @@ -0,0 +1,6 @@ +package examples.android.lib + +import kotlinx.serialization.Serializable + +@Serializable +data class Data(val stringValue: String, val intValue: Int) diff --git a/examples/android/lib/src/main/kotlin/examples/android/lib/MainActivity.kt b/examples/android/lib/src/main/kotlin/examples/android/lib/MainActivity.kt index 00a46d0e8..2611053f9 100644 --- a/examples/android/lib/src/main/kotlin/examples/android/lib/MainActivity.kt +++ b/examples/android/lib/src/main/kotlin/examples/android/lib/MainActivity.kt @@ -18,5 +18,7 @@ class MainActivity : Activity() { .setTitle("Blah") .setMessage("Blah blah blah?") .show() + // Ensure Serialization plugin has run and generated code correctly. + Data.serializer() } } diff --git a/kotlin/internal/jvm/compile.bzl b/kotlin/internal/jvm/compile.bzl index ff1c70578..ade3aae93 100644 --- a/kotlin/internal/jvm/compile.bzl +++ b/kotlin/internal/jvm/compile.bzl @@ -236,7 +236,7 @@ def kt_jvm_compile_action(ctx, rule_kind, output_jar, compile_jar): dirs = _compiler_directories(ctx) srcs = _partitioned_srcs(ctx.files.srcs) friend = _compiler_friends(ctx, friends = getattr(ctx.attr, "friends", [])) - compile_deps = _compiler_deps(toolchains, friend, deps = ctx.attr.deps + ctx.attr.plugins) + compile_deps = _compiler_deps(toolchains, friend, deps = ctx.attr.deps) annotation_processors = _plugin_mappers.targets_to_annotation_processors(ctx.attr.plugins + ctx.attr.deps) transitive_runtime_jars = _plugin_mappers.targets_to_transitive_runtime_jars(ctx.attr.plugins + ctx.attr.deps) plugins = ctx.attr.plugins