To ensure your app uses dynamically loaded SO libraries instead of the ones bundled in the APK, you can exclude them during the build process using one of two methods.
Method 1: Using packagingOptions
Add exclude rules to your packagingOptions in the build.gradle file to prevent specific architectures or libraries from being packaged.
Method 2: Using a custom Gradle task
You can use a custom task to delete specific SO files from the merged native libs directory. This allows for more granular control, such as matching partial names.
Note: The provided task example uses an ext block to define deleteSoName and hooks into the mergeDebugNativeLibs and stripDebugDebugSymbols tasks via afterEvaluate to ensure the deletion happens at the correct stage of the build lifecycle.
ext {
deleteSoName = ["libnativecpptwo.so","libnativecpp.so"]
}
task(dynamicSo) {
// ... logic to delete files matching deleteSoName from build/intermediates/merged_native_libs/debug/out/lib
}.doLast {
// ...
}
afterEvaluate {
def customer = tasks.findByName("dynamicSo")
def merge = tasks.findByName("mergeDebugNativeLibs")
def strip = tasks.findByName("stripDebugDebugSymbols")
if (merge != null || strip != null) {
customer.mustRunAfter(merge)
strip.dependsOn(customer)
}
}