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

Support multiple kt_android_library targets with same package name. #790

Conversation

arunkumar9t2
Copy link
Contributor

@arunkumar9t2 arunkumar9t2 commented Jun 29, 2022

When two kt_android_library or android_library has the same package name either provided via custom_package or due to same package name in AndroidManifest.xml, R class fields from current module's resources were not compilable due to order of jars appearing in --direct_dependencies.

The current module's resources jar would contain merged R class from all dependencies but because it appears later in the list, the dependencies' R class is used which does not contain merged R class. The problem only appears when two targets contains the same package name.

The fix is similar to one applied in Bazel

This PR ensures the merged resources jar always comes ensuring order in deps attribute

@arunkumar9t2 arunkumar9t2 changed the title Support multiple kt_android_library targets with same package name. Fixes #787 Support multiple kt_android_library targets with same package name. Jun 29, 2022
@arunkumar9t2
Copy link
Contributor Author

Fixes #787

@corbinrsmith
Copy link

Did you try inverting the base_deps and base_name in https://github.com/bazelbuild/rules_kotlin/blob/master/kotlin/internal/jvm/android.bzl#L56?

It would be much less invasive that changing the order inside the rules.

@@ -51,6 +51,11 @@ load(
def _java_info(target):
return target[JavaInfo] if JavaInfo in target else None

def _is_android_sandwich(current_label_name, target):
"""Checks if the given target is <target>_base target from android sandwich macro"""
is_base_target = current_label_name.removesuffix("_kt") == target.label.name.removesuffix("_base")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work on Bazel versions older than 5.1.0, bazelbuild/bazel#14899

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nikolas-LFDesigns thanks, I removed this impl and just switched the order in deps attribute. That also worked.

@arunkumar9t2 arunkumar9t2 force-pushed the arun/support-merged-resources-jar-in-android-sandwich branch 2 times, most recently from 1b08d68 to 62c5d7f Compare June 29, 2022 20:52
…ixes bazelbuild#787

When two `kt_android_library` or `android_library` has the same package name either provided via `custom_package` or due to same package name in `AndroidManifest.xml`, R class fields from current module's resources were not compilable due to order of jars appearing in `--direct_dependencies`.

This CL ensure the merged resources jar always comes first.
@arunkumar9t2 arunkumar9t2 force-pushed the arun/support-merged-resources-jar-in-android-sandwich branch from 62c5d7f to c69a774 Compare June 29, 2022 20:54
@arunkumar9t2
Copy link
Contributor Author

Thanks @corbinrsmith, that worked as well. Updated PR.

@@ -89,7 +94,7 @@ def _compiler_toolchains(ctx):
java_runtime = find_java_runtime_toolchain(ctx, ctx.attr._host_javabase),
)

def _jvm_deps(toolchains, associated_targets, deps, runtime_deps = []):
def _jvm_deps(current_label_name, toolchains, associated_targets, deps, runtime_deps = []):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't infer off label name -- too brittle.

@restingbull restingbull merged commit 5f86721 into bazelbuild:master Jun 29, 2022
@arunkumar9t2 arunkumar9t2 deleted the arun/support-merged-resources-jar-in-android-sandwich branch June 30, 2022 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants