android中是Aspect 进行埋点笔记

package net.sourceforge.logger;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({
    
    ElementType.CONSTRUCTOR, ElementType.METHOD})
public @interface LoggerTrace {
    
    

}

package net.sourceforge.logger;

import android.util.Log;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Logger {
    
    
    private static String TAG = Logger.class.getSimpleName();

    @Pointcut("execution(@net.sourceforge.logger.LoggerTrace * *(..))")
    public void methodAnnotated() {
    
    

    }

    @Before("methodAnnotated()")
    public void beforeExecute(JoinPoint point) {
    
    
        Log.d(TAG, "beforeExecute: " + point.toLongString());
    }

    @After("methodAnnotated()")
    public void afterExecute(JoinPoint point) {
    
    
        Log.d(TAG, "afterExecute: " + point.toLongString());
        point.getThis().getClass().getAnnotations();
    }
}

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    
    
    ext.kotlin_version = '1.3.61'
    repositories {
    
    
        google()
        jcenter()

    }
    dependencies {
    
    
        classpath 'com.android.tools.build:gradle:3.5.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    
    
    repositories {
    
    
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    
    
    delete rootProject.buildDir
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    
    
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    lintOptions {
    
    
        abortOnError false
    }

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

}

dependencies {
    
    
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    api 'org.aspectj:aspectjrt:1.8.13'
}



apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'android-aspectjx'

android {
    
    
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
    
    
        applicationId "net.sourceforge.bluetooth"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    
    
        release {
    
    
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
aspectjx {
    
    
    //指定只对含有关键字'universal-image-loader', 'AspectJX-Demo/library'的库进行织入扫描,忽略其他库,提升编译效率
//    includeJarFilter 'universal-image-loader', 'AspectJX-Demo/library'
//    excludeJarFilter '.jar'
//    ajcArgs '-Xlint:warning'
}
dependencies {
    
    
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation project(':logger')
}

package net.sourceforge.bluetooth

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_second.*
import net.sourceforge.logger.LoggerTrace

class SecondActivity : AppCompatActivity() {
    
    

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        button2.setOnClickListener {
    
    
            adobe()
        }
    }

    @LoggerTrace
    private fun adobe() {
    
    
        Toast.makeText(this, "adobe", Toast.LENGTH_SHORT).show()
    }

}

猜你喜欢

转载自blog.csdn.net/genmenu/article/details/103614649