Merge "Allow for setting a logging_parent for an Android App."

This commit is contained in:
Treehugger Robot
2020-02-18 22:57:31 +00:00
committed by Gerrit Code Review
6 changed files with 141 additions and 28 deletions

View File

@@ -101,6 +101,7 @@ type aapt struct {
usesNonSdkApis bool
sdkLibraries []string
hasNoCode bool
LoggingParent string
splitNames []string
splits []split
@@ -234,7 +235,8 @@ func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, ex
manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode)
a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode,
a.LoggingParent)
// Add additional manifest files to transitive manifests.
additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)

View File

@@ -53,7 +53,7 @@ var optionalUsesLibs = []string{
// Uses manifest_fixer.py to inject minSdkVersion, etc. into an AndroidManifest.xml
func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext sdkContext, sdkLibraries []string,
isLibrary, useEmbeddedNativeLibs, usesNonSdkApis, useEmbeddedDex, hasNoCode bool) android.Path {
isLibrary, useEmbeddedNativeLibs, usesNonSdkApis, useEmbeddedDex, hasNoCode bool, loggingParent string) android.Path {
var args []string
if isLibrary {
@@ -91,6 +91,9 @@ func manifestFixer(ctx android.ModuleContext, manifest android.Path, sdkContext
args = append(args, "--has-no-code")
}
if loggingParent != "" {
args = append(args, "--logging-parent", loggingParent)
}
var deps android.Paths
targetSdkVersion, err := sdkContext.targetSdkVersion().effectiveVersionString(ctx)
if err != nil {

View File

@@ -111,6 +111,9 @@ type overridableAppProperties struct {
// the package name of this app. The package name in the manifest file is used if one was not given.
Package_name *string
// the logging parent of this app.
Logging_parent *string
}
type AndroidApp struct {
@@ -303,7 +306,7 @@ func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
a.aapt.splitNames = a.appProperties.Package_splits
a.aapt.sdkLibraries = a.exportedSdkLibs
a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
// apps manifests are handled by aapt, don't let Module see them

View File

@@ -1181,6 +1181,7 @@ func TestOverrideAndroidApp(t *testing.T) {
name: "bar",
base: "foo",
certificate: ":new_certificate",
logging_parent: "bah",
}
android_app_certificate {
@@ -1203,6 +1204,7 @@ func TestOverrideAndroidApp(t *testing.T) {
signFlag string
overrides []string
aaptFlag string
logging_parent string
}{
{
moduleName: "foo",
@@ -1211,6 +1213,7 @@ func TestOverrideAndroidApp(t *testing.T) {
signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
overrides: []string{"qux"},
aaptFlag: "",
logging_parent: "",
},
{
moduleName: "bar",
@@ -1219,6 +1222,7 @@ func TestOverrideAndroidApp(t *testing.T) {
signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
overrides: []string{"qux", "foo"},
aaptFlag: "",
logging_parent: "bah",
},
{
moduleName: "baz",
@@ -1227,6 +1231,7 @@ func TestOverrideAndroidApp(t *testing.T) {
signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
overrides: []string{"qux", "foo"},
aaptFlag: "--rename-manifest-package org.dandroid.bp",
logging_parent: "",
},
}
for _, expected := range expectedVariants {
@@ -1260,6 +1265,13 @@ func TestOverrideAndroidApp(t *testing.T) {
expected.overrides, mod.appProperties.Overrides)
}
// Test Overridable property: Logging_parent
logging_parent := mod.aapt.LoggingParent
if expected.logging_parent != logging_parent {
t.Errorf("Incorrect overrides property value for logging parent, expected: %q, got: %q",
expected.logging_parent, logging_parent)
}
// Check the package renaming flag, if exists.
res := variant.Output("package-res.apk")
aapt2Flags := res.Args["flags"]

View File

@@ -51,6 +51,9 @@ def parse_args():
help='specify additional <uses-library> tag to add. android:requred is set to false')
parser.add_argument('--uses-non-sdk-api', dest='uses_non_sdk_api', action='store_true',
help='manifest is for a package built against the platform')
parser.add_argument('--logging-parent', dest='logging_parent', default='',
help=('specify logging parent as an additional <meta-data> tag. '
'This value is ignored if the logging_parent meta-data tag is present.'))
parser.add_argument('--use-embedded-dex', dest='use_embedded_dex', action='store_true',
help=('specify if the app wants to use embedded dex and avoid extracted,'
'locally compiled code. Must not conflict if already declared '
@@ -124,6 +127,52 @@ def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library):
element.setAttributeNode(target_attr)
def add_logging_parent(doc, logging_parent_value):
"""Add logging parent as an additional <meta-data> tag.
Args:
doc: The XML document. May be modified by this function.
logging_parent_value: A string representing the logging
parent value.
Raises:
RuntimeError: Invalid manifest
"""
manifest = parse_manifest(doc)
logging_parent_key = 'android.content.pm.LOGGING_PARENT'
elems = get_children_with_tag(manifest, 'application')
application = elems[0] if len(elems) == 1 else None
if len(elems) > 1:
raise RuntimeError('found multiple <application> tags')
elif not elems:
application = doc.createElement('application')
indent = get_indent(manifest.firstChild, 1)
first = manifest.firstChild
manifest.insertBefore(doc.createTextNode(indent), first)
manifest.insertBefore(application, first)
indent = get_indent(application.firstChild, 2)
last = application.lastChild
if last is not None and last.nodeType != minidom.Node.TEXT_NODE:
last = None
if not find_child_with_attribute(application, 'meta-data', android_ns,
'name', logging_parent_key):
ul = doc.createElement('meta-data')
ul.setAttributeNS(android_ns, 'android:name', logging_parent_key)
ul.setAttributeNS(android_ns, 'android:value', logging_parent_value)
application.insertBefore(doc.createTextNode(indent), last)
application.insertBefore(ul, last)
last = application.lastChild
# align the closing tag with the opening tag if it's not
# indented
if last and last.nodeType != minidom.Node.TEXT_NODE:
indent = get_indent(application.previousSibling, 1)
application.appendChild(doc.createTextNode(indent))
def add_uses_libraries(doc, new_uses_libraries, required):
"""Add additional <uses-library> tags
@@ -291,6 +340,9 @@ def main():
if args.uses_non_sdk_api:
add_uses_non_sdk_api(doc)
if args.logging_parent:
add_logging_parent(doc, args.logging_parent)
if args.use_embedded_dex:
add_use_embedded_dex(doc)

View File

@@ -226,6 +226,47 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
self.assertEqual(output, expected)
class AddLoggingParentTest(unittest.TestCase):
"""Unit tests for add_logging_parent function."""
def add_logging_parent_test(self, input_manifest, logging_parent=None):
doc = minidom.parseString(input_manifest)
if logging_parent:
manifest_fixer.add_logging_parent(doc, logging_parent)
output = StringIO.StringIO()
manifest_fixer.write_xml(output, doc)
return output.getvalue()
manifest_tmpl = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
'%s'
'</manifest>\n')
def uses_logging_parent(self, logging_parent=None):
attrs = ''
if logging_parent:
meta_text = ('<meta-data android:name="android.content.pm.LOGGING_PARENT" '
'android:value="%s"/>\n') % (logging_parent)
attrs += ' <application>\n %s </application>\n' % (meta_text)
return attrs
def test_no_logging_parent(self):
"""Tests manifest_fixer with no logging_parent."""
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_logging_parent()
output = self.add_logging_parent_test(manifest_input)
self.assertEqual(output, expected)
def test_logging_parent(self):
"""Tests manifest_fixer with no logging_parent."""
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_logging_parent('FOO')
output = self.add_logging_parent_test(manifest_input, 'FOO')
self.assertEqual(output, expected)
class AddUsesLibrariesTest(unittest.TestCase):
"""Unit tests for add_uses_libraries function."""