bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.25k stars 4.08k forks source link

Aapt2 error: attribute 'package' in <manifest> tag is not a valid Android package name: 'butterknife'. #15598

Open jeraldsun opened 2 years ago

jeraldsun commented 2 years ago

Description of the bug:

I got a aapt2 error when I ran bazel build on my Android project which has both Gradle and Bazel as its build tool:

java.lang.RuntimeException: Error during Statically linking com.google.devtools.build.android.aapt2.CompiledResources@28194a50:
Command: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/androidsdk/aapt2_binary\
    link\
    --static-lib\
    --manifest\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/manifest-aapt-dummy/AndroidManifest.xml\
    --no-static-lib-packages\
    --no-version-vectors\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/com_jakewharton_butterknife_runtime_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/androidx_core_core_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/androidx_lifecycle_lifecycle_runtime_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/androidx_versionedparcelable_versionedparcelable_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/com_jakewharton_butterknife_symbols/symbols.zip\
    -I\
    external/androidsdk/platforms/android-30/android.jar\
    --auto-add-overlay\
    -o\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/lib.apk\
    --java\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/java\
    --output-text-symbols\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/R.txt
Output:
/var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/manifest-aapt-dummy/AndroidManifest.xml:2: error: attribute 'package' in <manifest> tag is not a valid Android package name: 'butterknife'.

    at com.google.devtools.build.android.CommandHelper.execute(CommandHelper.java:42)
    at com.google.devtools.build.android.AaptCommandBuilder.execute(AaptCommandBuilder.java:297)
    at com.google.devtools.build.android.aapt2.ResourceLinker.linkStatically(ResourceLinker.java:252)
    at com.google.devtools.build.android.ValidateAndLinkResourcesAction.main(ValidateAndLinkResourcesAction.java:207)
    at com.google.devtools.build.android.ResourceProcessorBusyBox$Tool$7.call(ResourceProcessorBusyBox.java:105)
    at com.google.devtools.build.android.ResourceProcessorBusyBox.processRequest(ResourceProcessorBusyBox.java:234)
    at com.google.devtools.build.android.ResourceProcessorBusyBox.main(ResourceProcessorBusyBox.java:177)

Exception in thread "main" java.lang.RuntimeException: Error during Statically linking com.google.devtools.build.android.aapt2.CompiledResources@28194a50:
Command: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/androidsdk/aapt2_binary\
    link\
    --static-lib\
    --manifest\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/manifest-aapt-dummy/AndroidManifest.xml\
    --no-static-lib-packages\
    --no-version-vectors\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/com_jakewharton_butterknife_runtime_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/androidx_core_core_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/androidx_lifecycle_lifecycle_runtime_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/androidx_versionedparcelable_versionedparcelable_symbols/symbols.zip\
    -R\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/filtered/bazel-out/android-armeabi-v7a-fastbuild/bin/external/maven/com_jakewharton_butterknife_symbols/symbols.zip\
    -I\
    external/androidsdk/platforms/android-30/android.jar\
    --auto-add-overlay\
    -o\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/lib.apk\
    --java\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/java\
    --output-text-symbols\
    /var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/R.txt
Output:
/var/folders/rb/9p01gkyj44192g83kl03rzwh0000gp/T/android_resources_tmp4927927222921530021/manifest-aapt-dummy/AndroidManifest.xml:2: error: attribute 'package' in <manifest> tag is not a valid Android package name: 'butterknife'.

    at com.google.devtools.build.android.CommandHelper.execute(CommandHelper.java:42)
    at com.google.devtools.build.android.AaptCommandBuilder.execute(AaptCommandBuilder.java:297)
    at com.google.devtools.build.android.aapt2.ResourceLinker.linkStatically(ResourceLinker.java:252)
    at com.google.devtools.build.android.ValidateAndLinkResourcesAction.main(ValidateAndLinkResourcesAction.java:207)
    at com.google.devtools.build.android.ResourceProcessorBusyBox$Tool$7.call(ResourceProcessorBusyBox.java:105)
    at com.google.devtools.build.android.ResourceProcessorBusyBox.processRequest(ResourceProcessorBusyBox.java:234)
    at com.google.devtools.build.android.ResourceProcessorBusyBox.main(ResourceProcessorBusyBox.java:177)
Target //src/main:app failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 3.447s, Critical Path: 3.05s
INFO: 7 processes: 2 internal, 3 darwin-sandbox, 2 worker.
FAILED: Build did NOT complete successfully

I think the root cause is that the ButterKnife project has a wrong package name to aapt2. But when I use Gradle to build my project, this problem doesn't appear. I'm not sure whether Gradle nor AGP make any tricks here. But it seems that Bazel has many aapt2 error when Gradle works well in my project.

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Run bazel build //src/main:app at the root of this project: https://github.com/jeraldsun/BazelAaptButterknifeIssue

Which operating system are you running Bazel on?

macOS 12.3.1

What is the output of bazel info release?

release 5.1.1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

https://github.com/bazelbuild/bazel/issues/10527 https://issuetracker.google.com/issues/73450622

Any other information, logs, or outputs that you want to share?

No response

arunkumar9t2 commented 2 years ago

@jeraldsun as a possible fix you could patch the butterknife aar to replace package name to contain . as expected by Bazel via a genrule.

I created https://github.com/jeraldsun/BazelAaptButterknifeIssue/pull/1/files which demonstrates this approach. rules_jvm_external's override_targets support makes it easier to replace usages automatically.

jeraldsun commented 2 years ago

@jeraldsun as a possible fix you could patch the butterknife aar to replace package name to contain . as expected by Bazel via a genrule.

I created https://github.com/jeraldsun/BazelAaptButterknifeIssue/pull/1/files which demonstrates this approach. rules_jvm_external's override_targets support makes it easier to replace usages automatically.

@arunkumar9t2 Got it. Thank you very much. I'll have a try.