Merge "Update clang to r328903"
diff --git a/README.md b/README.md
index b3239e9..3549b02 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,14 @@
 For a list of valid module types and their properties see
 [$OUT_DIR/soong/.bootstrap/docs/soong_build.html](https://go/Android.bp).
 
+### Globs
+
+Properties that take a list of files can also take glob patterns.  Glob
+patterns can contain the normal Unix wildcard `*`, for example "*.java". Glob
+patterns can also contain a single `**` wildcard as a path element, which will
+match zero or more path elements.  For example, `java/**/*.java` will match
+`java/Main.java` and `java/com/android/Main.java`.
+
 ### Variables
 
 An Android.bp file may contain top-level variable assignments:
diff --git a/cc/library.go b/cc/library.go
index 4a7884b..b31fee2 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -709,7 +709,7 @@
 }
 
 func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
-	return library.wholeStaticMissingDeps
+	return append([]string(nil), library.wholeStaticMissingDeps...)
 }
 
 func (library *libraryDecorator) objs() Objects {
diff --git a/cmd/extract_jar_packages/Android.bp b/cmd/extract_jar_packages/Android.bp
new file mode 100644
index 0000000..ea0cbbf
--- /dev/null
+++ b/cmd/extract_jar_packages/Android.bp
@@ -0,0 +1,25 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+blueprint_go_binary {
+    name: "extract_jar_packages",
+    deps: [
+        "android-archive-zip",
+        "blueprint-pathtools",
+    ],
+    srcs: [
+        "extract_jar_packages.go",
+    ],
+}
+
diff --git a/cmd/extract_jar_packages/extract_jar_packages.go b/cmd/extract_jar_packages/extract_jar_packages.go
new file mode 100644
index 0000000..fca308f
--- /dev/null
+++ b/cmd/extract_jar_packages/extract_jar_packages.go
@@ -0,0 +1,88 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+	"archive/zip"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"path/filepath"
+	"sort"
+	"strings"
+)
+
+var (
+	outputFile = flag.String("o", "", "output file")
+	prefix     = flag.String("prefix", "", "prefix for each entry in the output file")
+	inputFile  = flag.String("i", "", "input jar or srcjar")
+)
+
+func must(err error) {
+	if err != nil {
+		log.Fatal(err)
+	}
+}
+
+func fileToPackage(file string) string {
+	dir := filepath.Dir(file)
+	return strings.Replace(dir, "/", ".", -1)
+}
+
+func main() {
+	flag.Usage = func() {
+		fmt.Fprintln(os.Stderr, "usage: extract_jar_packages -i <input file> -o <output -file> [-prefix <prefix>]")
+		flag.PrintDefaults()
+	}
+
+	flag.Parse()
+
+	if *outputFile == "" || *inputFile == "" {
+		flag.Usage()
+		os.Exit(1)
+	}
+
+	pkgSet := make(map[string]bool)
+
+	reader, err := zip.OpenReader(*inputFile)
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer reader.Close()
+
+	for _, f := range reader.File {
+		ext := filepath.Ext(f.Name)
+		if ext == ".java" || ext == ".class" {
+			pkgSet[fileToPackage(f.Name)] = true
+		}
+	}
+
+	var pkgs []string
+	for k := range pkgSet {
+		pkgs = append(pkgs, k)
+	}
+	sort.Strings(pkgs)
+
+	var data []byte
+	for _, pkg := range pkgs {
+		data = append(data, *prefix...)
+		data = append(data, pkg...)
+		data = append(data, "\n"...)
+	}
+
+	must(ioutil.WriteFile(*outputFile, data, 0666))
+}
diff --git a/java/aapt2.go b/java/aapt2.go
index 61e9451..70c7507 100644
--- a/java/aapt2.go
+++ b/java/aapt2.go
@@ -113,14 +113,17 @@
 	blueprint.RuleParams{
 		Command: `${config.Aapt2Cmd} link -o $out $flags --java $genDir --proguard $proguardOptions ` +
 			`--output-text-symbols ${rTxt} $inFlags && ` +
-			`${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir`,
+			`${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir &&` +
+			`${config.ExtractJarPackagesCmd} -i $genJar -o $extraPackages --prefix '--extra-packages '`,
+
 		CommandDeps: []string{
 			"${config.Aapt2Cmd}",
 			"${config.SoongZipCmd}",
+			"${config.ExtractJarPackagesCmd}",
 		},
 		Restat: true,
 	},
-	"flags", "inFlags", "proguardOptions", "genDir", "genJar", "rTxt")
+	"flags", "inFlags", "proguardOptions", "genDir", "genJar", "rTxt", "extraPackages")
 
 var fileListToFileRule = pctx.AndroidStaticRule("fileListToFile",
 	blueprint.RuleParams{
@@ -130,7 +133,7 @@
 	})
 
 func aapt2Link(ctx android.ModuleContext,
-	packageRes, genJar, proguardOptions, rTxt android.WritablePath,
+	packageRes, genJar, proguardOptions, rTxt, extraPackages android.WritablePath,
 	flags []string, deps android.Paths,
 	compiledRes, compiledOverlay android.Paths) {
 
@@ -172,7 +175,7 @@
 		Description:     "aapt2 link",
 		Implicits:       deps,
 		Output:          packageRes,
-		ImplicitOutputs: android.WritablePaths{proguardOptions, genJar, rTxt},
+		ImplicitOutputs: android.WritablePaths{proguardOptions, genJar, rTxt, extraPackages},
 		Args: map[string]string{
 			"flags":           strings.Join(flags, " "),
 			"inFlags":         strings.Join(inFlags, " "),
@@ -180,6 +183,7 @@
 			"genDir":          genDir.String(),
 			"genJar":          genJar.String(),
 			"rTxt":            rTxt.String(),
+			"extraPackages":   extraPackages.String(),
 		},
 	})
 }
diff --git a/java/aar.go b/java/aar.go
index 16d82af..9e5cddb 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -25,6 +25,8 @@
 type AndroidLibraryDependency interface {
 	Dependency
 	ExportPackage() android.Path
+	ExportedProguardFlagFiles() android.Paths
+	ExportedStaticPackages() android.Paths
 }
 
 func init() {
@@ -57,12 +59,13 @@
 }
 
 type aapt struct {
-	aaptSrcJar          android.Path
-	exportPackage       android.Path
-	manifestPath        android.Path
-	proguardOptionsFile android.Path
-	rroDirs             android.Paths
-	rTxt                android.Path
+	aaptSrcJar            android.Path
+	exportPackage         android.Path
+	manifestPath          android.Path
+	proguardOptionsFile   android.Path
+	rroDirs               android.Paths
+	rTxt                  android.Path
+	extraAaptPackagesFile android.Path
 
 	aaptProperties aaptProperties
 }
@@ -122,9 +125,9 @@
 	linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
 	linkDeps = append(linkDeps, assetFiles...)
 
-	staticLibs, libDeps, libFlags := aaptLibs(ctx, sdkVersion)
+	transitiveStaticLibs, libDeps, libFlags := aaptLibs(ctx, sdkVersion)
 
-	overlayFiles = append(overlayFiles, staticLibs...)
+	overlayFiles = append(overlayFiles, transitiveStaticLibs...)
 	linkDeps = append(linkDeps, libDeps...)
 	linkFlags = append(linkFlags, libFlags...)
 
@@ -177,6 +180,8 @@
 	srcJar := android.PathForModuleGen(ctx, "R.jar")
 	proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
 	rTxt := android.PathForModuleOut(ctx, "R.txt")
+	// This file isn't used by Soong, but is generated for exporting
+	extraPackages := android.PathForModuleOut(ctx, "extra_packages")
 
 	var compiledRes, compiledOverlay android.Paths
 	for _, dir := range resDirs {
@@ -188,7 +193,7 @@
 
 	compiledOverlay = append(compiledOverlay, overlayFiles...)
 
-	aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
+	aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
 		linkFlags, linkDeps, compiledRes, compiledOverlay)
 
 	a.aaptSrcJar = srcJar
@@ -196,11 +201,14 @@
 	a.manifestPath = manifestPath
 	a.proguardOptionsFile = proguardOptionsFile
 	a.rroDirs = rroDirs
+	a.extraAaptPackagesFile = extraPackages
 	a.rTxt = rTxt
 }
 
 // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
-func aaptLibs(ctx android.ModuleContext, sdkVersion string) (staticLibs, deps android.Paths, flags []string) {
+func aaptLibs(ctx android.ModuleContext, sdkVersion string) (transitiveStaticLibs, deps android.Paths,
+	flags []string) {
+
 	var sharedLibs android.Paths
 
 	sdkDep := decodeSdkDep(ctx, sdkVersion)
@@ -210,7 +218,8 @@
 
 	ctx.VisitDirectDeps(func(module android.Module) {
 		var exportPackage android.Path
-		if aarDep, ok := module.(AndroidLibraryDependency); ok {
+		aarDep, _ := module.(AndroidLibraryDependency)
+		if aarDep != nil {
 			exportPackage = aarDep.ExportPackage()
 		}
 
@@ -221,15 +230,16 @@
 			}
 		case staticLibTag:
 			if exportPackage != nil {
-				staticLibs = append(staticLibs, exportPackage)
+				transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
+				transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
 			}
 		}
 	})
 
 	deps = append(deps, sharedLibs...)
-	deps = append(deps, staticLibs...)
+	deps = append(deps, transitiveStaticLibs...)
 
-	if len(staticLibs) > 0 {
+	if len(transitiveStaticLibs) > 0 {
 		flags = append(flags, "--auto-add-overlay")
 	}
 
@@ -237,7 +247,9 @@
 		flags = append(flags, "-I "+sharedLib.String())
 	}
 
-	return staticLibs, deps, flags
+	transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
+
+	return transitiveStaticLibs, deps, flags
 }
 
 type AndroidLibrary struct {
@@ -247,6 +259,17 @@
 	androidLibraryProperties androidLibraryProperties
 
 	aarFile android.WritablePath
+
+	exportedProguardFlagFiles android.Paths
+	exportedStaticPackages    android.Paths
+}
+
+func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
+	return a.exportedProguardFlagFiles
+}
+
+func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
+	return a.exportedStaticPackages
 }
 
 var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
@@ -279,6 +302,17 @@
 		BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
 		ctx.CheckbuildFile(a.aarFile)
 	}
+
+	ctx.VisitDirectDeps(func(m android.Module) {
+		if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
+			a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
+			a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
+			a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
+		}
+	})
+
+	a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
+	a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
 }
 
 func AndroidLibraryFactory() android.Module {
@@ -316,9 +350,12 @@
 
 	properties AARImportProperties
 
-	classpathFile android.WritablePath
-	proguardFlags android.WritablePath
-	exportPackage android.WritablePath
+	classpathFile         android.WritablePath
+	proguardFlags         android.WritablePath
+	exportPackage         android.WritablePath
+	extraAaptPackagesFile android.WritablePath
+
+	exportedStaticPackages android.Paths
 }
 
 var _ AndroidLibraryDependency = (*AARImport)(nil)
@@ -327,6 +364,14 @@
 	return a.exportPackage
 }
 
+func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
+	return android.Paths{a.proguardFlags}
+}
+
+func (a *AARImport) ExportedStaticPackages() android.Paths {
+	return a.exportedStaticPackages
+}
+
 func (a *AARImport) Prebuilt() *android.Prebuilt {
 	return &a.prebuilt
 }
@@ -343,7 +388,7 @@
 		}
 	}
 
-	ctx.AddDependency(ctx.Module(), staticLibTag, a.properties.Libs...)
+	ctx.AddDependency(ctx.Module(), libTag, a.properties.Libs...)
 	ctx.AddDependency(ctx.Module(), staticLibTag, a.properties.Static_libs...)
 }
 
@@ -391,6 +436,7 @@
 	srcJar := android.PathForModuleGen(ctx, "R.jar")
 	proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
 	rTxt := android.PathForModuleOut(ctx, "R.txt")
+	a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
 
 	var linkDeps android.Paths
 
@@ -403,14 +449,14 @@
 	linkFlags = append(linkFlags, "--manifest "+manifest.String())
 	linkDeps = append(linkDeps, manifest)
 
-	staticLibs, libDeps, libFlags := aaptLibs(ctx, String(a.properties.Sdk_version))
+	transitiveStaticLibs, libDeps, libFlags := aaptLibs(ctx, String(a.properties.Sdk_version))
 
 	linkDeps = append(linkDeps, libDeps...)
 	linkFlags = append(linkFlags, libFlags...)
 
-	overlayRes := append(android.Paths{flata}, staticLibs...)
+	overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
 
-	aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt,
+	aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
 		linkFlags, linkDeps, nil, overlayRes)
 }
 
diff --git a/java/androidmk.go b/java/androidmk.go
index fc93cfb..b168f2c 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -135,6 +135,7 @@
 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", prebuilt.classpathFile.String())
 				fmt.Fprintln(w, "LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE :=", prebuilt.exportPackage.String())
 				fmt.Fprintln(w, "LOCAL_SOONG_EXPORT_PROGUARD_FLAGS :=", prebuilt.proguardFlags.String())
+				fmt.Fprintln(w, "LOCAL_SOONG_STATIC_LIBRARY_EXTRA_PACKAGES :=", prebuilt.extraAaptPackagesFile.String())
 				fmt.Fprintln(w, "LOCAL_SDK_VERSION :=", String(prebuilt.properties.Sdk_version))
 			},
 		},
@@ -243,8 +244,10 @@
 		}
 
 		fmt.Fprintln(w, "LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE :=", a.exportPackage.String())
+		fmt.Fprintln(w, "LOCAL_SOONG_STATIC_LIBRARY_EXTRA_PACKAGES :=", a.extraAaptPackagesFile.String())
 		fmt.Fprintln(w, "LOCAL_FULL_MANIFEST_FILE :=", a.manifestPath.String())
-		fmt.Fprintln(w, "LOCAL_SOONG_EXPORT_PROGUARD_FLAGS :=", a.proguardOptionsFile.String())
+		fmt.Fprintln(w, "LOCAL_SOONG_EXPORT_PROGUARD_FLAGS :=",
+			strings.Join(a.exportedProguardFlagFiles.Strings(), " "))
 		fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
 		fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
 	})
diff --git a/java/app.go b/java/app.go
index dd83a0a..ae0592a 100644
--- a/java/app.go
+++ b/java/app.go
@@ -63,6 +63,14 @@
 	appProperties appProperties
 }
 
+func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
+	return nil
+}
+
+func (a *AndroidApp) ExportedStaticPackages() android.Paths {
+	return nil
+}
+
 var _ AndroidLibraryDependency = (*AndroidApp)(nil)
 
 type certificate struct {
@@ -116,8 +124,17 @@
 	// apps manifests are handled by aapt, don't let Module see them
 	a.properties.Manifest = nil
 
-	a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
-		a.proguardOptionsFile)
+	var staticLibProguardFlagFiles android.Paths
+	ctx.VisitDirectDeps(func(m android.Module) {
+		if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
+			staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
+		}
+	})
+
+	staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
+
+	a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
+	a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
 
 	if ctx.ModuleName() != "framework-res" {
 		a.Module.compile(ctx, a.aaptSrcJar)
diff --git a/java/config/config.go b/java/config/config.go
index 3d7f910..6633f79 100644
--- a/java/config/config.go
+++ b/java/config/config.go
@@ -85,6 +85,7 @@
 	pctx.SourcePathVariable("GenKotlinBuildFileCmd", "build/soong/scripts/gen-kotlin-build-file.sh")
 
 	pctx.SourcePathVariable("JarArgsCmd", "build/soong/scripts/jar-args.sh")
+	pctx.HostBinToolVariable("ExtractJarPackagesCmd", "extract_jar_packages")
 	pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
 	pctx.HostBinToolVariable("MergeZipsCmd", "merge_zips")
 	pctx.HostBinToolVariable("Zip2ZipCmd", "zip2zip")
diff --git a/java/config/makevars.go b/java/config/makevars.go
index 5210f20..27c7daa 100644
--- a/java/config/makevars.go
+++ b/java/config/makevars.go
@@ -77,4 +77,6 @@
 
 	ctx.Strict("JACOCO_CLI_JAR", "${JacocoCLIJar}")
 	ctx.Strict("DEFAULT_JACOCO_EXCLUDE_FILTER", strings.Join(DefaultJacocoExcludeFilter, ","))
+
+	ctx.Strict("EXTRACT_JAR_PACKAGES", "${ExtractJarPackagesCmd}")
 }
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 2396467..703401c 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -31,6 +31,7 @@
 var (
 	sdkStubsLibrarySuffix = ".stubs"
 	sdkSystemApiSuffix    = ".system"
+	sdkTestApiSuffix      = ".test"
 	sdkDocsSuffix         = ".docs"
 	sdkImplLibrarySuffix  = ".impl"
 	sdkXmlFileSuffix      = ".xml"
@@ -44,6 +45,15 @@
 var (
 	publicApiStubsTag = dependencyTag{name: "public"}
 	systemApiStubsTag = dependencyTag{name: "system"}
+	testApiStubsTag   = dependencyTag{name: "test"}
+)
+
+type apiScope int
+
+const (
+	apiScopePublic apiScope = iota
+	apiScopeSystem
+	apiScopeTest
 )
 
 var (
@@ -82,6 +92,9 @@
 	// or .aidl files.
 	Srcs []string `android:"arch_variant"`
 
+	// list of optional source files that are part of API but not part of runtime library.
+	Api_srcs []string `android:"arch_variant"`
+
 	// list of of java libraries that will be in the classpath
 	Libs []string `android:"arch_variant"`
 
@@ -92,6 +105,9 @@
 	// list of package names that will be documented and publicized as API
 	Api_packages []string
 
+	// list of package names that must be hidden from the API
+	Hidden_api_packages []string
+
 	// TODO: determines whether to create HTML doc or not
 	//Html_doc *bool
 }
@@ -100,16 +116,19 @@
 	android.ModuleBase
 	android.DefaultableModuleBase
 
-	properties sdkLibraryProperties
+	properties       sdkLibraryProperties
+	deviceProperties CompilerDeviceProperties
 
 	publicApiStubsPath android.Paths
 	systemApiStubsPath android.Paths
+	testApiStubsPath   android.Paths
 }
 
 func (module *sdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
 	// Add dependencies to the stubs library
-	ctx.AddDependency(ctx.Module(), publicApiStubsTag, module.stubsName(false))
-	ctx.AddDependency(ctx.Module(), systemApiStubsTag, module.stubsName(true))
+	ctx.AddDependency(ctx.Module(), publicApiStubsTag, module.stubsName(apiScopePublic))
+	ctx.AddDependency(ctx.Module(), systemApiStubsTag, module.stubsName(apiScopeSystem))
+	ctx.AddDependency(ctx.Module(), testApiStubsTag, module.stubsName(apiScopeTest))
 }
 
 func (module *sdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
@@ -126,6 +145,8 @@
 				module.publicApiStubsPath = stubs.HeaderJars()
 			case systemApiStubsTag:
 				module.systemApiStubsPath = stubs.HeaderJars()
+			case testApiStubsTag:
+				module.testApiStubsPath = stubs.HeaderJars()
 			default:
 				ctx.ModuleErrorf("depends on module %q of unknown tag %q", otherName, tag)
 			}
@@ -148,19 +169,25 @@
 }
 
 // Module name of the stubs library
-func (module *sdkLibrary) stubsName(forSystemApi bool) string {
+func (module *sdkLibrary) stubsName(apiScope apiScope) string {
 	stubsName := module.BaseModuleName() + sdkStubsLibrarySuffix
-	if forSystemApi {
+	switch apiScope {
+	case apiScopeSystem:
 		stubsName = stubsName + sdkSystemApiSuffix
+	case apiScopeTest:
+		stubsName = stubsName + sdkTestApiSuffix
 	}
 	return stubsName
 }
 
 // Module name of the docs
-func (module *sdkLibrary) docsName(forSystemApi bool) string {
+func (module *sdkLibrary) docsName(apiScope apiScope) string {
 	docsName := module.BaseModuleName() + sdkDocsSuffix
-	if forSystemApi {
+	switch apiScope {
+	case apiScopeSystem:
 		docsName = docsName + sdkSystemApiSuffix
+	case apiScopeTest:
+		docsName = docsName + sdkTestApiSuffix
 	}
 	return docsName
 }
@@ -191,10 +218,15 @@
 // SDK version that the stubs library is built against. Note that this is always
 // *current. Older stubs library built with a numberd SDK version is created from
 // the prebuilt jar.
-func (module *sdkLibrary) sdkVersion(forSystemApi bool) string {
-	if forSystemApi {
+func (module *sdkLibrary) sdkVersion(apiScope apiScope) string {
+	switch apiScope {
+	case apiScopePublic:
+		return "current"
+	case apiScopeSystem:
 		return "system_current"
-	} else {
+	case apiScopeTest:
+		return "test_current"
+	default:
 		return "current"
 	}
 }
@@ -202,10 +234,13 @@
 // $(INTERNAL_PLATFORM_<apiTagName>_API_FILE) points to the generated
 // api file for the current source
 // TODO: remove this when apicheck is done in soong
-func (module *sdkLibrary) apiTagName(forSystemApi bool) string {
+func (module *sdkLibrary) apiTagName(apiScope apiScope) string {
 	apiTagName := strings.Replace(strings.ToUpper(module.BaseModuleName()), ".", "_", -1)
-	if forSystemApi {
+	switch apiScope {
+	case apiScopeSystem:
 		apiTagName = apiTagName + "_SYSTEM"
+	case apiScopeTest:
+		apiTagName = apiTagName + "_TEST"
 	}
 	return apiTagName
 }
@@ -213,11 +248,14 @@
 // returns the path (relative to this module) to the API txt file. Files are located
 // ./<api_dir>/<api_level>.txt where <api_level> is either current, system-current, removed,
 // or system-removed.
-func (module *sdkLibrary) apiFilePath(apiLevel string, forSystemApi bool) string {
+func (module *sdkLibrary) apiFilePath(apiLevel string, apiScope apiScope) string {
 	apiDir := "api"
 	apiFile := apiLevel
-	if forSystemApi {
+	switch apiScope {
+	case apiScopeSystem:
 		apiFile = "system-" + apiFile
+	case apiScopeTest:
+		apiFile = "test-" + apiFile
 	}
 	apiFile = apiFile + ".txt"
 
@@ -225,7 +263,7 @@
 }
 
 // Creates a static java library that has API stubs
-func (module *sdkLibrary) createStubsLibrary(mctx android.TopDownMutatorContext, forSystemApi bool) {
+func (module *sdkLibrary) createStubsLibrary(mctx android.TopDownMutatorContext, apiScope apiScope) {
 	props := struct {
 		Name              *string
 		Srcs              []string
@@ -243,10 +281,10 @@
 		}
 	}{}
 
-	props.Name = proptools.StringPtr(module.stubsName(forSystemApi))
+	props.Name = proptools.StringPtr(module.stubsName(apiScope))
 	// sources are generated from the droiddoc
-	props.Srcs = []string{":" + module.docsName(forSystemApi)}
-	props.Sdk_version = proptools.StringPtr(module.sdkVersion(forSystemApi))
+	props.Srcs = []string{":" + module.docsName(apiScope)}
+	props.Sdk_version = proptools.StringPtr(module.sdkVersion(apiScope))
 	// Unbundled apps will use the prebult one from /prebuilts/sdk
 	props.Product_variables.Unbundled_build.Enabled = proptools.BoolPtr(false)
 	props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
@@ -264,7 +302,7 @@
 
 // Creates a droiddoc module that creates stubs source files from the given full source
 // files
-func (module *sdkLibrary) createDocs(mctx android.TopDownMutatorContext, forSystemApi bool) {
+func (module *sdkLibrary) createDocs(mctx android.TopDownMutatorContext, apiScope apiScope) {
 	props := struct {
 		Name                    *string
 		Srcs                    []string
@@ -280,17 +318,22 @@
 		Removed_api_filename    *string
 	}{}
 
-	props.Name = proptools.StringPtr(module.docsName(forSystemApi))
-	props.Srcs = module.properties.Srcs
+	props.Name = proptools.StringPtr(module.docsName(apiScope))
+	props.Srcs = append(props.Srcs, module.properties.Srcs...)
+	props.Srcs = append(props.Srcs, module.properties.Api_srcs...)
 	props.Custom_template = proptools.StringPtr("droiddoc-templates-sdk")
 	props.Installable = proptools.BoolPtr(false)
 	props.Libs = module.properties.Libs
 
 	droiddocArgs := " -hide 110 -hide 111 -hide 113 -hide 121 -hide 125 -hide 126 -hide 127 -hide 128" +
 		" -stubpackages " + strings.Join(module.properties.Api_packages, ":") +
+		" " + android.JoinWithPrefix(module.properties.Hidden_api_packages, "-hidePackage ") +
 		" -nodocs"
-	if forSystemApi {
+	switch apiScope {
+	case apiScopeSystem:
 		droiddocArgs = droiddocArgs + " -showAnnotation android.annotation.SystemApi"
+	case apiScopeTest:
+		droiddocArgs = droiddocArgs + " -showAnnotation android.annotation.TestApi"
 	}
 	props.Args = proptools.StringPtr(droiddocArgs)
 
@@ -300,13 +343,17 @@
 	// TODO: If any incompatible change is detected, break the build
 	currentApiFileName := "current.txt"
 	removedApiFileName := "removed.txt"
-	if forSystemApi {
+	switch apiScope {
+	case apiScopeSystem:
 		currentApiFileName = "system-" + currentApiFileName
 		removedApiFileName = "system-" + removedApiFileName
+	case apiScopeTest:
+		currentApiFileName = "test-" + currentApiFileName
+		removedApiFileName = "test-" + removedApiFileName
 	}
 	currentApiFileName = path.Join("api", currentApiFileName)
 	removedApiFileName = path.Join("api", removedApiFileName)
-	props.Api_tag_name = proptools.StringPtr(module.apiTagName(forSystemApi))
+	props.Api_tag_name = proptools.StringPtr(module.apiTagName(apiScope))
 	// Note: the exact names of these two are not important because they are always
 	// referenced by the make variable $(INTERNAL_PLATFORM_<TAG_NAME>_API_FILE)
 	props.Api_filename = proptools.StringPtr(currentApiFileName)
@@ -316,16 +363,12 @@
 	// API class is extending from the framework class. In that case, doclava needs
 	// to know whether the base class is hidden or not. Since that information is
 	// encoded as @hide string in the comment, we need source files for the classes,
-	// not the compiled ones. Also there are rare cases where part of SDK library is
-	// implemented in the framework (e.g. org.apache.http.legacy). In that case,
-	// we need framework source to make API stubs, though the sources are not
-	// required to build the runtime library.
+	// not the compiled ones.
 	props.Srcs_lib = proptools.StringPtr("framework")
 	props.Srcs_lib_whitelist_dirs = []string{"core/java"}
-	props.Srcs_lib_whitelist_pkgs = module.properties.Api_packages
 	// Add android.annotation package to give access to the framework-defined
 	// annotations such as SystemApi, NonNull, etc.
-	props.Srcs_lib_whitelist_pkgs = append(props.Srcs_lib_whitelist_pkgs, "android.annotation")
+	props.Srcs_lib_whitelist_pkgs = []string{"android.annotation"}
 	// These libs are required by doclava to parse the framework sources add via
 	// Src_lib and Src_lib_whitelist_* properties just above.
 	// If we don't add them to the classpath, errors messages are generated by doclava,
@@ -363,7 +406,7 @@
 		props.Product_specific = proptools.BoolPtr(true)
 	}
 
-	mctx.CreateModule(android.ModuleFactoryAdaptor(LibraryFactory(true)), &props)
+	mctx.CreateModule(android.ModuleFactoryAdaptor(LibraryFactory(true)), &props, &module.deviceProperties)
 }
 
 // Creates the xml file that publicizes the runtime library
@@ -447,13 +490,24 @@
 // once for public API level and once for system API level
 func sdkLibraryMutator(mctx android.TopDownMutatorContext) {
 	if module, ok := mctx.Module().(*sdkLibrary); ok {
+		if module.properties.Srcs == nil {
+			mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
+		}
+		if module.properties.Api_packages == nil {
+			mctx.PropertyErrorf("api_packages", "java_sdk_library must specify api_packages")
+		}
+
 		// for public API stubs
-		module.createStubsLibrary(mctx, false)
-		module.createDocs(mctx, false)
+		module.createStubsLibrary(mctx, apiScopePublic)
+		module.createDocs(mctx, apiScopePublic)
 
 		// for system API stubs
-		module.createStubsLibrary(mctx, true)
-		module.createDocs(mctx, true)
+		module.createStubsLibrary(mctx, apiScopeSystem)
+		module.createDocs(mctx, apiScopeSystem)
+
+		// for test API stubs
+		module.createStubsLibrary(mctx, apiScopeTest)
+		module.createDocs(mctx, apiScopeTest)
 
 		// for runtime
 		module.createXmlFile(mctx)
@@ -470,6 +524,7 @@
 func sdkLibraryFactory() android.Module {
 	module := &sdkLibrary{}
 	module.AddProperties(&module.properties)
+	module.AddProperties(&module.deviceProperties)
 	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
 	android.InitDefaultableModule(module)
 	return module
diff --git a/ui/build/config.go b/ui/build/config.go
index aa60b3f..5622dff 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -44,10 +44,11 @@
 	skipMake   bool
 
 	// From the product config
-	katiArgs     []string
-	ninjaArgs    []string
-	katiSuffix   string
-	targetDevice string
+	katiArgs        []string
+	ninjaArgs       []string
+	katiSuffix      string
+	targetDevice    string
+	targetDeviceDir string
 
 	brokenDupRules bool
 }
@@ -102,8 +103,10 @@
 		"OUT_DIR_COMMON_BASE",
 
 		// Variables that have caused problems in the past
+		"CDPATH",
 		"DISPLAY",
 		"GREP_OPTIONS",
+		"NDK_ROOT",
 
 		// Drop make flags
 		"MAKEFLAGS",
@@ -566,3 +569,11 @@
 func (c *configImpl) BuildBrokenDupRules() bool {
 	return c.brokenDupRules
 }
+
+func (c *configImpl) SetTargetDeviceDir(dir string) {
+	c.targetDeviceDir = dir
+}
+
+func (c *configImpl) TargetDeviceDir() string {
+	return c.targetDeviceDir
+}
diff --git a/ui/build/dumpvars.go b/ui/build/dumpvars.go
index eb43ed6..cc9e742 100644
--- a/ui/build/dumpvars.go
+++ b/ui/build/dumpvars.go
@@ -157,6 +157,9 @@
 		// To find target/product/<DEVICE>
 		"TARGET_DEVICE",
 
+		// So that later Kati runs can find BoardConfig.mk faster
+		"TARGET_DEVICE_DIR",
+
 		// Whether --werror_overriding_commands will work
 		"BUILD_BROKEN_DUP_RULES",
 	}, exportEnvVars...), BannerVars...)
@@ -182,6 +185,7 @@
 	config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"]))
 	config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"]))
 	config.SetTargetDevice(make_vars["TARGET_DEVICE"])
+	config.SetTargetDeviceDir(make_vars["TARGET_DEVICE_DIR"])
 
 	config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] != "false")
 }
diff --git a/ui/build/finder.go b/ui/build/finder.go
index 3bd6d87..6dc35a0 100644
--- a/ui/build/finder.go
+++ b/ui/build/finder.go
@@ -56,7 +56,14 @@
 		RootDirs:         []string{"."},
 		ExcludeDirs:      []string{".git", ".repo"},
 		PruneFiles:       pruneFiles,
-		IncludeFiles:     []string{"Android.mk", "Android.bp", "Blueprints", "CleanSpec.mk", "TEST_MAPPING"},
+		IncludeFiles: []string{
+			"Android.mk",
+			"AndroidProducts.mk",
+			"Android.bp",
+			"Blueprints",
+			"CleanSpec.mk",
+			"TEST_MAPPING",
+		},
 	}
 	dumpDir := config.FileListDir()
 	f, err = finder.New(cacheParams, filesystem, logger.New(ioutil.Discard),
@@ -81,8 +88,16 @@
 		ctx.Fatalf("Could not export module list: %v", err)
 	}
 
+	androidProductsMks := f.FindNamedAt("device", "AndroidProducts.mk")
+	androidProductsMks = append(androidProductsMks, f.FindNamedAt("vendor", "AndroidProducts.mk")...)
+	androidProductsMks = append(androidProductsMks, f.FindNamedAt("product", "AndroidProducts.mk")...)
+	err = dumpListToFile(androidProductsMks, filepath.Join(dumpDir, "AndroidProducts.mk.list"))
+	if err != nil {
+		ctx.Fatalf("Could not export product list: %v", err)
+	}
+
 	cleanSpecs := f.FindFirstNamedAt(".", "CleanSpec.mk")
-	dumpListToFile(cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
+	err = dumpListToFile(cleanSpecs, filepath.Join(dumpDir, "CleanSpec.mk.list"))
 	if err != nil {
 		ctx.Fatalf("Could not export module list: %v", err)
 	}
diff --git a/ui/build/kati.go b/ui/build/kati.go
index a73ebdf..81edd32 100644
--- a/ui/build/kati.go
+++ b/ui/build/kati.go
@@ -94,7 +94,8 @@
 	args = append(args,
 		"BUILDING_WITH_NINJA=true",
 		"SOONG_ANDROID_MK="+config.SoongAndroidMk(),
-		"SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk())
+		"SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
+		"TARGET_DEVICE_DIR="+config.TargetDeviceDir())
 
 	if config.UseGoma() {
 		args = append(args, "-j"+strconv.Itoa(config.Parallel()))
@@ -196,17 +197,22 @@
 		"--werror_find_emulator",
 		"--werror_overriding_commands",
 		"--use_find_emulator",
+		"--kati_stats",
 		"-f", "build/make/core/cleanbuild.mk",
 		"BUILDING_WITH_NINJA=true",
 		"SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
+		"TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
 	}
 
 	cmd := Command(ctx, config, "ckati", executable, args...)
 	cmd.Sandbox = katiCleanSpecSandbox
-	cmd.Stdout = ctx.Stdout()
-	cmd.Stderr = ctx.Stderr()
+	pipe, err := cmd.StdoutPipe()
+	if err != nil {
+		ctx.Fatalln("Error getting output pipe for ckati:", err)
+	}
+	cmd.Stderr = cmd.Stdout
 
-	// Kati leaks memory, so ensure leak detection is turned off
-	cmd.Environment.Set("ASAN_OPTIONS", "detect_leaks=0")
-	cmd.RunOrFatal()
+	cmd.StartOrFatal()
+	katiRewriteOutput(ctx, pipe)
+	cmd.WaitOrFatal()
 }