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 .aar files #564

Closed
cgrushko opened this issue Nov 4, 2015 · 22 comments
Closed

Support .aar files #564

cgrushko opened this issue Nov 4, 2015 · 22 comments
Assignees
Labels
P1 I'll work on this now. (Assignee required) type: feature request
Milestone

Comments

@cgrushko
Copy link
Contributor

cgrushko commented Nov 4, 2015

... similarly to java_import, we need a way to use .aar files in Android builds.

@davidzchen
Copy link
Member

Related: #561

@damienmg damienmg added type: feature request P2 We'll consider working on this in future. (Assignee optional) mobile labels Nov 17, 2015
@ahumesky ahumesky assigned ahumesky and unassigned lberki Jul 12, 2016
@paladine
Copy link

This issue should be raised in priority given that the new Google Play Services ships exclusively in AAR format. It no longer ships in a buildable form. Therefore you can't really build an Android application with bazel that utilizes Play Services.

What we really need is support for external AAR files, like new_local_repository where you can define all the aar Google Play Service files, and have dependencies on them from your android_library/binary

@damienmg damienmg added P1 I'll work on this now. (Assignee required) and removed P2 We'll consider working on this in future. (Assignee optional) labels Jul 27, 2016
@damienmg damienmg added this to the 0.4 milestone Jul 27, 2016
@damienmg
Copy link
Contributor

@ahumesky This is definitely a P1 now, is this beeing working on?

@ahumesky
Copy link
Contributor

ahumesky commented Jul 27, 2016

Was there some announcement somewhere about Google Play Services no longer updating google-play-services.jar (/extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar)?

Importing AARs is blocked on the unpredictable action outputs work, which is being worked on, but it's not in a stable state right now. We could possibly come up with a workaround where the android_sdk_repository rule inspects the pom files and aar files in the android sdk's local maven repository where the google play services are, and generates a build file with genrules that unzips the aars and creates the other rules (java_impor, android_library, etc). It would be a pretty big hack. I'm not sure when we could find the cycles for that, it would be at least a few weeks.

@BrianEdmonds
Copy link

BrianEdmonds commented Aug 6, 2016

I needed this for personal use, so here's your hack, which I whipped up this morning. How I've run it:

$ cd <WORKSPACE>/android
$ mkdir com.android.support
$ cd com.android.support
$ <PATH/TO>/m2repo2build.sh

Then depend on something like //android/com.android.support:appcompat-v7. Lather, rinse, repeat for other m2repository packages (eg. com.google.firebase, com.google.android.gms, etc). You'll need to modify the script to point to your android-sdk-linux directory. No guarantee of function on any platform, but it works nicely for me so far.

m2repo2build.zip

@BrianEdmonds
Copy link

Turns out it needed some hacking to work on MacOS, and I wasn't dealing with implementation jars, and was getting the custom_packages wrong, but that is now accounted for. Again definitely still a hack, and no guarantees it'll do anything useful for anyone else.

m2repo2build.zip

@damienmg
Copy link
Contributor

damienmg commented Sep 1, 2016

@ahumesky : ping can we resolve this in the following week? It looks like we need to drop it from 0.4 but that seems serious enough for not dropping it. It might need retriage.

@ahumesky
Copy link
Contributor

ahumesky commented Sep 1, 2016

The underlying mechanism (unpredictable action outputs) isn't ready yet, so it's pretty unlikely to be resolved next week. We could pursue the hacks mentioned above, but we even for that we won't have time to work on this for a few weeks at least.

@damienmg damienmg modified the milestones: 0.5, 0.4 Sep 1, 2016
@damienmg
Copy link
Contributor

damienmg commented Sep 1, 2016

Dropping to milestones 0.5 :( [sadpanda]

@BrianEdmonds
Copy link

Just saying, the script I posted is working great for me. I'd be happier with a real, baked in solution, but if that is a whole extra major milestone away, you might want to include some hints on how to work around it in the mean time.

@aj-michael
Copy link
Contributor

Just to confirm what has already been said about google-play-services being .aar only, with the current Google Play Services (version 32) installed:

$ bazel build @androidsdk//:google_play_services
...
ERROR: missing input file '@androidsdk//:extras/google/google_play_services/libproject/google-play-services_lib/AndroidManifest.xml'.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/1b3a644758ec22c3d80472e8df5aba01/external/androidsdk/BUILD:5:1: @androidsdk//:google_play_services: missing input file '@androidsdk//:extras/google/google_play_services/libproject/google-play-services_lib/AndroidManifest.xml'.
Target @androidsdk//:google_play_services failed to build
...

bazel-io pushed a commit that referenced this issue Sep 14, 2016
…. Currently only uses AndroidManifest.xml, classes.jar and res/ from AARs. This is sufficient for many of the AARs of Google Play Services and Android Support Repository.

The next step will be for AndroidSdkRepositoryRule to scan the SDK and generate aar_import rules for the AARs within.

The rule is not yet documented because it is not intended for end users to use it yet. We should probably support more of the features of AARs before that time. See http://tools.android.com/tech-docs/new-build-system/aar-format for all of the files that can be included in AARs.

Also note that R.txt from the AAR is intentionally ignored and regenerated based on the contents of res/. This is more correct, because the R.txt inside of an AAR can contain ids for dependencies of the AAR that are not included in res/.

See #564 for discussion of supporting AARs and #1745 for motivation to get it done soon.

--
MOS_MIGRATED_REVID=133127933
@sbilkoloft
Copy link

@aj-michael can u kindly give me an example how to use aar_import? I know it's not documented on purpose and people aren't supposed to be using it yet, but I don't mind if it breaks in the future as long as it can unblock us now.

@aj-michael
Copy link
Contributor

Hi @sbilkoloft, sorry for the delay. If you build bazel from HEAD, you can use the rule as

aar_import(
    name = "myaar",
    aar = "my-local-aar.aar",
)

aar_import(
    name = "myotheraar",
    aar = "my-other-local-aar.aar",
    exports = [
        ":myaar",
    ],
)

android_library(
    name = "mylib",
    ... 
    deps = [":myotheraar"],
)

This will give your android_library or android_binary access to the classes and resources from the AAR. If you have any questions or feedback, please let me know.

@aj-michael aj-michael assigned aj-michael and unassigned ahumesky Oct 10, 2016
@sbilkoloft
Copy link

Hi @aj-michael,

For the record, seems like the code snippet posted above doesn't work as is. Does the 'aar' field have to be a path? If so, it doesn't accept absolute paths (says it cannot start with '/'). I also tried creating a symlink to my aar file and referencing it locally. Got an error that bazel-out/.../_aar/unzipped/myaar doesn't exist.

@lingz
Copy link

lingz commented Oct 15, 2016

@aj-michael I'm getting the same error sbilkoloft is reporting. bazel-out/../_aar/ etc doesn't exist

@aj-michael
Copy link
Contributor

@sbilkoloft The 'aar' field is a path relative to the BUILD file. Think of it like how "srcs" works on android_binary. It cannot accept absolute paths because Bazel rules only have access to the files in the package. I haven't tried it with symlinks, but it's intended to be used with the AAR in your source tree.

From the partial error snippet you posted, that is likely do to this bug, #1928, that occurs if the AAR does not have any resources. The workaround for now is to use the --spawn_strategy=standalone to disable Bazel sandboxing.

@aj-michael
Copy link
Contributor

As of 3f008a0, #1928 is closed, so aar_import will no longer require --spawn_strategy=standalone in all Bazel binaries built after that commit. Marking this issue as fixed as aar_import is now supported.

@aj-michael
Copy link
Contributor

I closed this issue a bit too soon. b5f480c was just committed which is also required for aar_import to run in the Bazel sandbox.

There is one change remaining, which is to document this rule. Reopening until that is submitted.

@aj-michael aj-michael reopened this Dec 12, 2016
@merryHunter
Copy link

Probably, I am doing something wrong, but when I am trying to include aar of gvr sdk as shown in the example documentation, I get:
....

Cannot stat file /home/ivan/.cache/bazel/_bazel_ivan/94788c7f3adc99cf31db5cbecde32303/bazel-sandbox/5e63d754-5cf1-441e-853c-1f98fdc9fd06-17/execroot/tutorial/bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/_aar/unzipped/resources/google-vr-sdk_b/res/values: No such file or directory
....
ivan@ivan:~/projects/isee/examples/tutorial$ bazel build //android:android --verbose_failures
INFO: Found 1 target...
ERROR: /home/ivan/projects/isee/examples/tutorial/android/BUILD:11:1: Processing Android resources for //android:google-vr-sdk_a failed: linux-sandbox failed: error executing command
(cd /home/ivan/.cache/bazel/_bazel_ivan/94788c7f3adc99cf31db5cbecde32303/bazel-sandbox/108a9868-f2ae-466c-bd3b-47b6ff0aa1b2-0/execroot/tutorial &&
exec env -
/home/ivan/.cache/bazel/_bazel_ivan/94788c7f3adc99cf31db5cbecde32303/execroot/tutorial/_bin/linux-sandbox @/home/ivan/.cache/bazel/_bazel_ivan/94788c7f3adc99cf31db5cbecde32303/bazel-sandbox/108a9868-f2ae-466c-bd3b-47b6ff0aa1b2-0/linux-sandbox.params -- bazel-out/host/bin/external/bazel_tools/src/tools/android/java/com/google/devtools/build/android/AndroidResourceProcessingAction --buildToolsVersion 24.0.3 --aapt bazel-out/host/bin/external/androidsdk/aapt_binary --annotationJar external/androidsdk/tools/support/annotations.jar --androidJar external/androidsdk/platforms/android-23/android.jar --primaryData bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/_aar/unzipped/resources/google-vr-sdk_a::bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/_aar/google-vr-sdk_a/AndroidManifest.xml --packageType LIBRARY --rOutput bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/google-vr-sdk_a_symbols/R.txt --symbolsTxtOut bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/google-vr-sdk_a_symbols/local-R.txt --srcJarOutput bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/google-vr-sdk_a.srcjar --manifestOutput bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/google-vr-sdk_a_processed_manifest/AndroidManifest.xml --resourcesOutput bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/google-vr-sdk_a_files/resource_files.zip '--useAaptCruncher=no' --debug).
Error parsing command line: While parsing option --primaryData: invalid UnvalidatedAndroidData: bazel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-fastbuild/bin/android/_aar/unzipped/resources/google-vr-sdk_a does not exist
Try --help.
Target //android:android failed to build

Instead, with --spawn-strategy=standalone the build is done successfully!
My BUILD file:
`aar_import(
name = "google-vr-sdk_c",
aar = "gvr-libraries/sdk-common-1.10.0.aar", )
aar_import(
name = "google-vr-sdk_a",
aar = "gvr-libraries/sdk-audio-1.10.0.aar", )
aar_import(
name = "google-vr-sdk_b",
aar = "gvr-libraries/sdk-base-1.10.0.aar", )

android_binary(
name = "android",
custom_package = "com.google.bazel.example.android",
manifest = "src/main/java/com/google/bazel/example/android/AndroidManifest.xml",
resource_files = glob(["src/main/java/com/google/bazel/example/android/res/"]),
visibility = ["//visibility:public"],
deps = [":activities",]
)
android_library(
name = "activities",
srcs = glob(["src/main/java/com/google/bazel/example/android/activities/*.java"]),
custom_package = "com.google.bazel.example.android.activities",
manifest = "src/main/java/com/google/bazel/example/android/activities/AndroidManifest.xml",
resource_files = glob(["src/main/java/com/google/bazel/example/android/activities/res/
"]),
deps = [":google-vr-sdk_a", ":google-vr-sdk_b",":google-vr-sdk_c"]
)`
Bazel version 0.4.3

@aj-michael
Copy link
Contributor

aj-michael commented Jan 30, 2017

@merryHunter , it looks like commit b5f480c has not made it into a Bazel release yet. Bazel 0.4.3 was baselined from c645a45 which was on Dec 5, whereas b5f480c was committed on Dec 9. The fix will be in the next release, which you can track the progress of here: #2282.

Alternatively, if you're eager, you can compile Bazel from head with

git clone https://github.com/bazelbuild/bazel
cd bazel
./compile.sh compile `which bazel`
head_bazel=$(pwd)/output/bazel

@merryHunter
Copy link

@aj-michael thank you! I thought I had checked the commit timeline, but I was mistaken.

@DandyLorenz
Copy link

Can you help me see how to solve this problem?Thanks. google-ai-edge/mediapipe#1450

luca-digrazia pushed a commit to luca-digrazia/DatasetCommitsDiffSearch that referenced this issue Sep 5, 2022
    Fixes bazelbuild/bazel#564.

    RELNOTES: aar_import rule is now documented.

    --
    PiperOrigin-RevId: 141843994
    MOS_MIGRATED_REVID=141843994
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 I'll work on this now. (Assignee required) type: feature request
Projects
None yet
Development

No branches or pull requests