Android应用构建耗时增加,如何优化构建速度?mergeDebugNativeLibs耗时突出
Hey there, let's tackle that slow build time issue you're facing—especially that massive :core:mergeDebugNativeLibs step eating up nearly 40 seconds of your build. Since you only use NDK via Realm, here are some targeted optimizations to speed things up:
1. Trim Unnecessary ABIs from Realm's Native Libraries
Realm by default includes native libraries (.so files) for multiple CPU architectures, which means the mergeDebugNativeLibs task has to process way more files than you probably need. You can restrict it to only the ABIs your target users actually use by adding this to your :core module's build.gradle:
android { defaultConfig { ndk { // Keep only the architectures relevant to your user base // Common choices: arm64-v8a (modern devices) + armeabi-v7a (older 32-bit) abiFilters 'arm64-v8a', 'armeabi-v7a' } } }
This will drastically reduce the number of files the merge task has to handle, which should cut down that 39-second runtime significantly.
2. Enable Gradle's Build Optimizations
Even if you're using --offline, making sure these Gradle properties are set in your project's gradle.properties can help with overall build efficiency, including caching the mergeDebugNativeLibs task when nothing changes:
# Enable parallel builds for multi-module projects org.gradle.parallel=true # Enable build caching to reuse outputs of previous tasks org.gradle.caching=true # Only configure modules that are actually needed for the current build org.gradle.configureondemand=true
3. Upgrade Realm and Android Gradle Plugin (AGP)
Older versions of Realm or AGP might have inefficiencies in how native libraries are processed. Upgrading to the latest stable versions of both can often bring performance improvements to tasks like mergeDebugNativeLibs.
4. Audit Dependencies for Unwanted Native Libraries
Sometimes other third-party dependencies might sneak in extra .so files without you realizing it. Run this command to check your :core module's dependency tree:
./gradlew :core:dependencies
Look for any dependencies that include native libraries you don't need, and exclude them using exclude in your build.gradle dependency declaration. For example:
implementation("some-dependency:1.0.0") { exclude group: 'com.unwanted', module: 'native-lib' }
5. Optimize Packaging Options
If there are duplicate or unnecessary .so files being included, you can tweak the packaging options to clean things up. Add this to your :core module's build.gradle:
android { packagingOptions { // Pick the first occurrence of duplicate .so files to avoid conflicts pickFirst 'lib/**/*.so' // Exclude any specific unused .so files you've identified exclude 'lib/x86/unneeded-lib.so' } }
Just be careful here—make sure you don't exclude any libraries Realm needs to function properly.
Start with the ABI filtering step first, as it's the most direct fix for your mergeDebugNativeLibs bottleneck. Then work through the other suggestions to squeeze out more build speed.
内容的提问来源于stack exchange,提问作者Allen

