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

Cannot use compiler plugins on Android #319

Open
refi64 opened this issue Apr 22, 2020 · 6 comments
Open

Cannot use compiler plugins on Android #319

refi64 opened this issue Apr 22, 2020 · 6 comments
Labels
component: android component: jvm P1 status: need more information This issue needs response/confirmation/update from the reporter type: bug Something isn't working

Comments

@refi64
Copy link

refi64 commented Apr 22, 2020

I have the following in my BUILD file:

kt_compiler_plugin(
    name = "serialization_plugin",
    deps = [
        "@com_github_jetbrains_kotlin//:kotlinx-serialization-compiler-plugin",
    ],
)

# ...

kt_android_library(
    name = "%s_lib" % mode, 
    plugins = [":serialization_plugin"],
    # ...
)

When built, this results in:

in deps attribute of android_binary rule //app:cloverplay_paid: Dependencies on .jar artifacts are not allowed in Android binaries, please use a java_import to depend on external/com_github_jetbrains_kotlin/lib/kotlinx-serialization-compiler-plugin.jar. If this is an implicit dependency then the rule that introduces it will need to be fixed to account for it correctly.. Since this rule was created by the macro 'android_binary', the error might have been caused by the macro implementation

If I use java_import to import the JAR file, then I get:

in jars attribute of java_import rule //app:serialization_plugin_jar: should not refer to Java rules

At a glance I can tell this commit is related, but beyond that I'm not sure what's going on. I guess the plugin's jar file is ending up in the deps, but somehow differently than kt_jvm_import normally does?

@cgruber
Copy link
Collaborator

cgruber commented Jun 3, 2020

You need to use a kt_jvm_import against the raw jar, and then depend on the import target in the kt_compiler_plugin() statement.

@cgruber
Copy link
Collaborator

cgruber commented Jun 3, 2020

The "use a java_import" is a default bazel message, which doesn't account for kotlin, sadly. Try using kt_jvm_import.

@refi64
Copy link
Author

refi64 commented Aug 21, 2020

Sorry for the super delayed reply, but the :kotlinx-serialization-compiler-plugin target is already using kt_jvm_import, so I'm not sure how this is actually supposed to work out then?

@bjdodson
Copy link

I'm experiencing the same issue trying to add Anvil to our project. I've tried both referencing the anvil-compiler jar directly, and referencing as a maven dependency. In this case I think we'll need to ultimately reference as a maven dependency, since Anvil has several dependencies of its own.

In my closest attempt, I have the anvil_compiler_plugin rule building, but the plugin doesn't run.

kt_jvm_import(
    name = "anvil_compiler",
    jar = "kotlin/kotlinc/lib/anvil-compiler-2.0.0.jar",
    neverlink = True
)

kt_compiler_plugin(
    name = "anvil_compiler_plugin",
    id = "com.squareup.anvil.compiler",
    options = {
    },
    deps = [
        ":anvil_compiler",
    ],
    visibility = ["//visibility:public"],
)

kt_android_library(
    name = "bindings",
    srcs = glob([
        ...
    ]),
    plugins = [
        '//libs:anvil_compiler_plugin'
    ],

@Nikolas-LFDesigns
Copy link
Contributor

Still the same issue on 1.4.0-legacy-RC4.
Doesn't work even on predefined plugins like allopen:

kt_compiler_plugin(
    name = "open_for_testing_plugin",
    id = "org.jetbrains.kotlin.allopen",
    options = {
        "annotation": "com.example.OpenForTesting", # my custom annotation here
    },
    deps = [
        "@com_github_jetbrains_kotlin//:allopen-compiler-plugin",
    ],
)

kt_android_library(
    name = "custom_library"
    [...]
    plugins = [
        ":open_for_testing_plugin",
    ],
    [...]
)

android_binary(
     [...]
     deps = [
          ":custom_library",
          [...]
     ],
)

Dependencies on .jar artifacts are not allowed in Android binaries, please use a java_import to depend on external/com_github_jetbrains_kotlin/lib/allopen-compiler-plugin.jar. .
Sounds really like a jar from the plugin is only intended to work on host jvm (e.g. kt_jvm_binary or android_local_test), but not on android binaries. Is there a way to set a plugin binary dependency to not be included as a library dependency on which a plugin is run? Like maybe runtime_deps etc?

@markrietveld
Copy link

Is this still an issue in 2024, or has this been fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: android component: jvm P1 status: need more information This issue needs response/confirmation/update from the reporter type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants