Code Monkey home page Code Monkey logo

Comments (6)

fhilgers avatar fhilgers commented on August 10, 2024

As far as I understood by looking over the implementation, mobile-install mainly uses the AndroidIdeInfo provider which has a merged R.java:

In the extract function from mobile-install/adapters/android_binary:

resource_src_jar = target[AndroidIdeInfo].resource_jar.source_jar,  # This is the R with real ids.

I am not really familiar with android but maybe this is helpful, as in the stacktrace the error is that the specific androidx/startup/R$string could not be found.

When looking at all the split apks that mobile install produces and extracting the dex files there is also only com/example/basicapp/R.class with all the merged resources.

from rules_android.

fhilgers avatar fhilgers commented on August 10, 2024

Okay there are actually two reasons why it fails:

In mobile_install/adapters/android_binary.bzl all the resources files are filtered:

    return dict(
        debug_key = utils.only(ctx.rule.files.debug_key, allow_empty = True),
        debug_signing_keys = ctx.rule.files.debug_signing_keys,
        debug_signing_lineage_file = utils.only(ctx.rule.files.debug_signing_lineage_file, allow_empty = True),
        key_rotation_min_sdk = ctx.rule.attr.key_rotation_min_sdk,
        merged_manifest = target[AndroidIdeInfo].generated_manifest,
        native_libs = target[AndroidIdeInfo].native_libs,
        package = target[AndroidIdeInfo].java_package,
        resource_apk = target[AndroidIdeInfo].resource_apk,
        resource_src_jar = target[AndroidIdeInfo].resource_jar.source_jar,  # This is the R with real ids.
        aar_native_libs_info = MIAndroidAarNativeLibsInfo(
            transitive_native_libs = transitive_native_libs,
        ),
        android_dex_info = providers.make_mi_android_dex_info(
            dex_shards = dex(
                ctx,
                target[JavaInfo].runtime_output_jars +
                #filter_jars(
                #    ctx.label.name + "_resources.jar",
                #    target[JavaInfo].runtime_output_jars,
                #) +
                (
                    [
                        extension_registry_class_jar,
                    ] if extension_registry_class_jar else []
                ),
                target[JavaInfo].transitive_compile_time_jars,
            ),
            deps = providers.collect(MIAndroidDexInfo, ctx.rule.attr.deps),
        ),
        # TODO(djwhang): It wasteful to collect packages in
        # android_resources_info, rather we should be looking to pull them
        # from the resources_v3_info.
        android_resources_info = providers.make_mi_android_resources_info(
            package = target[AndroidIdeInfo].java_package,
            deps = providers.collect(
                MIAndroidResourcesInfo,
                ctx.rule.attr.deps,
            ),
        ),
        java_resources_info = providers.make_mi_java_resources_info(
            deps = providers.collect(
                MIJavaResourcesInfo,
                ctx.rule.attr.deps,
            ),
        ),
        apk = target[AndroidIdeInfo].signed_apk,
    )

Commenting that filtering out makes another problem apparent:

The rules_jvm_external ruleset uses jvm_import instead of java_import for importing jar files. I have not yet looked into the difference but changing that to java_import leads to everything working with mobile-install.

What is the reason all resources files are filtered out? Is it important or was it just overlooked that it filters out external resources. Should they be added somewhere else?

from rules_android.

fhilgers avatar fhilgers commented on August 10, 2024

The reason why jvm_import does not work is that it has to be added to the adapters in the mobile-install aspects.

from rules_android.

fhilgers avatar fhilgers commented on August 10, 2024

So the problem was threefold:

  1. No aspect for jvm_import which is used by rules_jvm_external instead of java import -> The aspect for java_import can be reused for that
  2. Package name is inferred by directory structure or the explicit attribute on aar_import. This is not set by rules_jvm_external. There is alread a Todo label to instead retrieve the package name from AndroidManifest.xml -> I have implemented the package name retrieval from manifest and that works fine.
  3. The aar_import aspect ignores StarlarkAndroidResourcesInfo -> Try to use that if available

Let me know if this makes sense and whether these proposed fixes are appropriate, so that I can open a pr.

from rules_android.

ted-xie avatar ted-xie commented on August 10, 2024

Hi, thanks for filing this issue!

  1. I think jvm_import only exists in rules_jvm_external. I believe they had their own reasons to write their own rule for dealing with imported jars rather than re-using java_import.
  2. Usually package name inferred by directory structure works well enough. Can you elaborate why this doesn't work for your case? I also don't see where you've implemented the package name retrieval in the patch you shared.
  3. I think we will end up implementing this in the near future, as we finish up the Starlark migration effort for rules_android and fully delete both native Android rules and native Android providers from Bazel.

from rules_android.

fhilgers avatar fhilgers commented on August 10, 2024

rules_jvm_external is not setting the package field in aar_import and as the BUILD file with all imports is in the root of the generated repository the package name is empty. My usecase was just importing any library from maven but because the package name is unset the resources are missing or in the wrong location for them to be found. The patch I shared was before I found out the problems, it was just importing packages from maven via rules_jvm_external where the problem happened. I can clean up a bit and make a pr so it can be looked at.

from rules_android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.