From 1bbd0626f2d83036b4e1225cc51edfea5e4055b7 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 14 May 2021 15:52:25 +0100 Subject: [PATCH] Make uncompressDex available for use through hiddenAPIModule In order for the bootclasspath_fragment to perform dex encoding on its contents it needs to know whether the dex file is uncompressed or not. This change makes that information available by passing it to initHiddenAPI, storing it in hiddenAPI struct and providing access through the hiddenAPIModule. Bug: 179354495 Test: m droid Change-Id: I913416b4836766de194203fd8ed5124b61dfa3dd --- java/base.go | 4 ++-- java/hiddenapi.go | 24 ++++++++++++++++++++++-- java/java.go | 6 +++--- java/sdk_library.go | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/java/base.go b/java/base.go index 4b56a300b..c828503d4 100644 --- a/java/base.go +++ b/java/base.go @@ -1218,10 +1218,10 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { } // Initialize the hiddenapi structure. - j.initHiddenAPI(ctx, dexOutputFile, j.implementationJarFile) + j.initHiddenAPI(ctx, dexOutputFile, j.implementationJarFile, j.dexProperties.Uncompress_dex) // Encode hidden API flags in dex file. - dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile, proptools.Bool(j.dexProperties.Uncompress_dex)) + dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile) // merge dex jar with resources if necessary if j.resourceJar != nil { diff --git a/java/hiddenapi.go b/java/hiddenapi.go index 04b8fc689..1e838247b 100644 --- a/java/hiddenapi.go +++ b/java/hiddenapi.go @@ -43,6 +43,10 @@ type hiddenAPI struct { // the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API // processing. classesJarPaths android.Paths + + // The compressed state of the dex file being encoded. This is used to ensure that the encoded + // dex file has the same state. + uncompressDexState *bool } func (h *hiddenAPI) bootDexJar() android.Path { @@ -53,6 +57,10 @@ func (h *hiddenAPI) classesJars() android.Paths { return h.classesJarPaths } +func (h *hiddenAPI) uncompressDex() *bool { + return h.uncompressDexState +} + // hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement. type hiddenAPIModule interface { android.Module @@ -62,12 +70,16 @@ type hiddenAPIModule interface { type hiddenAPIIntf interface { bootDexJar() android.Path classesJars() android.Paths + uncompressDex() *bool } var _ hiddenAPIIntf = (*hiddenAPI)(nil) // Initialize the hiddenapi structure -func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar, classesJar android.Path) { +// +// uncompressedDexState should be nil when the module is a prebuilt and so does not require hidden +// API encoding. +func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar, classesJar android.Path, uncompressedDexState *bool) { // Save the classes jars even if this is not active as they may be used by modular hidden API // processing. @@ -82,6 +94,8 @@ func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar, classesJar // hiddenAPISingletonPathsStruct.stubFlags file. h.bootDexJarPath = dexJar + h.uncompressDexState = uncompressedDexState + // If hiddenapi processing is disabled treat this as inactive. if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { return @@ -119,12 +133,18 @@ func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Modul // // Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi // flags and returns this instead of the supplied dex jar. -func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath, uncompressDex bool) android.OutputPath { +func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath) android.OutputPath { if !h.active { return dexJar } + // A nil uncompressDexState prevents the dex file from being encoded. + if h.uncompressDexState == nil { + ctx.ModuleErrorf("cannot encode dex file %s when uncompressDexState is nil", dexJar) + } + uncompressDex := *h.uncompressDexState + hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", dexJar.Base()).OutputPath // Create a copy of the dex jar which has been encoded with hiddenapi flags. diff --git a/java/java.go b/java/java.go index d4a52086e..45eb69339 100644 --- a/java/java.go +++ b/java/java.go @@ -1309,7 +1309,7 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.dexJarFile = dexOutputPath // Initialize the hiddenapi structure. - j.initHiddenAPI(ctx, dexOutputPath, outputFile) + j.initHiddenAPI(ctx, dexOutputPath, outputFile, nil) } else { // This should never happen as a variant for a prebuilt_apex is only created if the // prebuilt_apex has been configured to export the java library dex file. @@ -1341,10 +1341,10 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { } // Initialize the hiddenapi structure. - j.initHiddenAPI(ctx, dexOutputFile, outputFile) + j.initHiddenAPI(ctx, dexOutputFile, outputFile, j.dexProperties.Uncompress_dex) // Encode hidden API flags in dex file. - dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile, proptools.Bool(j.dexProperties.Uncompress_dex)) + dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile) j.dexJarFile = dexOutputFile } diff --git a/java/sdk_library.go b/java/sdk_library.go index 1866a538e..99eacf4e7 100644 --- a/java/sdk_library.go +++ b/java/sdk_library.go @@ -2121,7 +2121,7 @@ func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleCo di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo) if dexOutputPath := di.PrebuiltExportPath(module.BaseModuleName(), ".dexjar"); dexOutputPath != nil { module.dexJarFile = dexOutputPath - module.initHiddenAPI(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0]) + module.initHiddenAPI(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil) } else { // This should never happen as a variant for a prebuilt_apex is only created if the // prebuilt_apex has been configured to export the java library dex file.