Skip to content

Commit

Permalink
Make android_aar_prebuilt() aware of remaining features
Browse files Browse the repository at this point in the history
* Fail if it finds a non-trivial AndroidManifest.xml
* Fail if it finds any .so files
* Fail if it finds any assets
* Support proguard.txt

This also tweaks the naming of the sub-jar targets to give them better
target names (which show up in .jar names).

TBR=bshe
BUG=640836

Review-Url: https://codereview.chromium.org/2309643002
Cr-Commit-Position: refs/heads/master@{#417160}
  • Loading branch information
agrieve authored and Commit bot committed Sep 8, 2016
1 parent 64a891a commit 8dbd4fb
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 22 deletions.
52 changes: 48 additions & 4 deletions build/android/gyp/aar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

import argparse
import os
import posixpath
import re
import shutil
import sys
from xml.etree import ElementTree
import zipfile

from util import build_utils
Expand All @@ -19,6 +22,22 @@
import gn_helpers


def _IsManifestEmpty(manifest_str):
"""Returns whether the given manifest has merge-worthy elements.
E.g.: <activity>, <service>, etc.
"""
doc = ElementTree.fromstring(manifest_str)
for node in doc:
if node.tag == 'application':
if len(node):
return False
elif node.tag != 'uses-sdk':
return False

return True


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--input-file',
Expand Down Expand Up @@ -50,14 +69,39 @@ def main():

if args.list:
data = {}
data['aidl'] = []
data['assets'] = []
data['resources'] = []
data['jars'] = []
data['subjars'] = []
data['subjar_tuples'] = []
data['has_classes_jar'] = False
data['has_proguard_flags'] = False
data['has_native_libraries'] = False
with zipfile.ZipFile(aar_file) as z:
data['is_manifest_empty'] = (
_IsManifestEmpty(z.read('AndroidManifest.xml')))

for name in z.namelist():
if name.startswith('res/') and not name.endswith('/'):
if name.endswith('/'):
continue
if name.startswith('aidl/'):
data['aidl'].append(name)
elif name.startswith('res/'):
data['resources'].append(name)
if name.endswith('.jar'):
data['jars'].append(name)
elif name.startswith('libs/') and name.endswith('.jar'):
label = posixpath.basename(name)[:-4]
label = re.sub(r'[^a-zA-Z0-9._]', '_', label)
data['subjars'].append(name)
data['subjar_tuples'].append([label, name])
elif name.startswith('assets/'):
data['assets'].append(name)
elif name.startswith('jni/'):
data['has_native_libraries'] = True
elif name == 'classes.jar':
data['has_classes_jar'] = True
elif name == 'proguard.txt':
data['has_proguard_flags'] = True

print gn_helpers.ToGNString(data)


Expand Down
103 changes: 85 additions & 18 deletions build/config/android/rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,10 @@ if (enable_java_templates) {
# aar_path: Path to the AAR.
# proguard_configs: List of proguard configs to use in final apk step for
# any apk that depends on this library.
# ignore_aidl: Whether to ignore .aidl files found with the .aar.
# ignore_assets: Whether to ignore assets found in the .aar.
# ignore_manifest: Whether to ignore merging of AndroidManifest.xml.
# ignore_native_libraries: Whether to ignore .so files found in the .aar.
# TODO(jbudorick@): remove this arguments after crbug.com/522043 is fixed.
# requires_android: Whether this target can only be used for compiling Android related targets.
#
Expand All @@ -2601,9 +2605,14 @@ if (enable_java_templates) {
# aar_path = "foo.aar"
# }
template("android_aar_prebuilt") {
assert(defined(invoker.aar_path))
_output_path = "${target_gen_dir}/${target_name}"
_unpack_target_name = "${target_name}__unpack_aar"
_ignore_aidl = defined(invoker.ignore_aidl) && invoker.ignore_aidl
_ignore_assets = defined(invoker.ignore_assets) && invoker.ignore_assets
_ignore_manifest =
defined(invoker.ignore_manifest) && invoker.ignore_manifest
_ignore_native_libraries = defined(invoker.ignore_native_libraries) &&
invoker.ignore_native_libraries

# Scan the AAR file and determine the resources and jar files.
# Some libraries might not have resources; others might have two jars.
Expand All @@ -2616,6 +2625,24 @@ if (enable_java_templates) {
],
"scope")

assert(_ignore_aidl || _scanned_files.aidl == [],
"android_aar_prebuilt() aidl not yet supported." +
" Implement or use ignore_aidl = true." +
" http://crbug.com/644439")
assert(_ignore_assets || _scanned_files.assets == [],
"android_aar_prebuilt() assets not yet supported." +
" Implement or use ignore_assets = true." +
" http://crbug.com/643966")
assert(_ignore_native_libraries || !_scanned_files.has_native_libraries,
"android_aar_prebuilt() with .so files is not supported." +
" Use ignore_native_libraries = true to silence this error.")
assert(_ignore_manifest || _scanned_files.is_manifest_empty,
"android_aar_prebuilt() manifest merging not yet supported and" +
" non-trivial AndroidManifest.xml detected." +
" Implement or use ignore_manifest = true." +
" http://crbug.com/643967")
assert(_scanned_files.has_classes_jar || _scanned_files.subjars == [])

action(_unpack_target_name) {
script = "//build/android/gyp/aar.py" # Unzips the AAR
args = [
Expand All @@ -2638,19 +2665,20 @@ if (enable_java_templates) {
rebase_path(_scanned_files.resources, "", _output_path),
"abspath")
}
if (defined(_scanned_files.jars)) {
outputs +=
get_path_info(rebase_path(_scanned_files.jars, "", _output_path),
"abspath")
if (_scanned_files.has_classes_jar) {
outputs += [ "${_output_path}/classes.jar" ]
}
outputs +=
get_path_info(rebase_path(_scanned_files.subjars, "", _output_path),
"abspath")
if (_scanned_files.has_proguard_flags) {
outputs += [ "${_output_path}/proguard.txt" ]
}
}

_resource_targets = []

# Create the android_resources target for resources.
if (_scanned_files.resources != []) {
_res_target_name = "${target_name}__res"
_resource_targets += [ ":$_res_target_name" ]
android_resources(_res_target_name) {
forward_variables_from(invoker, [ "deps" ])
if (!defined(deps)) {
Expand All @@ -2667,14 +2695,32 @@ if (enable_java_templates) {
}
}

# Create android_java_prebuilt targets for jar files.
_jar_targets = []
_counter = 0
foreach(jar, _scanned_files.jars) {
_counter += 1
_current_target = "${target_name}__jar_$_counter"
_jar_targets += [ ":$_current_target" ]
# Create android_java_prebuilt target for extra jars within jars/.
_subjar_targets = []
foreach(_tuple, _scanned_files.subjar_tuples) {
_current_target = "${target_name}__subjar_${_tuple[0]}"
_subjar_targets += [ ":$_current_target" ]
java_prebuilt(_current_target) {
forward_variables_from(invoker,
[
"jar_excluded_patterns",
"requires_android",
])
deps = [
":$_unpack_target_name",
]
if (!defined(requires_android)) {
requires_android = true
}
supports_android = true
jar_path = "$_output_path/${_tuple[1]}"
}
}

# Create android_java_prebuilt target for classes.jar.
if (_scanned_files.has_classes_jar) {
_jar_target_name = "${target_name}__classes"
java_prebuilt(_jar_target_name) {
forward_variables_from(invoker,
[
"deps",
Expand All @@ -2686,17 +2732,38 @@ if (enable_java_templates) {
if (!defined(deps)) {
deps = []
}
deps += _resource_targets + [ ":$_unpack_target_name" ]
deps += _subjar_targets + [ ":$_unpack_target_name" ]
if (defined(_res_target_name)) {
deps += [ ":$_res_target_name" ]
}
if (!defined(requires_android)) {
requires_android = true
}
supports_android = true
jar_path = "${_output_path}/$jar"
jar_path = "$_output_path/classes.jar"

if (_scanned_files.has_proguard_flags) {
if (!defined(proguard_configs)) {
proguard_configs = []
}
proguard_configs += [ "$_output_path/proguard.txt" ]
}
}
}

java_group(target_name) {
deps = _resource_targets + _jar_targets
deps = []
if (defined(_jar_target_name)) {
deps += [ ":$_jar_target_name" ]

# Although subjars are meant to be private, we add them as deps here
# because in practice they seem to contain classes required to be in the
# classpath.
deps += _subjar_targets
}
if (defined(_res_target_name)) {
deps += [ ":$_res_target_name" ]
}
}
}
}
1 change: 1 addition & 0 deletions build/secondary/third_party/android_tools/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ android_java_prebuilt("android_support_annotations_java") {
android_aar_prebuilt("android_support_v4_java") {
lib_name = "support-v4"
aar_path = "$lib_path/$lib_name/$lib_version/$lib_name-$lib_version.aar"
ignore_aidl = true # We don't appear to need these currently.
}

android_aar_prebuilt("android_support_v13_java") {
Expand Down
2 changes: 2 additions & 0 deletions third_party/gvr-android-sdk/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ android_aar_prebuilt("gvr_common_java") {
android_aar_prebuilt("gvr_base_java") {
aar_path = "src/libraries/base/base.aar"
proguard_configs = [ "proguard/base.flags" ]
ignore_manifest = true # Ignored because manifest merging is not supported (http://crbug.com/643967)
ignore_native_libraries = true
}

android_aar_prebuilt("gvr_controller_java") {
Expand Down

0 comments on commit 8dbd4fb

Please sign in to comment.