Android代码分析日志
Android Studio有个Terminal工具。
首先确保app/build.gradle中没有如下配置:
android {
lintOptions {
abortOnError false
}
}
在Terminal命令框内输入:gradlew build
会提示如下错误:
Ran lint on variant debug: 695 issues found
Ran lint on variant release: 695 issues found
Wrote HTML report to file:/D:/Android%20Studio%20Workspace/InvoicingSys/app/build/outputs/lint-results.html
Wrote XML report to D:\Android Studio Workspace\InvoicingSys\app\build\outputs\lint-results.xml
:app:lint FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:lint'.
> Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
...
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 24.38 secs
这时候就可以发现分析日志在:D:\Android Studio Workspace\InvoicingSys\app\build\outputs\目录下的两个文件。
这里面将会列出:所有存在warning的代码,未被用到的声明、资源文件,不合理的布局等。
Terminal的使用
开发工具当然是Android Studio.
1、在Terminal命令框内输入 gradlew -v
如果是初次使用该命令会提示下载gradle,然后显示无数个省略号,直到下载、解压完成。会出现如下结果:
------------------------------------------------------------
Gradle 2.4
------------------------------------------------------------
Build time: 2015-05-05 08:09:24 UTC
Build number: none
Revision: 5c9c3bc20ca1c281ac7972643f1e2d190f2c943c
Groovy: 2.3.10
Ant: Apache Ant(TM) version 1.9.4 compiled on April 29 2014
JVM: 1.7.0_75 (Oracle Corporation 24.75-b04)
OS: Windows 7 6.1 amd64
说明安装成功。
2、然后,执行gradlew clean
同样如果是第一次执行该命令,也会下载一些依赖包。等下载完成后,会出现如下结果:
:clean
:app:clean
BUILD SUCCESSFUL
Total time: 11.925 secs
说明清理build文件成功。
3、然后,用gradlew命令打包
执行命令:gradlew build
如果app/build.gradle中没有配置了:
android {
lintOptions {
abortOnError false
}
}
那么会出现打包失败:
Wrote HTML report to file:/D:/Android%20Studio%20Workspace/InvoicingSys/app/build/outputs/lint-results.html
Wrote XML report to D:\Android Studio Workspace\InvoicingSys\app\build\outputs\lint-results.xml
:app:lint FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:lint'.
> Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
...
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
那么,如果想打包成功则需要如下配置:
android {
lintOptions {
abortOnError false//忽略lint警告,否则会打包失败
}
signingConfigs {
release {
storeFile file("xxxxx")//app是其根目录
storePassword "xxxxx"
keyAlias "xxxx"
keyPassword "xxxx"
}
}
buildTypes {
release {
signingConfig signingConfigs.release//如果不使用此签名,打出的包是未签名的。
minifyEnabled true//可以不混淆。混淆可以使apk文件变小。
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
另外gradlew还有那些命令呢?
gradlew installRelease 打发布包,并安装到设备。
gradlew -h 显示常规命令列表。有些命令没有在其列表中列出。