Android Studio编译耗时太长

zwghvu4y  于 2023-06-27  发布在  Android
关注(0)|答案(2)|浏览(210)

我的Android Studio编译需要很长时间。最少10分钟,而且往往比这更长。
这始于Gradle插件升级到7.0.4
这是我的build.gradle在ap级别:

  1. buildscript {
  2. ext.kotlin_version = "1.6.0"
  3. repositories {
  4. google()
  5. jcenter()
  6. maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
  7. }
  8. dependencies {
  9. classpath 'com.android.tools.build:gradle:7.0.4'
  10. classpath 'com.google.gms:google-services:4.3.10'
  11. classpath "io.realm:realm-gradle-plugin:6.0.2"
  12. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  13. }
  14. }
  15. allprojects {
  16. repositories {
  17. google()
  18. jcenter()
  19. maven { url 'https://github.com/WickeDev/stetho-realm/raw/master/maven-repo' }
  20. maven { url 'https://jitpack.io' }
  21. maven {
  22. url 'https://maven.wefi.com/repository/wefi-release-repo'
  23. artifactUrls 'https://maven.wefi.com/repository/wefi-release-repo'
  24. }
  25. configurations.all {
  26. resolutionStrategy {
  27. force 'org.xerial:sqlite-jdbc:3.34.0'
  28. }
  29. }
  30. }
  31. }
  32. task clean(type: Delete) {
  33. delete rootProject.buildDir
  34. }

这是导致问题的库模块的build.gradle:

  1. apply plugin: 'com.android.library'
  2. apply plugin: 'kotlin-android'
  3. apply plugin: 'kotlin-kapt'
  4. apply plugin: 'kotlin-android-extensions'
  5. apply plugin: 'realm-android'
  6. apply from: '../../WeFiBuild/common/dependencies.gradle'
  7. ext.versionMajor = 0
  8. ext.versionMinor = 0
  9. ext.versionBuild = 0
  10. ext.versionRevision = 0
  11. ext.playStoreVersionCode = 0
  12. def computeVersionName() {
  13. def branch = gitBranch()
  14. if (branch == "master")
  15. return "${versionMajor}.${versionMinor}.${versionRevision}"
  16. else
  17. return "${versionMajor}.${versionMinor}.${versionRevision}.${versionBuild}.rc${versionBuild}"
  18. }
  19. def computeVersionCode() {
  20. computeBuildVersion()
  21. return playStoreVersionCode
  22. // return (versionMajor * 10_000_000) + (versionMinor * 100_000) + (versionBuild * 1_000) + versionRevision
  23. }
  24. def versionPropsFile = file('./../../WeFi/app/version.properties')
  25. ext.computeBuildVersion = {
  26. if (versionPropsFile.canRead()) {
  27. Properties versionProps = new Properties()
  28. versionProps.load(new FileInputStream(versionPropsFile))
  29. versionMajor = versionProps['VERSION_MAJOR'].toInteger()
  30. versionMinor = versionProps['VERSION_MINOR'].toInteger()
  31. versionBuild = versionProps['VERSION_BUILD'].toInteger()
  32. versionRevision = versionProps['VERSION_REVISION'].toInteger()
  33. } else {
  34. throw new FileNotFoundException("Could not read version.properties!")
  35. }
  36. return computeVersionName()
  37. }
  38. android {
  39. compileSdkVersion 31
  40. useLibrary 'org.apache.http.legacy'
  41. defaultConfig {
  42. minSdkVersion 21
  43. targetSdkVersion 30
  44. versionCode computeVersionCode()
  45. versionName computeVersionName()
  46. multiDexEnabled true
  47. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  48. resConfigs "en", "es" ,"pt"
  49. javaCompileOptions {
  50. annotationProcessorOptions {
  51. arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
  52. }
  53. }
  54. }
  55. buildTypes {
  56. debug {
  57. }
  58. release {
  59. minifyEnabled false
  60. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  61. }
  62. }
  63. compileOptions {
  64. sourceCompatibility JavaVersion.VERSION_1_8
  65. targetCompatibility JavaVersion.VERSION_1_8
  66. }
  67. kotlinOptions {
  68. freeCompilerArgs = ['-Xjvm-default=compatibility']
  69. }
  70. }
  71. dependencies {
  72. implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
  73. //Don't change it back to implementation else we will not be able to get it's jar from the intermediates/compile_library_classes
  74. api project(path: ':WeFiUtil')
  75. api project(path: ':WeFiUtilExt')
  76. api project(path: ':WeFiKeepAliveLibs')
  77. api project(path: ':WeFiOsDataPollingLibs')
  78. api project(path: ':WeFiDataCollectLibs')
  79. implementation files('../cauchoHessian/cauchoHessian.jar')
  80. implementation files('../WeFiSupportLibrary/WeFiSupportLibrary.jar')
  81. implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
  82. compileOnly "com.google.code.gson:gson:$gsonVersion"
  83. implementation 'com.google.firebase:firebase-crashlytics:18.2.6'
  84. implementation 'com.google.firebase:firebase-analytics:20.0.2'
  85. compileOnly "com.google.android.gms:play-services-location:$playServicesVersion"
  86. compileOnly "com.google.firebase:firebase-analytics:$firebaseAnalyticsVersion"
  87. implementation "com.google.firebase:firebase-messaging:23.0.0"
  88. implementation "com.android.installreferrer:installreferrer:2.2"
  89. implementation "androidx.room:room-runtime:2.4.1"
  90. kapt "androidx.room:room-compiler:2.4.1"
  91. implementation "androidx.core:core-ktx:1.7.0"
  92. implementation 'androidx.preference:preference-ktx:1.1.1'
  93. // Excel support
  94. implementation 'org.apache.poi:poi:3.17'
  95. def moshiVersion = "1.13.0"
  96. implementation("com.squareup.moshi:moshi:$moshiVersion")
  97. implementation("com.squareup.moshi:moshi-kotlin:$moshiVersion")
  98. kapt("com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion")
  99. api "com.android.volley:volley:1.2.1"
  100. }
  101. repositories {
  102. mavenCentral()
  103. }
  104. def gitBranch() {
  105. def branch = ""
  106. def proc = "git rev-parse --abbrev-ref HEAD".execute()
  107. proc.in.eachLine { line -> branch = line }
  108. proc.err.eachLine { line -> println line }
  109. proc.waitFor()
  110. println "branch name = " + branch
  111. branch
  112. }

gradle.properties

  1. # Project-wide Gradle settings.
  2. # IDE (e.g. Android Studio) users:
  3. # Gradle settings configured through the IDE *will override*
  4. # any settings specified in this file.
  5. # For more details on how to configure your build environment visit
  6. # http://www.gradle.org/docs/current/userguide/build_environment.html
  7. # Specifies the JVM arguments used for the daemon process.
  8. # The setting is particularly useful for tweaking memory settings.
  9. #org.gradle.jvmargs=-Xmx1Fg536m
  10. # Increase memory allotted to JVM
  11. org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -Dfile.encoding=UTF-8 -XX:+UseParallelGC
  12. # When configured, Gradle will run in incubating parallel mode.
  13. # This option should only be used with decoupled projects. More details, visit
  14. # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
  15. org.gradle.parallel=true
  16. #android.enableSeparateAnnotationProcessing=true
  17. android.useAndroidX=true
  18. android.enableJetifier=true
  19. android.injected.testOnly = false
  20. org.gradle.caching=true
  21. org.gradle.daemon=true
  22. # Enable Configure on demand
  23. org.gradle.configureondemand=true
  24. kotlin.incremental=true
  25. # positive value will enable caching
  26. # use the same value as the number of modules that use kapt
  27. kapt.classloaders.cache.size=5
  28. # disable for caching to work
  29. kapt.include.compile.classpath=false

gradle-wrapper.properties

  1. #Wed Jan 12 11:49:46 MSK 2022
  2. distributionBase=GRADLE_USER_HOME
  3. distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
  4. distributionPath=wrapper/dists
  5. zipStorePath=wrapper/dists
  6. zipStoreBase=GRADLE_USER_HOME

生成分析器显示以下两个导致问题的任务:

yqkkidmi

yqkkidmi1#

尝试在gradle.properties中设置org.gradle.jvmargs,如下所示:

  1. org.gradle.jvmargs=-Xmx3g

请注意,清理后的第一个大项目的构建可能需要10分钟甚至更长时间。
一些其他提示:

  • 更新Android Gradle插件至最新稳定版本(当前7.1.3)和Gradle版本(当前7.2)。请参阅本文档。
  • 更新Kotlin版本至1.6.10或更高版本。
  • 更新Android StudioBuild-Tools
  • sourceCompatibilitytargetCompatibility使用JavaVersion.VERSION_11
  1. compileOptions {
  2. sourceCompatibility JavaVersion.VERSION_11
  3. targetCompatibility JavaVersion.VERSION_11
  4. }
  5. kotlinOptions {
  6. jvmTarget = "11"
  7. ...
  8. }
  • grade.properties中设置android.enableJetifier=false
展开查看全部
t5fffqht

t5fffqht2#

通过添加

  1. kotlin.incremental=true

到gradle.properties我的构建时间从23秒到1秒。

相关问题