Android编译优化

厌烦运行卡顿、编译耗时过长等问题,特写本片文章以作记录。废话不多说,直接上内容。
本机环境(studio3.1.2, win10 64)

android studio配置

  1. studio64.exe.vmoptions配置
-Xms1g
-Xmx4g
-Xmn2g
-Xss1m
-XX:PermSize=64m
-XX:MaxPermSize=512m
-XX:ReservedCodeCacheSize=240m
-XX:+UseCompressedOops
-XX:+UseConcMarkSweepGC
-XX:+AggressiveOpts
-XX:SoftRefLRUPolicyMSPerMB=50

-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true
-Djna.nosys=true
-Djna.boot.library.path=

-da
  1. host配置
host文件中添加
127.0.0.1 search.maven.org

项目配置优化

  1. 项目根gradle.properties优化
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.caching=true
org.gradle.jvmargs=-Xms1024m -Xmx2048m -XX:MaxPermSize=768m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
android.enableBuildCache=true
android.enableAapt2=false
android.enableD8=true
  1. module的build.gradle优化
android {
    signingConfigs {
        demo {
            keyAlias 'demo'
            keyPassword 'demo'
            storeFile file('demo.keystore')
            storePassword 'demo'
        }
    }

    dexOptions {
        //最大栈内存
        javaMaxHeapSize "4g"
        //预编译
        preDexLibraries = true
        //最大的线程数
        maxProcessCount = 4
    }

    aaptOptions {
        // cruncherEnabled false //gradle3.1已弃用,在buildType中使用crunchPngs代替
        cruncherProcesses 0 //PNG优化线程
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    compileSdkVersion COMPILE_SDK_VERSION.toInteger()
    buildToolsVersion BUILD_TOOL_VERSION

    defaultConfig {
        applicationId "com.android.demo"
        minSdkVersion MIN_SDK_VERSION.toInteger()
        targetSdkVersion TARGET_SDK_VERSION.toInteger()
        versionCode version_code
        versionName version_name

        multiDexEnabled true
        flavorDimensions "versionCode"
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
                arguments = [moduleName: project.getName()]
            }
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        doNotStrip '*/mips/*.so'
        doNotStrip '*/mips64/*.so'
    }


    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

    buildTypes {

        debug {
            crunchPngs false //禁用PNG优化
            // minifyEnabled false //默认值false
            ndk {
                abiFilters "armeabi-v7a"
                abiFilters "arm64-v8a"
            }
            signingConfig signingConfigs.demo

            matchingFallbacks = ['release']
        }
    }   
}

你可能感兴趣的:(Android编译优化)