美文网首页
Android加快Gradle编译速度(Kotlin)

Android加快Gradle编译速度(Kotlin)

作者: 会超能力的橙子 | 来源:发表于2019-05-20 14:15 被阅读0次

    做以下几点配置:

    1. 在项目根目录下新建gradle.properties文件(没有的话),修改内容如下:
    # Project-wide Gradle settings.
    # IDE (e.g. Android Studio) users:
    # Gradle settings configured through the IDE *will override*
    # any settings specified in this file.
    # For more details on how to configure your build environment visit
    # http://www.gradle.org/docs/current/userguide/build_environment.html
    # Specifies the JVM arguments used for the daemon process.
    # The setting is particularly useful for tweaking memory settings.
    # org.gradle.jvmargs=-Xmx1536m
    # 开启gradle并行编译,开启daemon,调整jvm内存大小
    org.gradle.daemon=true
    org.gradle.configureondemand=true
    org.gradle.parallel=true
    org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    # When configured, Gradle will run in incubating parallel mode.
    # This option should only be used with decoupled projects. More details, visit
    # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
    # org.gradle.parallel=true
    # AndroidX package structure to make it clearer which packages are bundled with the
    # Android operating system, and which are packaged with your app's APK
    # https://developer.android.com/topic/libraries/support-library/androidx-rn
    android.useAndroidX=true
    # Automatically convert third-party libraries to use AndroidX
    android.enableJetifier=true
    # Kotlin code style for this project: "official" or "obsolete":
    kotlin.code.style=official
    # 开启gradle缓存
    org.gradle.caching=true
    android.enableBuildCache=true
    # 开启kotlin的增量和并行编译
    kotlin.incremental=true
    kotlin.incremental.java=true
    kotlin.incremental.js=true
    kotlin.caching.enabled=true
    # 开启kotlin并行编译
    kotlin.parallel.tasks.in.project=true
    # 优化kapt
    # 并行运行kapt1.2.60版本以上支持
    kapt.use.worker.api=true
    # 增量编译 kapt1.3.30版本以上支持
    kapt.incremental.apt=true
    # kapt avoiding 如果用kapt依赖的内容没有变化,会完全重用编译内容,省掉`app:kaptGenerateStubsDebugKotlin`的时间
    kapt.include.compile.classpath=false
    

    最后在app级别下的build.gradle下添加如下内容:

    android闭包下添加:

    android {
    
        // 优化编译速度
        // 优化`transformClassDexBuilderForDebug`的速度
        dexOptions {
            preDexLibraries true
            maxProcessCount 8
        }
    
        // 优化版本控制 release版本使用动态版本号 debug版本使用固定版本号
        // 影响不大 Cannot set the value of read-only property 'versionName' for object of ... ?
    //    applicationVariants.all { variant ->
    //        if (variant.buildType.name == "release") {
    //            versionName = currentName()
    //            versionCode = currentCode()
    //        }
    //    }
    ...
    }
    

    添加kapt闭包

    // 优化编译速度 如果有用到kapt添加如下配置
    kapt {
        useBuildCache = true
        javacOptions {
            option("-Xmaxerrs", 500)
        }
    }
    

    至此便完成了Kotlin下Gradle的编译速度的优化。

    相关文章

      网友评论

          本文标题:Android加快Gradle编译速度(Kotlin)

          本文链接:https://www.haomeiwen.com/subject/bulkzqtx.html