bazel-contrib / rules_jvm_external

Bazel rules to resolve, fetch and export Maven artifacts
Apache License 2.0
328 stars 249 forks source link

Annotation processor not found #92

Open matthewcheok opened 5 years ago

matthewcheok commented 5 years ago

I added a java_plugin to enable support for annotation processors but it doesn't seem to work:

java_plugin(
    name = "epoxy_plugin",
    processor_class = "com.airbnb.epoxy.EpoxyProcessor",
    deps = [
        "@maven//:com_airbnb_android_epoxy",
        "@maven//:com_airbnb_android_epoxy_annotations",
    ],
)

java_library(
    name = "epoxy_lib",
    exported_plugins = ["epoxy_plugin"],
)

I'm getting the following error:

warning: [kapt] Can't find annotation processor class com.airbnb.epoxy.EpoxyProcessor: com.airbnb.epoxy.EpoxyProcessor
src/main/java/com/matthewcheok/bazeltest/MainActivity.kt:19:17: error: unresolved reference: itemCustomView
                itemCustomView {
                ^
src/main/java/com/matthewcheok/bazeltest/MainActivity.kt:20:21: error: unresolved reference: id
                    id("custom view $i")
                    ^
src/main/java/com/matthewcheok/bazeltest/MainActivity.kt:21:21: error: expression 'title' of type 'CharSequence!' cannot be invoked as a function. The function 'invoke()' is not found
                    title("this is a green custom view item")
                    ^

The unresolved references are likely because we're not getting the code generation expected.

The sample repo is at https://github.com/matthewcheok/bazel-epoxy-annotation-processor

jin commented 5 years ago

~Have you tried adding generates_api = 1 to the java_plugin target?~

Nevermind, just tried it, it doesn't work.

jin commented 5 years ago

I got slightly further by adding com.airbnb:epoxy-processor:3.3.1 to maven_install and @maven//:com_airbnb_epoxy_processor to the deps of the java_plugin target. The BUILD file looks like this:

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

java_plugin(
    name = "epoxy_plugin",
    processor_class = "com.airbnb.epoxy.EpoxyProcessor",
    generates_api = 1,
    deps = [
        "@maven//:com_airbnb_android_epoxy_processor",
    ],
)

java_library(
    name = "epoxy_lib",
    exported_plugins = ["epoxy_plugin"],
    exports = [
        "@maven//:com_airbnb_android_epoxy",
        "@maven//:com_airbnb_android_epoxy_processor",
        "@maven//:com_airbnb_android_epoxy_annotations",
    ]
)

android_library(
    name = "resources",
    manifest = "AndroidManifest.xml",
    custom_package = "com.matthewcheok.bazeltest",
    resource_files = glob(
        ["res/**"], 
        exclude=["res/.DS_Store"]
    ),
    deps = [
        "@maven//:androidx_appcompat_appcompat",
        "@maven//:androidx_constraintlayout_constraintlayout",
    ],
)

kt_android_library(
    name = "lib",
    srcs = glob(["java/**/*.kt"]),
    custom_package = "com.matthewcheok.bazeltest",
    plugins = [":epoxy_plugin"],
    deps = [
        ":resources",
        ":epoxy_lib",
        "@maven//:androidx_constraintlayout_constraintlayout",
        "@maven//:androidx_annotation_annotation",
        "@maven//:androidx_appcompat_appcompat",
        "@maven//:androidx_core_core",
        "@maven//:androidx_core_core_ktx",
        "@maven//:androidx_drawerlayout_drawerlayout",
        "@maven//:androidx_lifecycle_lifecycle_common",
        "@maven//:androidx_lifecycle_lifecycle_viewmodel",
        "@maven//:androidx_fragment_fragment",
        "@maven//:androidx_recyclerview_recyclerview", 
    ],
    visibility = ["//visibility:public"],
)

I'm now running into an SDK error with javapoet:

ERROR: /private/var/tmp/_bazel_jin/54321455f30132f9d0deb1ed5ef71a3e/external/maven/BUILD:798:1: Dexing external/maven/_dx/com_squareup_javapoet_1_11_1/javapoet-1.11.1.jar_desugared.jar with applicable dexopts [] failed (Exit 1)
java.util.concurrent.ExecutionException: com.android.dx.cf.code.SimException: ERROR in com.squareup.javapoet.CodeBlock.joining:(Ljava/lang/String;)Ljava/util/stream/Collector;: invoking a static interface method java.util.stream.Collector.of:(Ljava/util/function/Supplier;Ljava/util/function/BiConsumer;Ljava/util/function/BinaryOperator;Ljava/util/function/Function;[Ljava/util/stream/Collector$Characteristics;)Ljava/util/stream/Collector; strictly requires --min-sdk-version >= 24 (blocked at current API level 13)
    at java.base/java.util.concurrent.FutureTask.report(Unknown Source)
    at java.base/java.util.concurrent.FutureTask.get(Unknown Source)
    at com.google.devtools.build.android.dexer.DexBuilder.produceDexArchive(DexBuilder.java:253)
    at com.google.devtools.build.android.dexer.DexBuilder.processRequest(DexBuilder.java:218)
    at com.google.devtools.build.android.dexer.DexBuilder.runPersistentWorker(DexBuilder.java:173)
    at com.google.devtools.build.android.dexer.DexBuilder.main(DexBuilder.java:121)
Caused by: com.android.dx.cf.code.SimException: ERROR in com.squareup.javapoet.CodeBlock.joining:(Ljava/lang/String;)Ljava/util/stream/Collector;: invoking a static interface method java.util.stream.Collector.of:(Ljava/util/function/Supplier;Ljava/util/function/BiConsumer;Ljava/util/function/BinaryOperator;Ljava/util/function/Function;[Ljava/util/stream/Collector$Characteristics;)Ljava/util/stream/Collector; strictly requires --min-sdk-version >= 24 (blocked at current API level 13)
    at com.android.dx.cf.code.Simulator.fail(Simulator.java:947)
    at com.android.dx.cf.code.Simulator.checkInvokeInterfaceSupported(Simulator.java:917)
    at com.android.dx.cf.code.Simulator.access$500(Simulator.java:43)
    at com.android.dx.cf.code.Simulator$SimVisitor.visitConstant(Simulator.java:687)
    at com.android.dx.cf.code.BytecodeArray.parseInstruction(BytecodeArray.java:764)
    at com.android.dx.cf.code.Simulator.simulate(Simulator.java:117)
    at com.android.dx.cf.code.Ropper.processBlock(Ropper.java:789)
    at com.android.dx.cf.code.Ropper.doit(Ropper.java:744)
    at com.android.dx.cf.code.Ropper.convert(Ropper.java:349)
    at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:309)
    at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:150)
    at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:102)
    at com.google.devtools.build.android.dexer.Dexing.addToDexFile(Dexing.java:198)
    at com.google.devtools.build.android.dexer.DexConverter.toDexFile(DexConverter.java:31)
    at com.google.devtools.build.android.dexer.DexConversionEnqueuer$ClassToDex.call(DexConversionEnqueuer.java:173)
    at com.google.devtools.build.android.dexer.DexConversionEnqueuer$ClassToDex.call(DexConversionEnqueuer.java:156)
    at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)
Target //:bazel_test failed to build
matthewcheok commented 5 years ago

It looks like we need to specify a min SDK version. But I'm not sure where we might do that.

jin commented 5 years ago

Usually dex options go into android_binary's dexopts attribute. However, this seems like a bug because javapoet isn't supposed to go into the APK, let alone getting dexed and desugared. https://github.com/JakeWharton/butterknife/issues/1234#issuecomment-377429674

I think we'll need a way to specify neverlink on specific dependencies in the tree to avoid bringing artifacts like javapoet into the runtime classpath.

jin commented 5 years ago

Just an update: the dexopt issue will be fixed by https://github.com/bazelbuild/rules_jvm_external/pull/97 by marking the javapoet artifact as neverlink = True. I was able to reproduce the epoxy failures, but I believe this is due to an issue in the kt_android_library rule. I tried to use the Room annotation processor as well, and it worked on a regular android_library with Java sources, but not kt_android_library.

jin commented 5 years ago

97 is in. With v2.0.1, you can mark the javapoet as neverlink, which should prevent the min-sdk issue. However, the issue with Epoxy's annotation processor not running still stands.

jin commented 5 years ago

I don't have bandwidth to work on this at the moment, may pick it up when the new Kotlin rules are released.

fisheye-123 commented 4 years ago

Any update? I have same issue that bazel + Kotlin + epoxy not working

clewis-turner commented 4 years ago

We have been able to build our application with Bazel + Kotlin + epoxy using the following configuration:

WORKSPACE: primary maven_install:

"com.airbnb.android:epoxy:3.9.0",
"com.airbnb.android:epoxy-processor:3.9.0",
"com.airbnb.android:epoxy-annotations:3.9.0",
"com.squareup.kotlinpoet.1.5.0",
"com.squareup.javapoet.1.13.0",

secondary maven_install:

maven_install(
    name = "jetify",
    artifacts = [
        "com.google.android.material:material:1.1.0",
        ...
    ],
    jetify = True,
    excluded_artifacts = [
        "com.android.support:*",
    ],
    repositories = [
        ...
    ],
)

BUILD:

java_library(
    name = "epoxy_compiler",
    exported_plugins = [
        ":epoxy_plugin",
    ],
    visibility = [
        "//visibility:public",
    ],
)

java_plugin(
    name = "epoxy_plugin",
    generates_api = 1,
    processor_class = "com.airbnb.epoxy.EpoxyProcessor",
    deps = [
        "@maven//:com_airbnb_android_epoxy",
        "@maven//:com_airbnb_android_epoxy_processor",
        "@maven//:com_airbnb_android_epoxy_annotations",
    ],
)

kt_android_library (
  ...
  deps = [
     ...
    ":epoxy_compiler",
    "@maven//:com_airbnb_android_epoxy",
    "@maven//:com_airbnb_android_epoxy_processor",
    "@maven//:com_squareup_javapoet",
    "@maven//:com_squareup_kotlinpoet",
    "@jetify//:com_google_android_material_material",

   ]
) 

We shouldn't need that visibility reference. As you can see, we also do not seem to need to include neverlink for javapoet and kotlinpoet. We also needed to exclude the Google material library from our primary install and utilize a jetified version.

Good luck, hope this helps!