浏览代码

成功集成AspectJ

詹子聪 5 年之前
父节点
当前提交
a61ba78528

+ 3 - 2
NewMvp.iml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<module external.linked.project.id="NewMvp" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
+<module external.linked.project.id="NewMvp" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
   <component name="FacetManager">
     <facet type="java-gradle" name="Java-Gradle">
       <configuration>
@@ -8,10 +8,11 @@
       </configuration>
     </facet>
   </component>
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
     <exclude-output />
     <content url="file://$MODULE_DIR$">
       <excludeFolder url="file://$MODULE_DIR$/.gradle" />
+      <excludeFolder url="file://$MODULE_DIR$/build" />
     </content>
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />

+ 8 - 0
app/build.gradle

@@ -1,4 +1,6 @@
 apply plugin: 'com.android.application'
+apply plugin: 'com.hujiang.android-aspectjx'
+
 
 android {
     compileSdkVersion versions.compileSdk
@@ -29,4 +31,10 @@ dependencies {
 
     // 查看内存泄露
     debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.1'
+
+    compile 'org.aspectj:aspectjrt:1.8.+'
 }
+
+aspectjx {
+    exclude  "android.support",'androidx','com.google','com.squareup.leakcanary','com.squareup.leakcanary.core','com.alipay','org.apache','com.tencent'
+}

+ 37 - 0
app/src/main/java/com/miekir/newmvp/LoadingAspect.java

@@ -0,0 +1,37 @@
+package com.miekir.newmvp;
+
+import android.util.Log;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.After;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+
+import java.lang.reflect.Method;
+
+/**
+ * 加载框
+ */
+@Aspect
+public class LoadingAspect {
+    private static final String TAG = "LoadingAspect";
+    //https://blog.csdn.net/zhengchao1991/article/details/53391244
+    @Pointcut("@annotation(com.miekir.mvp.base.NeedLoading)")
+    public void timeTask() {}
+
+    @After("timeTask()")
+    public void onTimeTaskAround(ProceedingJoinPoint joinPoint) throws Throwable {
+        // 注入加载框
+        Log.e("log", "do loading");
+//        Object targetObject  = joinPoint.getTarget();
+//        try {
+//            Method m = targetObject.getClass().getMethod("showLoading");
+//            m.invoke(targetObject);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+
+        joinPoint.proceed();
+    }
+}

+ 2 - 0
app/src/main/java/com/miekir/newmvp/TestViewModel1.java

@@ -1,5 +1,6 @@
 package com.miekir.newmvp;
 
+import com.miekir.mvp.base.NeedLoading;
 import com.miekir.mvp.presenter.BaseViewModel;
 
 import java.util.ArrayList;
@@ -13,6 +14,7 @@ import java.util.List;
  * Description:注意ViewModel的类必须是public的,否则无法创建实例
  */
 public class TestViewModel1 extends BaseViewModel {
+    @NeedLoading
     public void go() {
         new Thread(new Runnable() {
             @Override

+ 1 - 1
build.gradle

@@ -7,7 +7,7 @@ buildscript {
         
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.5.2'
+        classpath 'com.android.tools.build:gradle:3.6.1'
         classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files

+ 1 - 1
gradle/wrapper/gradle-wrapper.properties

@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip

+ 0 - 2
mvp/build.gradle

@@ -1,5 +1,4 @@
 apply plugin: 'com.android.library'
-apply plugin: 'com.hujiang.android-aspectjx'
 
 
 android {
@@ -38,5 +37,4 @@ dependencies {
     implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
     implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
 
-    implementation 'org.aspectj:aspectjrt:1.8.+'
 }

+ 15 - 0
mvp/src/main/java/com/miekir/mvp/base/NeedLoading.java

@@ -0,0 +1,15 @@
+package com.miekir.mvp.base;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @description 方法回调标记
+ * @date 2019/8/19
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface NeedLoading {
+}

+ 7 - 0
mvp/src/main/java/com/miekir/mvp/presenter/BaseViewModel.java

@@ -1,5 +1,7 @@
 package com.miekir.mvp.presenter;
 
+import android.util.Log;
+
 import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.ViewModel;
 
@@ -25,6 +27,11 @@ public class BaseViewModel extends ViewModel {
         return liveData;
     }
 
+    // todo
+    public void showLoading() {
+        Log.e("log", "Loading");
+    }
+
     public void post(int responseCode, String msg, Object dataBean, int sourceCode) {
         if (liveData == null) {
             return;