Skip to content

Commit

Permalink
增加了lint检查
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale committed Dec 26, 2016
1 parent a0690ca commit a010e57
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 123 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
compileSdkVersion 25
buildToolsVersion '25'

defaultConfig {
applicationId "com.orhanobut.loggersample"
minSdkVersion 15
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
Expand All @@ -21,7 +21,7 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
compile project(':logger')
compile 'com.jakewharton.timber:timber:4.4.0'
compile project(':lintaar')
}
11 changes: 9 additions & 2 deletions app/src/main/java/com/orhanobut/loggersample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.orhanobut.loggersample;

import java.util.Arrays;

import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -9,15 +11,20 @@
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.Settings;

import java.util.Arrays;


public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// lint
System.out.println("lint error");

Log.d(TAG, "onCreate: lint error");

Logger.initialize(
new Settings()
// .setStyle(new XLogStyle())
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' // Add this line
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
1 change: 1 addition & 0 deletions lint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions lint/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apply plugin: 'java'

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.tools.lint:lint-api:25.0.0'
compile 'com.android.tools.lint:lint-checks:25.0.0'
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

jar {
manifest {
attributes("Lint-Registry": "kale.log.lint.IssueRegister")
}
}

configurations {
lintJarOutput
}

dependencies {
lintJarOutput files(jar)
}

defaultTasks 'assemble'
21 changes: 21 additions & 0 deletions lint/src/main/java/kale/log/lint/IssueRegister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kale.log.lint;

import java.util.Collections;
import java.util.List;

import com.android.tools.lint.client.api.IssueRegistry;
import com.android.tools.lint.detector.api.Issue;

/**
* @author Kale
* @date 2016/12/26
*/
public class IssueRegister extends IssueRegistry {

@Override
public List<Issue> getIssues() {
return Collections.singletonList(
LogDetector.ISSUE
);
}
}
67 changes: 67 additions & 0 deletions lint/src/main/java/kale/log/lint/LogDetector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package kale.log.lint;

import java.util.Collections;
import java.util.List;

import com.android.tools.lint.client.api.JavaParser;
import com.android.tools.lint.detector.api.Category;
import com.android.tools.lint.detector.api.Detector;
import com.android.tools.lint.detector.api.Implementation;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.JavaContext;
import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.Severity;

import lombok.ast.AstVisitor;
import lombok.ast.ForwardingAstVisitor;
import lombok.ast.MethodInvocation;
import lombok.ast.Node;

/**
* @author Kale
* @date 2016/12/26
*/
public class LogDetector extends Detector implements Detector.JavaScanner {

private static final String ISSUE_DESCRIPTION = "You should use our {AppLog}";

public static final Issue ISSUE = Issue.create(
"LogUseError",
"避免使用Log/System.out.println",
"使用{AppLog},防止在正式包打印log",
Category.SECURITY,
5,
Severity.ERROR,
new Implementation(LogDetector.class, Scope.JAVA_FILE_SCOPE)
);

@Override
public List<Class<? extends Node>> getApplicableNodeTypes() {
return Collections.<Class<? extends Node>>singletonList(MethodInvocation.class);
}

@Override
public AstVisitor createJavaVisitor(final JavaContext context) {
return new ForwardingAstVisitor() {
@Override
public boolean visitMethodInvocation(MethodInvocation node) {

if (node.toString().startsWith("System.out.println")) {
context.report(ISSUE, node, context.getLocation(node), ISSUE_DESCRIPTION);
return true;
}

JavaParser.ResolvedNode resolve = context.resolve(node);
if (resolve instanceof JavaParser.ResolvedMethod) {
JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolve;
JavaParser.ResolvedClass containingClass = method.getContainingClass();
if (containingClass.matches("android.util.Log")) {
context.report(ISSUE, node, context.getLocation(node), ISSUE_DESCRIPTION);
return true;
}
}
return super.visitMethodInvocation(node);
}
};
}
}
1 change: 1 addition & 0 deletions lintaar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
48 changes: 48 additions & 0 deletions lintaar/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
}

/**
* rules for including "lint.jar" in aar
*/

configurations {
lintJarImport
}
dependencies {
lintJarImport project(path: ":lint", configuration: "lintJarOutput")
}
task copyLintJar(type: Copy) {
from(configurations.lintJarImport) {
rename {
String fileName ->
'lint.jar'
}
}
into 'build/intermediates/lint/'
}

project.afterEvaluate {
def compileLintTask = project.tasks.find { it.name == 'compileLint' }
compileLintTask.dependsOn(copyLintJar)
}
17 changes: 17 additions & 0 deletions lintaar/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in H:\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
7 changes: 7 additions & 0 deletions lintaar/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest package="kale.log.lint" xmlns:android="http://schemas.android.com/apk/res/android">

<application>

</application>

</manifest>
3 changes: 3 additions & 0 deletions lintaar/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">lintAar</string>
</resources>
3 changes: 3 additions & 0 deletions logger/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

group='com.github.tianzhijiexian'

android {
compileSdkVersion 25
Expand Down
35 changes: 0 additions & 35 deletions logger/src/test/java/com.orhanobut.logger/LoggerTest.java

This file was deleted.

82 changes: 0 additions & 82 deletions logger/src/test/java/com.orhanobut.logger/SettingsTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app', ':logger'
include ':app', ':logger', ':lint', ':lintaar'

0 comments on commit a010e57

Please sign in to comment.