Skip to content
This repository has been archived by the owner on Jul 27, 2018. It is now read-only.

Unable to add kotlin_library to android_binary w/ kotlin v2 #8

Open
holmes opened this issue Jan 26, 2018 · 5 comments
Open

Unable to add kotlin_library to android_binary w/ kotlin v2 #8

holmes opened this issue Jan 26, 2018 · 5 comments

Comments

@holmes
Copy link

holmes commented Jan 26, 2018

Referenced from bazelbuild/intellij#217

I've got almost all my packages building, but it's failing when I try to put a kotlin_library in an android_binary with the error:

/work/catalog holmes/new-kotlin-rules * ./bazelw mobile-install --start_app //src/test/java/com/squareup/thyme:thyme
ERROR: /work/catalog/src/test/java/com/squareup/thyme/BUILD:53:15: in exports attribute of android_library rule //src/test/java/com/squareup/thyme:java: kotlin_library rule '//src/test/java/com/squareup/thyme:kt' is misplaced here (expected aar_import, android_library, cc_library, java_import, java_library or java_lite_proto_library)
ERROR: Analysis of target '//src/test/java/com/squareup/thyme:thyme' failed; build aborted: Analysis of target '//src/test/java/com/squareup/thyme:java' failed; build aborted
INFO: Elapsed time: 4.541s

Kotlin definition:

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

kotlin_library(
    name = "kt",
    srcs = glob(["MainActivityPresenter.kt"]),
    deps = [
        ":res",
        "@com_android_support_appcompat_v7_27_0_2//aar",
        "@com_android_support_animated_vector_drawable_27_0_2//aar",
        "@com_android_support_support_annotations_27_0_2//jar",
        "@com_android_support_support_vector_drawable_27_0_2//aar",
        "@com_android_support_support_core_utils_27_0_2//aar",
        "@com_android_support_support_core_ui_27_0_2//aar",
        "@com_android_support_support_fragment_27_0_2//aar",
        "@com_android_support_support_compat_27_0_2//aar",
        "@com_android_support_recyclerview_v7_27_0_2//aar",
        "//src/test/java/com/squareup/thyme/utilities:utilities",
        "//src/test/java/com/squareup/thyme/calendar:calendar",
        "@android_arch_lifecycle_common_1_0_3//jar",
        "//third_party:rxbinding",
        "//third_party:rxjava",
        "//src/main/java/com/squareup/shared/serum/event",
        "//src/main/java/com/squareup/shared/serum/store",
    ],
)

Android binary definition:

android_binary(
    name = "thyme",
    srcs = glob(["*.java"]),
    custom_package = PACKAGE,
    manifest = MANIFEST,
    manifest_values = {
        "minSdkVersion": "21",
        "targetSdkVersion": "27",
    },
    multidex = "native",
    deps = [
        ":java",
        ":kt",
        ":res",
        "//src/main/java/com/squareup/shared/appointments/calendar/layout",
        "//src/main/java/com/squareup/shared/appointments/calendar/model",
        "//src/main/java/com/squareup/shared/appointments/calendar/stores",
        "//src/main/java/com/squareup/shared/appointments/sync/model",
        "//src/main/java/com/squareup/shared/serum/event",
        "//src/main/java/com/squareup/shared/serum/store",
        "//third_party:dagger",
        "//third_party:rxandroid",
        "//third_party:rxbinding",
        "//third_party:rxjava",
        "@android_arch_core_runtime_1_0_0//aar",
        "@android_arch_lifecycle_common_1_0_3//jar",
        "@android_arch_lifecycle_runtime_1_0_3//aar",
        "@com_android_support_animated_vector_drawable_27_0_2//aar",
        "@com_android_support_appcompat_v7_27_0_2//aar",
        "@com_android_support_recyclerview_v7_27_0_2//aar",
        "@com_android_support_support_annotations_27_0_2//jar",
        "@com_android_support_support_compat_27_0_2//aar",
        "@com_android_support_support_core_ui_27_0_2//aar",
        "@com_android_support_support_core_utils_27_0_2//aar",
        "@com_android_support_support_fragment_27_0_2//aar",
        "@com_android_support_support_vector_drawable_27_0_2//aar",
    ],
)
@hsyed
Copy link
Owner

hsyed commented Jan 26, 2018

I installed the android SDK and tried out the tutorial. The android rules are very picky about what they import. If you do a java_import of the kotlin_library and then use that as a dep, that should work. There is a potential caveat though, the java_import might generate an ijar, and this will not include inlining metadata.

The android rules need to be updated to accept the Kotlin rules, the Kotlin rules currently only export full_compile_jars -- so it would be as simple as that. The compiler switches might need to be made configurable in case "language-level=1.8" causes problems.

this is what the pattern looks like for importing the jar:

java_import(
    name = "lib",
    jars = ["//tests/smoke:test_embed_resources.jar"]
)

android_binary(
    name = "android",
    custom_package = "com.google.bazel.example.android",
    manifest = "src/main/java/com/google/bazel/example/android/AndroidManifest.xml",
    resource_files = glob(["src/main/java/com/google/bazel/example/android/res/**"]),
    deps = [":activities", ":lib"],
)

@holmes
Copy link
Author

holmes commented Jan 26, 2018

A slight detour now. I'm not sure how I wasn't seeing these problems in the first place, but now I am.

Prior, some of the packages were using kotlin_android_library which would bring in the android jar. Now that I've converted them to your kotlin_library they are failing since the jar is no longer present.

I tried adding "//tools/defaults:android_jar", to the deps section but I'm not getting anywhere.

The build file for reference:

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

kotlin_library(
    name = "utilities_kt",
    srcs = glob(["Butterknife.kt"]),
    deps = [
        "//tools/defaults:android_jar",
        "@com_android_support_animated_vector_drawable_27_0_2//aar",
        "@com_android_support_appcompat_v7_27_0_2//aar",
        "@com_android_support_recyclerview_v7_27_0_2//aar",
        "@com_android_support_support_annotations_27_0_2//jar",
        "@com_android_support_support_compat_27_0_2//aar",
        "@com_android_support_support_core_ui_27_0_2//aar",
        "@com_android_support_support_core_utils_27_0_2//aar",
        "@com_android_support_support_fragment_27_0_2//aar",
        "@com_android_support_support_vector_drawable_27_0_2//aar",
    ],
)

java_import(
    name = "utilities",
    jars = [":utilities_kt.jar"],
    visibility = [
        "//visibility:public",
    ],
)

The output:

/work/catalog holmes/new-kotlin-rules * ./bazelw build  //src/test/java/com/squareup/thyme:thyme_root_java --verbose_failures -s -j 1
INFO: Analysed target //src/test/java/com/squareup/thyme:thyme_root_java (1 packages loaded).
INFO: Found 1 target...
SUBCOMMAND: # //src/test/java/com/squareup/thyme/utilities:utilities_kt [action 'Compiling 1 Kotlin source files to src/test/java/com/squareup/thyme/utilities/utilities_kt.jar']
(cd /private/var/tmp/_bazel_holmes/b92ba0d1ca250871d6293b54dd8861ee/execroot/__main__ && \
  exec env - \
  bazel-out/host/bin/external/io_bazel_rules_kotlin/kotlin/workers/compiler_jvm @bazel-out/darwin-fastbuild/bin/src/test/java/com/squareup/thyme/utilities/utilities_kt-worker.args)
INFO: From Compiling 1 Kotlin source files to src/test/java/com/squareup/thyme/utilities/utilities_kt.jar:
error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class android.support.v7.widget.RecyclerView, unresolved supertypes: android.view.ViewGroup

src/test/java/com/squareup/thyme/utilities/Butterknife.kt:3:16: error: unresolved reference: app
import android.app.Activity
               ^

@hsyed
Copy link
Owner

hsyed commented Jan 29, 2018

@holmes is android.view.ViewGroup definitely included in //tools/defaults:android_jar or any of the other deps ?

These are the macro steps setup by the pubref rules.

@holmes
Copy link
Author

holmes commented Jan 29, 2018

I don't know what android_jar actually points to, but I'm assuming it's the base sdk.

android.view.ViewGroup is at the very core of Android development, so I can only assume it's in //tools/defaults:android_jar.

@aj-michael
Copy link

//tools/defaults:android_jar is a special rule that acts like a java_import on the platforms/android-XX/android.jar from the android_sdk rule that --android_sdk points to. It definitely includes android.view.ViewGroup.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants