Gradle 使用

Gradle 使用

buildFinished

项目臃肿加上开启minifyEnabled, dump.txt 文件变得非常大,利用gradle 构建生命周期去删除无用的构建垃圾.

settings.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
gradle.buildFinished {
println "构建结束..."
def f = new File("app/build/outputs/mapping")
delFileFolder(f)

}

def delFileFolder(File f) {
if (!f.exists()) {
return
}
if (f.isDirectory()) {
def files = f.listFiles()
for (File file : files) {
delFileFolder(file)
}
}
f.delete()
}

Build apk

出现以下错误

1
Unsupported major.minor version 52.0

stackoverflow

1
2
3
4
5
You get this error because a Java 7 VM tries to load a class compiled for Java 8

Java 8 has the class file version 52.0 but a Java 7 VM can only load class files up to version 51.0

In your case the Java 7 VM is your gradle build and the class is com.android.build.gradle.AppPlugin

简单来说,就是java的编译环境版本太低,java 8 class file的版本是52,Java 7虚拟机只能支持到51。所以需要升级到java 8 vm才行。

gradle命令的执行环境是在gradle.properties配置的

1
org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home

Gradle实用技巧

1
`./gradlew clean :app:assembleRelease --info | grep "unused resource" `
0%