How Do We Configure Android Studio to Run Its Lint on Every Build?

前端 未结 4 530
清酒与你
清酒与你 2020-12-12 19:21

Once upon a time, particularly in Eclipse-land, Lint would run on every build, and so if you failed Lint checks, you would find out immediately. With Android Studio (tested

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 20:04

    I accomplished this previously by adding a pre-push git hook that would run lint automatically on push, and fail to push if Lint errors were found. The pre-push hook script was stored in the Android project repo and was installed to a user's local machine automatically via gradle.

    install-git-hooks.gradle

    def hookDest = new File("${rootProject.rootDir}/.git/hooks/pre-push")
    def prePushHook = new File("${rootProject.rootDir}/pre-push")
    
    task installGitHooksTask(type: Copy) {
        hookDest.delete()
        hookDest << prePushHook.text
    }
    
    task gitExecutableHooks() {
        Runtime.getRuntime().exec("chmod -R +x ${hookDest}");
        println "gitExecutableHooks"
    }
    
    gitExecutableHooks.dependsOn installGitHooksTask
    

    Than in your app build.gradle

    apply from: rootProject.file('gradle/install-git-hooks.gradle')
    

    pre-push

    #!/bin/sh
    #
    # This hook is for Android project git repos.
    #
    # You can use git config variables to customize this hook.
    # -----------------------------------------------------------
    # Change hooks.lintTargetDirectory to point at a non . directory
    # Change hooks.lintArgs to add any custom lint arguments you prefer
    
    # Get custom info
    dirToLint=$(git config hooks.lintTargetDirectory)
    lintArgs=$(git config hooks.lintArgs)
    projectDir=$(git rev-parse --show-toplevel)
    lintReportPath="/app/lint-report.html"
    
    # If user has not defined a preferred directory to lint against, make it .
    if [ -z "$dirToLint"]
      then
      dirToLint="."
    fi
    
    # Perform lint check
    echo "Performing pre-commit lint check of ""$dirToLint"
    ./gradlew lint
    lintStatus=$?
    
    if [ $lintStatus -ne 0 ]
    then
      echo "Lint failure, git push aborted."
      echo "Open ${projectDir}${lintReportPath} in a browser to see Lint Report"
      exit 1
    fi
    
    exit $lintStatus
    

提交回复
热议问题