├── .gitignore ├── .idea ├── encodings.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── lqr │ │ └── androidaopdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── lqr │ │ │ └── androidaopdemo │ │ │ ├── MainActivity.java │ │ │ ├── TestAnnoAspect.java │ │ │ └── TestAnnoTrace.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── lqr │ └── androidaopdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/dictionaries 41 | .idea/libraries 42 | 43 | # Keystore files 44 | *.jks 45 | 46 | # External native build folder generated in Android Studio 2.2 and later 47 | .externalNativeBuild 48 | 49 | # Google Services (e.g. APIs or Firebase) 50 | google-services.json 51 | 52 | # Freeline 53 | freeline.py 54 | freeline/ 55 | freeline_project_description.json 56 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 23 | 33 | 34 | 35 | 36 | 37 | 38 | 40 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 CSDN_LQR 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidAopDemo 2 | Android面向切面编程Demo(AOP) 3 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | import org.aspectj.bridge.IMessage 3 | import org.aspectj.bridge.MessageHandler 4 | import org.aspectj.tools.ajc.Main 5 | 6 | buildscript { 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath 'org.aspectj:aspectjtools:1.8.9' 12 | classpath 'org.aspectj:aspectjweaver:1.8.9' 13 | } 14 | } 15 | android { 16 | compileSdkVersion 26 17 | defaultConfig { 18 | applicationId "com.lqr.androidaopdemo" 19 | minSdkVersion 16 20 | targetSdkVersion 26 21 | versionCode 1 22 | versionName "1.0" 23 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 24 | } 25 | buildTypes { 26 | release { 27 | minifyEnabled false 28 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 29 | } 30 | } 31 | } 32 | 33 | dependencies { 34 | implementation fileTree(dir: 'libs', include: ['*.jar']) 35 | implementation 'com.android.support:appcompat-v7:26.1.0' 36 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 37 | testImplementation 'junit:junit:4.12' 38 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 39 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 40 | 41 | compile 'org.aspectj:aspectjrt:1.8.9' 42 | } 43 | 44 | final def log = project.logger 45 | final def variants = project.android.applicationVariants 46 | 47 | variants.all { variant -> 48 | if (!variant.buildType.isDebuggable()) { 49 | log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.") 50 | return; 51 | } 52 | 53 | JavaCompile javaCompile = variant.javaCompile 54 | javaCompile.doLast { 55 | String[] args = ["-showWeaveInfo", 56 | "-1.8", 57 | "-inpath", javaCompile.destinationDir.toString(), 58 | "-aspectpath", javaCompile.classpath.asPath, 59 | "-d", javaCompile.destinationDir.toString(), 60 | "-classpath", javaCompile.classpath.asPath, 61 | "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)] 62 | log.debug "ajc args: " + Arrays.toString(args) 63 | 64 | MessageHandler handler = new MessageHandler(true); 65 | new Main().run(args, handler); 66 | for (IMessage message : handler.getMessages(null, true)) { 67 | switch (message.getKind()) { 68 | case IMessage.ABORT: 69 | case IMessage.ERROR: 70 | case IMessage.FAIL: 71 | log.error message.message, message.thrown 72 | break; 73 | case IMessage.WARNING: 74 | log.warn message.message, message.thrown 75 | break; 76 | case IMessage.INFO: 77 | log.info message.message, message.thrown 78 | break; 79 | case IMessage.DEBUG: 80 | log.debug message.message, message.thrown 81 | break; 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/lqr/androidaopdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.lqr.androidaopdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.lqr.androidaopdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/lqr/androidaopdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.lqr.androidaopdemo; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | 7 | public class MainActivity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_main); 13 | } 14 | 15 | @TestAnnoTrace(value = "lqr_test", type = 1) 16 | public void test(View view) { 17 | System.out.println("Hello, I am CSDN_LQR"); 18 | int a = 1 / 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/lqr/androidaopdemo/TestAnnoAspect.java: -------------------------------------------------------------------------------- 1 | package com.lqr.androidaopdemo; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.ProceedingJoinPoint; 5 | import org.aspectj.lang.annotation.After; 6 | import org.aspectj.lang.annotation.AfterReturning; 7 | import org.aspectj.lang.annotation.AfterThrowing; 8 | import org.aspectj.lang.annotation.Around; 9 | import org.aspectj.lang.annotation.Aspect; 10 | import org.aspectj.lang.annotation.Before; 11 | import org.aspectj.lang.annotation.Pointcut; 12 | 13 | @Aspect 14 | public class TestAnnoAspect { 15 | 16 | // @Pointcut("execution(* com.lqr.androidaopdemo.MainActivity.test(..))") 17 | @Pointcut("execution(@com.lqr.androidaopdemo.TestAnnoTrace * *(..))") 18 | public void pointcut() { 19 | 20 | } 21 | 22 | @Before("pointcut()") 23 | public void before(JoinPoint point) { 24 | System.out.println("@Before"); 25 | } 26 | 27 | @Around("pointcut()") 28 | public void around(ProceedingJoinPoint joinPoint) throws Throwable { 29 | System.out.println("@Around"); 30 | // MethodSignature signature = (MethodSignature) joinPoint.getSignature(); 31 | // String name = signature.getName(); // 方法名:test 32 | // Method method = signature.getMethod(); // 方法:public void com.lqr.androidaopdemo.MainActivity.test(android.view.View) 33 | // Class returnType = signature.getReturnType(); // 返回值类型:void 34 | // Class declaringType = signature.getDeclaringType(); // 方法所在类名:MainActivity 35 | // String[] parameterNames = signature.getParameterNames(); // 参数名:view 36 | // Class[] parameterTypes = signature.getParameterTypes(); // 参数类型:View 37 | 38 | // TestAnnoTrace annotation = method.getAnnotation(TestAnnoTrace.class); 39 | // String value = annotation.value(); 40 | // int type = annotation.type(); 41 | 42 | // long beginTime = SystemClock.currentThreadTimeMillis(); 43 | joinPoint.proceed(); 44 | // long endTime = SystemClock.currentThreadTimeMillis(); 45 | // long dx = endTime - beginTime; 46 | // System.out.println("耗时:" + dx + "ms"); 47 | } 48 | 49 | @After("pointcut()") 50 | public void after(JoinPoint point) { 51 | System.out.println("@After"); 52 | } 53 | 54 | @AfterReturning("pointcut()") 55 | public void afterReturning(JoinPoint point, Object returnValue) { 56 | System.out.println("@AfterReturning"); 57 | } 58 | 59 | @AfterThrowing(value = "pointcut()", throwing = "ex") 60 | public void afterThrowing(Throwable ex) { 61 | System.out.println("@afterThrowing"); 62 | System.out.println("ex = " + ex.getMessage()); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/lqr/androidaopdemo/TestAnnoTrace.java: -------------------------------------------------------------------------------- 1 | package com.lqr.androidaopdemo; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target({ElementType.METHOD,ElementType.CONSTRUCTOR}) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface TestAnnoTrace { 11 | String value(); 12 | 13 | int type(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 |