From a010e572096935b6cd46a0b61022b410815240bc Mon Sep 17 00:00:00 2001 From: Kale Date: Mon, 26 Dec 2016 14:10:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86lint=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 6 +- .../orhanobut/loggersample/MainActivity.java | 11 ++- build.gradle | 1 + lint/.gitignore | 1 + lint/build.gradle | 27 ++++++ .../java/kale/log/lint/IssueRegister.java | 21 +++++ .../main/java/kale/log/lint/LogDetector.java | 67 +++++++++++++++ lintaar/.gitignore | 1 + lintaar/build.gradle | 48 +++++++++++ lintaar/proguard-rules.pro | 17 ++++ lintaar/src/main/AndroidManifest.xml | 7 ++ lintaar/src/main/res/values/strings.xml | 3 + logger/build.gradle | 3 + .../java/com.orhanobut.logger/LoggerTest.java | 35 -------- .../com.orhanobut.logger/SettingsTest.java | 82 ------------------- settings.gradle | 2 +- 16 files changed, 209 insertions(+), 123 deletions(-) create mode 100644 lint/.gitignore create mode 100644 lint/build.gradle create mode 100644 lint/src/main/java/kale/log/lint/IssueRegister.java create mode 100644 lint/src/main/java/kale/log/lint/LogDetector.java create mode 100644 lintaar/.gitignore create mode 100644 lintaar/build.gradle create mode 100644 lintaar/proguard-rules.pro create mode 100644 lintaar/src/main/AndroidManifest.xml create mode 100644 lintaar/src/main/res/values/strings.xml delete mode 100644 logger/src/test/java/com.orhanobut.logger/LoggerTest.java delete mode 100644 logger/src/test/java/com.orhanobut.logger/SettingsTest.java diff --git a/app/build.gradle b/app/build.gradle index c805babf..5acf87cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" @@ -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') } \ No newline at end of file diff --git a/app/src/main/java/com/orhanobut/loggersample/MainActivity.java b/app/src/main/java/com/orhanobut/loggersample/MainActivity.java index fe549cca..1bf4dc1e 100644 --- a/app/src/main/java/com/orhanobut/loggersample/MainActivity.java +++ b/app/src/main/java/com/orhanobut/loggersample/MainActivity.java @@ -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; @@ -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()) diff --git a/build.gradle b/build.gradle index 1a2e10a2..00961c4d 100644 --- a/build.gradle +++ b/build.gradle @@ -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 } diff --git a/lint/.gitignore b/lint/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/lint/.gitignore @@ -0,0 +1 @@ +/build diff --git a/lint/build.gradle b/lint/build.gradle new file mode 100644 index 00000000..705988ab --- /dev/null +++ b/lint/build.gradle @@ -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' \ No newline at end of file diff --git a/lint/src/main/java/kale/log/lint/IssueRegister.java b/lint/src/main/java/kale/log/lint/IssueRegister.java new file mode 100644 index 00000000..408263f5 --- /dev/null +++ b/lint/src/main/java/kale/log/lint/IssueRegister.java @@ -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 getIssues() { + return Collections.singletonList( + LogDetector.ISSUE + ); + } +} diff --git a/lint/src/main/java/kale/log/lint/LogDetector.java b/lint/src/main/java/kale/log/lint/LogDetector.java new file mode 100644 index 00000000..3b794d62 --- /dev/null +++ b/lint/src/main/java/kale/log/lint/LogDetector.java @@ -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> getApplicableNodeTypes() { + return Collections.>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); + } + }; + } +} diff --git a/lintaar/.gitignore b/lintaar/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/lintaar/.gitignore @@ -0,0 +1 @@ +/build diff --git a/lintaar/build.gradle b/lintaar/build.gradle new file mode 100644 index 00000000..db9376d4 --- /dev/null +++ b/lintaar/build.gradle @@ -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) +} \ No newline at end of file diff --git a/lintaar/proguard-rules.pro b/lintaar/proguard-rules.pro new file mode 100644 index 00000000..f1800465 --- /dev/null +++ b/lintaar/proguard-rules.pro @@ -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 *; +#} diff --git a/lintaar/src/main/AndroidManifest.xml b/lintaar/src/main/AndroidManifest.xml new file mode 100644 index 00000000..37cdffef --- /dev/null +++ b/lintaar/src/main/AndroidManifest.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/lintaar/src/main/res/values/strings.xml b/lintaar/src/main/res/values/strings.xml new file mode 100644 index 00000000..04047fdd --- /dev/null +++ b/lintaar/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + lintAar + diff --git a/logger/build.gradle b/logger/build.gradle index adac9006..a6593025 100644 --- a/logger/build.gradle +++ b/logger/build.gradle @@ -1,4 +1,7 @@ apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' + +group='com.github.tianzhijiexian' android { compileSdkVersion 25 diff --git a/logger/src/test/java/com.orhanobut.logger/LoggerTest.java b/logger/src/test/java/com.orhanobut.logger/LoggerTest.java deleted file mode 100644 index 53e2d7ba..00000000 --- a/logger/src/test/java/com.orhanobut.logger/LoggerTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.orhanobut.logger; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; -import org.robolectric.annotation.Config; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Orhan Obut - */ -@RunWith(RobolectricGradleTestRunner.class) -@Config(constants = BuildConfig.class, sdk = 21) -public class LoggerTest { - - @Test - public void testInit() { - Logger.initialize(Settings.builder().build()); - assertThat(Logger.getSettings()).isInstanceOf(Settings.class); - } - - @Test - public void testT() { - Settings settings = Logger.t("tag").getSettings(); - assertThat(settings.getMethodCount()).isEqualTo(2); - - settings = Logger.t(10).getSettings(); - assertThat(settings.getMethodCount()).isEqualTo(2); - - settings = Logger.t("tag", 5).getSettings(); - assertThat(settings.getMethodCount()).isEqualTo(2); - } - -} diff --git a/logger/src/test/java/com.orhanobut.logger/SettingsTest.java b/logger/src/test/java/com.orhanobut.logger/SettingsTest.java deleted file mode 100644 index e35d21ad..00000000 --- a/logger/src/test/java/com.orhanobut.logger/SettingsTest.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.orhanobut.logger; - -import junit.framework.TestCase; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; -import org.robolectric.annotation.Config; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Orhan Obut - */ -@RunWith(RobolectricGradleTestRunner.class) -@Config(constants = BuildConfig.class, sdk = 21) -public class SettingsTest extends TestCase { - - private Settings settings; - - @Before - public void setup() { - settings = Settings.builder().build(); - } - - @After - public void tearDown() { - settings = null; - } - - @Test - public void testDefaultShowThreadInfo() { - assertThat(settings.isShowThreadInfo()).isFalse(); - } - - @Test - public void testCustomShowThreadInfo() { - settings.isShowThreadInfo(true); - assertThat(settings.isShowThreadInfo()).isTrue(); - } - - @Test - public void testDefaultLogLevel() { - assertThat(settings.getLogLevel()).isEqualTo(LogLevel.FULL); - } - - @Test - public void testCustomLogLevel() { - settings.setLogLevel(LogLevel.NONE); - assertThat(settings.getLogLevel()).isEqualTo(LogLevel.NONE); - - settings.setLogLevel(LogLevel.FULL); - assertThat(settings.getLogLevel()).isEqualTo(LogLevel.FULL); - } - - @Test - public void testMethodCount() { - //default - assertThat(settings.getMethodCount()).isEqualTo(1); - - settings.setMethodCount(4); - assertThat(settings.getMethodCount()).isEqualTo(4); - - //negative values should be convert to 0 - settings.setMethodCount(-10); - assertThat(settings.getMethodCount()).isEqualTo(0); - } - - @Test - public void testMethodOffset() { - //default - assertThat(settings.getMethodOffset()).isEqualTo(0); - - settings.setMethodOffset(10); - assertThat(settings.getMethodOffset()).isEqualTo(10); - - settings.setMethodOffset(-10); - assertThat(settings.getMethodOffset()).isEqualTo(-10); - } -} diff --git a/settings.gradle b/settings.gradle index 2ebe718e..81462eb6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':logger' +include ':app', ':logger', ':lint', ':lintaar'