Background: As the project is getting bigger, we need an automated analysis tool because there is no trial code standard, design specification and unit testing, etc. Although AndroidStuido has related plug-ins, it only works locally and cannot be synchronized to the server to generate ICONS and documents. With this in mind we chose Sonar as our code analysis tool. Sonar is a tool that can integrate software quality and metrics together and generate ICONS and documentation. It gives us a snapshot of code quality as well as lagging trends and future quality metrics to help us make the right decisions. What Sonar really stands out is that it not only provides metrics and statistics for your code, but also converts that technical data into real business value. Sonar is not only aimed at core developers and programmers, but also at project managers and higher management levels as Sonar provides a management perspective as well as reporting capabilities. Reason: Sonar, what can it help us do

  1. Whether specific coding specifications are followed
  2. Whether the best time has always been followed will address known bad practices
  3. Potential bugs and performance issues, security concerns, duplicate code
  4. Is the code logic complex
  5. Is the exposed API well documented and commented on
  6. Whether there are unit tests
  7. Does the code follow the best design and architecture

Android project access to Sonar one, environment preparation 1.1 Android related (PS: only say what you need, specific steps to Google)

  1. Gradle environment configuration, it is recommended that the configuration is relatively new and stable, in order to avoid some unknown bug time-consuming effort
  2. The Java environment is a cliche
  3. Android Studio software, highly recommended to install the latest release

Jenkins environment is relatively mature, there are a lot of online information, the best practice is to use Ubuntu virtual machine to build it again, of course, can also use Mac, it is not difficult to build, more cumbersome, patience can be done, if you want to understand the details can leave a message 1.3 Sonar environment

  1. Mysql is a must (I suggest you can learn SpringBoot+mysql+ Mybatis, build your own background, very interesting), Ubuntu and MAC mysql installation is relatively easy, there are corresponding commands can be directly installed
  2. Create sonar needs database, give the corresponding permissions
CREATE DATABASE sonar DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar'; 
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;
Copy the code

From SonarQube website SonarQube from downloaded and installed, configure the mysql database, start the sonar, the default domain name http://127.0.0.1:9000/sonar, Log in to the sonar environment with your account password and create the application in project -> plus -> Create. After creating the application, the projectkey and token will be generated and the domain name will be configured. Iii. Android project configuration

  1. The root directory build.gradle configures dependencies
repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath 'org. Sonarsource. Scanner. Gradle: sonarqube - gradle - plugin: 2.8'}Copy the code

Gradle plugin: “org.sonarqube”

  1. Sonar Configuration
sonarqube {
    properties {
        //property "sonar.projectName", "xxx"
        property "sonar.projectKey", "xxx"
        property "sonar.host.url", "http://xxx/"
        //property "sonar.language", "java"
        //property "sonar.sources", "src/main/"
        property "sonar.login", "xxx"
        //property "sonar.password", "password"
    }
}
Copy the code

Please check the official website for specific configuration details. The official website is very detailed, and there is a solution to the problem in the issue.

  1. Run:./gradlew sonarqube and you can check details on sonar once your build is complete

Sonar is configured on Jenkins

There are two ways to select Jenkins configuration (the general principle is two ways, and it may be refined)

4.1 Through Jenkins’ plug-in, colleagues here have sorted it out well, without retelling it, click to check the detailed address

Http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com http://one.wsj.com