Open fhilgers opened 1 month ago
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.
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?
The reason why jvm_import does not work is that it has to be added to the adapters in the mobile-install aspects.
So the problem was threefold:
Let me know if this makes sense and whether these proposed fixes are appropriate, so that I can open a pr.
Hi, thanks for filing this issue!
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.
When trying to use mobile-install on the example/basicapp I am getting the following stacktrace after launching the application: stacktrace.txt
I am using rules_jvm_external with an overridden rules_android to the version from rules_android git repository.
This is a patch to apply to the example:
Edit: I forgot to mention that building and manually installing the apk works fine.