Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cc203505 authored by Pedro Loureiro's avatar Pedro Loureiro
Browse files

Add module-lib to droidstubs' supported sdk types

If the android.jar is not found in the module-lib folder, it falls back
to searching it in the system folder and then falls back on the public
folder.

This only brings the functionality, we are not yet using it.

Test: m nothing
Bug: 202023154
Change-Id: I99c9004740c8e51faf53ebc3ba2444e8164215f3
parent 2df9ebf9
Loading
Loading
Loading
Loading
+20 −12
Original line number Diff line number Diff line
@@ -25,6 +25,9 @@ import (
	"android/soong/remoteexec"
)

// The values allowed for Droidstubs' Api_levels_sdk_type
var allowedApiLevelSdkTypes = []string{"public", "system", "module-lib"}

func init() {
	RegisterStubsBuildComponents(android.InitRegistrationContext)
}
@@ -134,7 +137,7 @@ type DroidstubsProperties struct {
	// the dirs which Metalava extracts API levels annotations from.
	Api_levels_annotations_dirs []string

	// the sdk kind which Metalava extracts API levels annotations from. Supports 'public' and 'system' for now; defaults to public.
	// the sdk kind which Metalava extracts API levels annotations from. Supports 'public', 'system' and 'module-lib' for now; defaults to public.
	Api_levels_sdk_type *string

	// the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
@@ -404,19 +407,24 @@ func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *a
	// When parsing a stub jar for a specific version, Metalava picks the first pattern that defines
	// an actual file present on disk (in the order the patterns were passed). For system APIs for
	// privileged apps that are only defined since API level 21 (Lollipop), fallback to public stubs
	// for older releases.
	if sdkType := proptools.StringDefault(d.properties.Api_levels_sdk_type, "public"); sdkType != "public" {
		if sdkType != "system" {
			ctx.PropertyErrorf("api_levels_sdk_type", "only 'public' and 'system' are supported")
	// for older releases. Similarly, module-lib falls back to system API.
	var sdkDirs []string
	switch proptools.StringDefault(d.properties.Api_levels_sdk_type, "public") {
	case "module-lib":
		sdkDirs = []string{"module-lib", "system", "public"}
	case "system":
		sdkDirs = []string{"system", "public"}
	case "public":
		sdkDirs = []string{"public"}
	default:
		ctx.PropertyErrorf("api_levels_sdk_type", "needs to be one of %v", allowedApiLevelSdkTypes)
		return
	}
		// If building non public stubs, add all sdkType patterns first...

	for _, sdkDir := range sdkDirs {
		for _, dir := range dirs {
			cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkType, filename))
		}
			cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkDir, filename))
		}
	for _, dir := range dirs {
		// ... and fallback to public ones, for Metalava to use if needed.
		cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, "public", filename))
	}
}

+36 −6
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
package java

import (
	"fmt"
	"reflect"
	"regexp"
	"strings"
@@ -82,8 +83,10 @@ func TestDroidstubs(t *testing.T) {
	}
}

func TestSystemDroidstubs(t *testing.T) {
	ctx, _ := testJavaWithFS(t, `
// runs a test for droidstubs with a customizable sdkType argument and returns
// the list of jar patterns that is passed as `--android-jar-pattern`
func getAndroidJarPatternsForDroidstubs(t *testing.T, sdkType string) []string {
	ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
		droiddoc_exported_dir {
			name: "some-exported-dir",
			path: "somedir",
@@ -102,9 +105,9 @@ func TestSystemDroidstubs(t *testing.T) {
				"some-other-exported-dir",
			],
			api_levels_annotations_enabled: true,
            api_levels_sdk_type: "system",
      api_levels_sdk_type: "%s",
		}
		`,
		`, sdkType),
		map[string][]byte{
			"foo-doc/a.java": nil,
		})
@@ -113,13 +116,40 @@ func TestSystemDroidstubs(t *testing.T) {
	manifest := m.Output("metalava.sbox.textproto")
	cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command)
	r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`)
	matches := r.FindAllString(cmd, -1)
	return r.FindAllString(cmd, -1)
}

func TestPublicDroidstubs(t *testing.T) {
	patterns := getAndroidJarPatternsForDroidstubs(t, "public")

	android.AssertArrayString(t, "order of patterns", []string{
		"--android-jar-pattern somedir/%/public/android.jar",
		"--android-jar-pattern someotherdir/%/public/android.jar",
	}, patterns)
}

func TestSystemDroidstubs(t *testing.T) {
	patterns := getAndroidJarPatternsForDroidstubs(t, "system")

	android.AssertArrayString(t, "order of patterns", []string{
		"--android-jar-pattern somedir/%/system/android.jar",
		"--android-jar-pattern someotherdir/%/system/android.jar",
		"--android-jar-pattern somedir/%/public/android.jar",
		"--android-jar-pattern someotherdir/%/public/android.jar",
	}, patterns)
}

func TestModuleLibDroidstubs(t *testing.T) {
	patterns := getAndroidJarPatternsForDroidstubs(t, "module-lib")

	android.AssertArrayString(t, "order of patterns", []string{
		"--android-jar-pattern somedir/%/module-lib/android.jar",
		"--android-jar-pattern someotherdir/%/module-lib/android.jar",
		"--android-jar-pattern somedir/%/system/android.jar",
		"--android-jar-pattern someotherdir/%/system/android.jar",
		"--android-jar-pattern somedir/%/public/android.jar",
		"--android-jar-pattern someotherdir/%/public/android.jar",
	}, matches)
	}, patterns)
}

func TestDroidstubsSandbox(t *testing.T) {