提问人:theblitz 提问时间:5/3/2022 最后编辑:Sergiotheblitz 更新时间:6/27/2023 访问量:1968
Android Studio 编译需要很长时间
Android studio compile takes forever
问:
我的 Android Studio 编译需要很长时间。 最少 10 分钟,而且通常比这更长。
这始于将 gradle 插件升级到 7.0.4
这是我在 ap 级别的 build.gradle:
buildscript {
ext.kotlin_version = "1.6.0"
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.google.gms:google-services:4.3.10'
classpath "io.realm:realm-gradle-plugin:6.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://github.com/WickeDev/stetho-realm/raw/master/maven-repo' }
maven { url 'https://jitpack.io' }
maven {
url 'https://maven.wefi.com/repository/wefi-release-repo'
artifactUrls 'https://maven.wefi.com/repository/wefi-release-repo'
}
configurations.all {
resolutionStrategy {
force 'org.xerial:sqlite-jdbc:3.34.0'
}
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是导致问题的库模块的 build.gradle:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
apply from: '../../WeFiBuild/common/dependencies.gradle'
ext.versionMajor = 0
ext.versionMinor = 0
ext.versionBuild = 0
ext.versionRevision = 0
ext.playStoreVersionCode = 0
def computeVersionName() {
def branch = gitBranch()
if (branch == "master")
return "${versionMajor}.${versionMinor}.${versionRevision}"
else
return "${versionMajor}.${versionMinor}.${versionRevision}.${versionBuild}.rc${versionBuild}"
}
def computeVersionCode() {
computeBuildVersion()
return playStoreVersionCode
// return (versionMajor * 10_000_000) + (versionMinor * 100_000) + (versionBuild * 1_000) + versionRevision
}
def versionPropsFile = file('./../../WeFi/app/version.properties')
ext.computeBuildVersion = {
if (versionPropsFile.canRead()) {
Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionMajor = versionProps['VERSION_MAJOR'].toInteger()
versionMinor = versionProps['VERSION_MINOR'].toInteger()
versionBuild = versionProps['VERSION_BUILD'].toInteger()
versionRevision = versionProps['VERSION_REVISION'].toInteger()
} else {
throw new FileNotFoundException("Could not read version.properties!")
}
return computeVersionName()
}
android {
compileSdkVersion 31
useLibrary 'org.apache.http.legacy'
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode computeVersionCode()
versionName computeVersionName()
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
resConfigs "en", "es" ,"pt"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
freeCompilerArgs = ['-Xjvm-default=compatibility']
}
}
dependencies {
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
//Don't change it back to implementation else we will not be able to get it's jar from the intermediates/compile_library_classes
api project(path: ':WeFiUtil')
api project(path: ':WeFiUtilExt')
api project(path: ':WeFiKeepAliveLibs')
api project(path: ':WeFiOsDataPollingLibs')
api project(path: ':WeFiDataCollectLibs')
implementation files('../cauchoHessian/cauchoHessian.jar')
implementation files('../WeFiSupportLibrary/WeFiSupportLibrary.jar')
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
compileOnly "com.google.code.gson:gson:$gsonVersion"
implementation 'com.google.firebase:firebase-crashlytics:18.2.6'
implementation 'com.google.firebase:firebase-analytics:20.0.2'
compileOnly "com.google.android.gms:play-services-location:$playServicesVersion"
compileOnly "com.google.firebase:firebase-analytics:$firebaseAnalyticsVersion"
implementation "com.google.firebase:firebase-messaging:23.0.0"
implementation "com.android.installreferrer:installreferrer:2.2"
implementation "androidx.room:room-runtime:2.4.1"
kapt "androidx.room:room-compiler:2.4.1"
implementation "androidx.core:core-ktx:1.7.0"
implementation 'androidx.preference:preference-ktx:1.1.1'
// Excel support
implementation 'org.apache.poi:poi:3.17'
def moshiVersion = "1.13.0"
implementation("com.squareup.moshi:moshi:$moshiVersion")
implementation("com.squareup.moshi:moshi-kotlin:$moshiVersion")
kapt("com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion")
api "com.android.volley:volley:1.2.1"
}
repositories {
mavenCentral()
}
def gitBranch() {
def branch = ""
def proc = "git rev-parse --abbrev-ref HEAD".execute()
proc.in.eachLine { line -> branch = line }
proc.err.eachLine { line -> println line }
proc.waitFor()
println "branch name = " + branch
branch
}
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=-Xmx1Fg536m
# Increase memory allotted to JVM
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -Dfile.encoding=UTF-8 -XX:+UseParallelGC
# 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
#android.enableSeparateAnnotationProcessing=true
android.useAndroidX=true
android.enableJetifier=true
android.injected.testOnly = false
org.gradle.caching=true
org.gradle.daemon=true
# Enable Configure on demand
org.gradle.configureondemand=true
kotlin.incremental=true
# positive value will enable caching
# use the same value as the number of modules that use kapt
kapt.classloaders.cache.size=5
# disable for caching to work
kapt.include.compile.classpath=false
gradle-wrapper.properties
#Wed Jan 12 11:49:46 MSK 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
生成分析器显示导致问题的以下两个任务:
答:
1赞
Sergio
5/10/2022
#1
尝试按如下方式设置设置:org.gradle.jvmargs
gradle.properties
org.gradle.jvmargs=-Xmx3g
请注意,清理后大项目的第一次构建可能需要长达 10 分钟甚至更长的时间。
其他一些提示:
将 Android Gradle 插件更新到最新的稳定版本(目前为 7.1.3)和 Gradle 版本(目前为 7.2)。请参阅此文档。
将 Kotlin 版本更新到 1.6.10 或更高版本。
更新 Android Studio 和 Build-Tools。
用于 和 :
JavaVersion.VERSION_11
sourceCompatibility
targetCompatibility
compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "11" ... }
嵌。
android.enableJetifier=false
grade.properties
评论
0赞
theblitz
5/10/2022
将其他参数留在那里?
1赞
Sergio
5/10/2022
尝试删除,如果不起作用,请同时删除其他参数(-Dfile.encoding=UTF-8 -XX:+UseParallelGC)-XX:MaxPermSize=512m
0赞
theblitz
5/10/2022
试过了。仍然不工作:(
1赞
Sergio
5/11/2022
@theblitz请查看我提出的答案中的其他一些提示。
0赞
theblitz
5/12/2022
非常感谢!现在效果很好。做了所有事情(除了 gradle 插件),现在很棒。
0赞
smirkingman
6/27/2023
#2
加入
kotlin.incremental=true
到 gradle.properties,我的构建时间从 23 秒缩短到 1 秒。
评论
gradle.properties