How to replace files in an existing directory in BUNDLE-METADATA folder in Android App Bundles (.aab)
file?
For desymbolication of native crashes, we want to replace the existing .dbg file in the path="/BUNDLE-METADATA/com.android.tools.build.debugsymbols/arm64-v8a
with the file we receive from other sources. These existing .dbg is created during build process. For some reason, using the .dbg created during the build is not able to help with desymboliction both locally as well as in play dev console. Only when we upload the original .so file to play dev console, the desymbolication happens correctly.
This API seems to work only for adding new files and directory. https://developer.android.com/reference/tools/gradle-api/8.3/null/com/android/build/api/variant/BundleConfig
Is it possible to replace existing files using AGP 7.3
?
In your scenario, you have an Android App Bundle (
.aab
) file and within it, there is a specific.dbg
file located in theBUNDLE-METADATA/com.android.tools.build.debugsymbols/arm64-v8a
directory.Your goal would be to replace this existing
.dbg
file with another one from a different source to aid in desymbolication of native crashes, as the current.dbg
file is not serving its purpose.I do not see any support for that.
Modifying a file in an existing Android App Bundle (AAB) can indeed impact its signature, potentially rendering it invalid. When you create an AAB, it is signed with a key that ensures its integrity and authenticity. Any subsequent modifications to the AAB, such as replacing a file, would alter the bundle's content, thus changing its signature.
There is also an optional code signing and verification mechanism called "Code Transparency" for apps published with Android App Bundles. Although it operates independently of the signing scheme used for app bundles and APKs, it emphasizes the importance of signing as a verification mechanism.
In that case, the optimal approach would be to replace the
.dbg
file just before the final packaging and signing of the app bundle occurs. The integrity and the signature of the app bundle remain intact.Build Pipeline Steps:
Build your app as usual up until the point where the
.dbg
file is generated.Replace the generated
.dbg
file with the external.dbg
file you have (script or simple cp step):That could be done through a custom gradle task or by writing a Gradle plugin
Continue with the final packaging and signing** of the app bundle. The
BUNDLE-METADATA
folder and its contents, including the replaced.dbg
file, would then be packaged and signed as part of the normal build process.That way, you make sure the replacement of the
.dbg
file happens before the final packaging and signing of the app bundle, thus maintaining the integrity and the signature of the app bundle.For instance: extending the Android Gradle Plugin (AGP) could include the "replacing files before the final packaging of the app bundle" step.
The new task named
replaceDbgFile
uses the Groovyexecute()
method to run thecp
command, replacing the generated.dbg
file with the external.dbg
file.The
gradle.tasks.named('bundleRelease').configure { dependsOn replaceDbgFile }
line makes sure thereplaceDbgFile
task is executed before thebundleRelease
task, which packages and signs the app bundle.The OP Nitin Sethi adds in the comments:
Your
dependsOn
approach is a sound method to make sure the custom task executes at the appropriate point in the build process, specifically after theextractReleaseNativeDebugMetadata
task. That way, the required files are in place before proceeding to the next steps in the build pipeline.See also "A Gradle quickie: properly using dependsOn", from Cédric Champeau.
For copying and removing files using the Groovy DSL within Gradle, there are more idiomatic ways that leverage Gradle's built-in task types and methods:
You can use Gradle's
Copy
task type to define a task for copying files.Use Gradle's
Delete
task type to define a task for deleting files.Make sure the copy and delete tasks are executed at the correct point in the build process.