#sonar android上的實踐html
因爲sonarqube依賴MySql,因此須要安裝MySql,安裝教程java
# User credentials. # Permissions to create tables, indices and triggers must be granted to JDBC user. # The schema must be created first. #sonar.jdbc.username= #sonar.jdbc.password= sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=root sonar.jdbc.password=root sonar.sorceEncoding=UTF-8 //安裝mysql是設置帳戶和密碼 sonar.login=admin sonar.password=admin 複製代碼
重啓sonarqube服務,再次訪問http://localhost:9000,會稍微有點慢,由於要初始化數據庫信息,至此即可以在sonar上面安裝插件了mysql
接下來能夠安裝中文插件,直接在配置->應用市場->搜索Chinese Pack而後安裝重啓就能夠了android
先如今sonar-scanner,下載地址 下載以後編輯E:\sonar-scanner-3.2.0.1227-windows\conf\sonar-scanner.propertiesgit
#Configure here general information about the environment, such as SonarQube server connection details for example #No information about specific project should appear here #----- Default SonarQube server sonar.host.url=http://localhost:9000 #----- Default source code encoding #sonar.sourceEncoding=UTF-8 sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=root sonar.jdbc.password=root 複製代碼
而後在想要分析的android根目錄下建立一個sonar-project.properties,內容以下github
#Configure here general information about the environment, such as SonarQube server connection details for example #No information about specific project should appear here #----- Default SonarQube server #sonar.host.url=http://localhost:9000 #----- Default source code encoding #sonar.sourceEncoding=UTF-8 # must be unique in a given SonarQube instance sonar.projectKey=StickyNavLayout-demo # this is the name displayed in the SonarQube UI sonar.projectName=StickyNavLayout-demo sonar.projectVersion=7.5 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # Since SonarQube 4.2, this property is optional if sonar.modules is set. # If not set, SonarQube starts looking for source code from the directory containing # the sonar-project.properties file. sonar.sources=E:\workplace\github\StickyNavLayout-demo\app\src # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8 複製代碼
而後在項目根目錄下執行sonar-scanner 進行分析sql
作android開發的話,平時都在androidStudio上開發,若是按照方式一的話,每次新項目都要去建立文件,這樣不是很方便,androidStudio gradle已經爲咱們添加好sonar-scaner,能夠經過下面方式進行配置數據庫
apply from: "dependencies.gradle" apply plugin: 'com.alibaba.arouter' buildscript { ext.kotlin_version = '1.2.30' repositories { #1 配置maven創庫 maven { url "https://plugins.gradle.org/m2/" } google() jcenter() } dependencies { #2 配置 classpath classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2" classpath 'com.android.tools.build:gradle:3.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' classpath "com.alibaba:arouter-register:1.0.0" // classpath "com.mob.sdk:MobSDK:+" classpath 'com.growingio.android:vds-gradle-plugin:2.4.3' //用於方便調試性能問題的打印插件。給訪法加上@DebugLog,就能輸出該方法的調用參數,以及執行時間 classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } #3配置plugin apply plugin: "org.sonarqube" #4配置task sonarqube { properties { property "sonar.sourceEncoding", "UTF-8" } } #4配置sonarqube參數 subprojects { apply plugin: 'eclipse' apply plugin: 'idea' repositories { mavenCentral() jcenter() } sonarqube { properties { property "sonar.sources", "src" property "sonar.java.binaries", "build/intermediates/javac" property "sonar.host.url", "http://http://10.1.3.40:9000/" property "sonar.login", "admin" property "sonar.password", "admin" property "sonar.jdbc.url", "jdbc:mysql://http://10.1.3.40:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance" property "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver" property "sonar.jdbc.username", "root" property "sonar.jdbc.password", "root" } } } allprojects { repositories { flatDir { dirs project(':app').file('libs') } // maven { //// 電腦本地Maven倉庫地址 // url uri('D:/AndroidStudio/LocalMaven') // } maven { url "http://mvn.mob.com/android" } google() jcenter() } configurations.all { resolutionStrategy { force "com.android.support:support-v4:${supportLib}" force "com.android.support:support-annotations:${supportLib}" force "com.android.support:appcompat-v7:${supportLib}" force "com.android.support:design:${supportLib}" force "com.android.support:recyclerview-v7:${supportLib}" force "com.android.support:cardview-v7:${supportLib}" force "com.android.support:design:${supportLib}" force "com.android.support:support-compat:${supportLib}" force "com.android.support:support-core-ui:${supportLib}" force "com.android.support:support-core-utils:${supportLib}" force "com.android.support:support-fragment:${supportLib}" force "com.android.support.constraint:constraint-layout:1.1.0" } } } task clean(type: Delete) { delete rootProject.buildDir } 複製代碼
gradle sonarqube
複製代碼
首先jenkins上按照Sonar插件windows
sonar.projectKey=項目名xxx sonar.projectName=項目名xxx sonar.projectVersion=1.0 sonar.sourceEncoding=UTF-8 sonar.sources=app/src,album/src,base/src,cropview/src,im-business/src,lib_live_agora/src,lib_log/src,performancelib/src,push/src,refreshlibrary/src,xrecyclerview/src sonar.java.binaries = app/build/intermediates/classes sonar.host.url=http://127.0.0.1:9000/ sonar.login= admin sonar.password=admin sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.driverClassName=com.mysql.jdbc.Driver sonar.jdbc.username=root sonar.jdbc.password=root 複製代碼
Task to run :scan JDK:JDK_8 Analysis properties: sonar.projectKey=test sonar.projectName=test sonar.projectVersion=1.0 sonar.sourceEncoding=UTF-8 sonar.sources=app sonar.java.binaries = app/build/intermediates/classes sonar.host.url=http://127.0.0.1:9000/ sonar.login= admin sonar.password=admin sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.driverClassName=com.mysql.jdbc.Driver sonar.jdbc.username=root sonar.jdbc.password=root Additional arguments:-Xbash
咱們知道,android 自帶一個叫lint的檢查工具,能夠檢查android相關無用資源,OverDraw等問題,github上也有一個現成的開源插件庫可是最新sonar7.5這個庫已經不支持了,sonar7.5變動了不少,因此本身研究開發了一個地址
目前java檢查規則是基於pmd這個插件進行二次開發,pmd的原理網上也有不少資料介紹,整體是使用java cc 生成解析器來解析源代碼並生成AST(抽象語法樹)
目前sonar 已經支持很是多第三方代碼檢查插件,例如sonar java,pmd,findbugs等,若是業務須要能夠自定義本身插件
1.androidLint 之前sonar6.5以前是有第三方androidlint,可是7.5之後,第三方庫不更新了,只有本身開發了一個 github.com/dengqu/sona…
android lint: 1.NewApi 代碼中使用的某些API高於Manifest中的Min SDK 2.Deprecated 使用已經廢棄的API 3.PxUsage 避免使用px,使用dp 4.DrawAllocation 避免在繪製或者解析佈局(draw/layout)時分配對象。E.g.,Ondraw()中實例化Paint對象。 5.Node can be replaced by a TextView with compound drawables 可優化的佈局:如包含一個Imageview和一個TextView的線性佈局,可被採用CompoundDrawable的TextView代替。 6.Overdraw: Painting regions more than once 若是爲RootView指定一個背景Drawable,會先用Theme的背景繪製一遍,而後才用指定的背景,這就是所謂的「Overdraw」。 能夠設置theme的background爲null來避免。 7.Hardcoded text 8.HashMap can be replaced with SparseArray 9.Layout hierarchy is too deep 10.Layout has too many views Memory allocations within drawing code