24

I have developed an Android app using the Eclipse IDE and now the code count has grown very huge. I want to do the code review using a static code analysis tool to help me find any silly mistakes in the code such duplicate code, exception handling errors etc. It should be pluggable within the Eclipse IDE.

Can anybody suggest a tool which I can use in my project to detect coding issues?

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
piks
  • 1,621
  • 8
  • 32
  • 59

3 Answers3

16

I don't know about "best"; I only know about "useful". I would start by simply opening the Lint Warnings view (Window -> Show View -> Other -> Android -> Lint Warnings). Then you might consider using FindBugs, an excellent tool.

It's not a static code analysis tool, but during development you should enable StrictMode. It helps find a lot of coding problems specific to Android. (Turn it off for deployment!)

For other tools, take a look at this thread.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks a lot for your help,one more query, can i use FindBugs as a plugin in eclipse? if yes then can u pls send the link for download it. – piks Mar 05 '12 at 09:00
  • @piks The FindBugs [downloads page](http://findbugs.sourceforge.net/downloads.html) at SourceForge lists several Eclipse plugin download sites. The site for official releases is http://findbugs.cs.umd.edu/eclipse – Ted Hopp Mar 05 '12 at 19:49
  • @piks, FindBugs looks for duplicates only when they might be causing possible bugs. CheckStyle has good duplicate code detection. In practice you would use a tool like Sonar, which in turn encompasses complementary set of tools like FindBugs, PMD and Checkstyle and even Lint. – Paddy May 30 '14 at 13:19
  • @TedHopp : Please tell me how can I achieve code checking for a gradle based android studio project. Is there any way to do so using command line. I would prefer a tool which can be controlled using command line as it can be used for continuous integration. – Nevin Raj Victor Apr 20 '15 at 07:16
  • 1
    @NevinRaj - Most tools (including FindBugs) are command-line tools at their core. (StrictMode has nothing to do with the IDE, so there's no problem using it in Android Studio.) – Ted Hopp Apr 20 '15 at 14:57
  • 3
    There is a plugin called QAPlug can be found in Android Studio, which integrates three tools: checkstyle, PMD and findbugs. – Priyeshj Feb 03 '16 at 19:06
14

Sonarqube step by step implementation

Step 1: First download the sonarqube LTS(Stable version) from this link

Don't download latest version. It produce java version issue. I tried 7.3 version working fine for me.

enter image description here

https://www.sonarqube.org/downloads/

Step 2: goto conf -> wrapper.conf -> set your java path

wrapper.java.command=C:\Program Files\Java\jdk1.8.0_60\bin\java

Next goto bin -> select your OS -> Click StartSonar

enter image description here

Step 3: http://localhost:9000/

Default Login credentials

Username - admin

Password - admin

Step 4: Project Build gradle File

   repositories {
       jcenter()
       maven { url "https://plugins.gradle.org/m2/" }//add
   }

   dependencies {
       classpath 'com.android.tools.build:gradle:2.3.0'
       classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.1" //add
       // NOTE: Do not place your application dependencies here; they belong
       // in the individual module build.gradle files
   }
}

allprojects {
   repositories {
       jcenter()
   }
}

task clean(type: Delete) {
   delete rootProject.buildDir
}

Step 5: (Just copy & paste at bottom of build.gradle)

App Module Build gradle File

apply plugin: 'org.sonarqube'

sonarqube
       {
           properties
                   {
                       property "sonar.projectName", "RealmSample"
                       property "sonar.projectKey", "org.sonarqube:android-simple-sq-scanner-gradle"
                       property "sonar.language", "java"
                       property "sonar.sources", "src"
                       property "sonar.binaries", "build"
                       property "sonar.sourceEncoding", "UTF-8"
                       property "sonar.login", "admin"
                       property "sonar.password", "admin"
                   }
       }

Step 6: Gradle.Properties File

systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.login=admin
systemProp.sonar.password=admin

Step 7:

Open android studio terminal tab(Android studio bottom) & open your current project path ex: cd:\ d:yourProjectPath

And apply this command

Windows OS

.\gradlew sonarqube

MAC OS

bash ./gradlew sonarqube

Step 8:

Check now http://localhost:9000 (if not refreshed click refresh button)..

Now you can analyze your code.

Note: If anybody using mac try this

Step 1:(Install homebrew command) ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step 2: Install open JDK (Java)

brew cask install adoptopenjdk

Step 3: Install Sonar

brew install sonar

Step 4: Start sonarqube

brew services start sonarqube

For kotlin support. (don't go latest version it will produce java version issue)

Use 7.3 version

download link - version https://www.sonarqube.org/sonarqube-7-3/

follow all above steps with 7.3 version and change language in build.gradle

property "sonar.language", "kotlin"
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
4

SonarQube is a platform to analyze code quality, security and reliability. It is a continuous inspection engine and offers reports on duplicated code, exception handling, coding standards, unit tests, code coverage, code complexity, potential bugs, comments, design and architecture etc.

I have used it and it helps me to detect bugs and keep my code clean and of good quality.

UPDATE

Below is the link of post on my blog which gives complete detailed explanation of integrating SonarQube with SonarQube Scanner.

Integrating and Understanding SonarQube in Android

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Android Developer
  • 9,157
  • 18
  • 82
  • 139