昨天看了一段android配置aspectj实现AOP的直播视频,就试着自己配置了一下,可能是因为我自己的AndroidStudio环境的问题,碰到了不少的坑(其实还是因为对gradle理解的不多),但总归是配置好了,就分享一下。
试了两种方式,不过项目下的build.gradle,没什么变化,直接看一下代码吧:
build.gradle(项目下)
buildscript { ext {//android appcompat支持库版本 androidSupportVersion=' 26.1.0 '//编译的SDK版本,如API20 compileSdkVersion=26//构建工具的版本,其中包括了打包工具aapt, dx等,如API20对应的构建工具的版本就是20.0.0 buildToolsVersion=" 26.0.2 "//兼容的最低SDK版本 minSdkVersion=15//向前兼容,保存新旧两种逻辑,并通过if - else方法来判断执行哪种逻辑 targetSdkVersion=26//芬兰湾的科特林版本号 kotlin_version=' 1.2.10 ' kotlinVersion=" org.jetbrains.kotlin: kotlin-stdlib-jre7: kotlin_version美元" appcompatV7=" com.android.support: appcompat-v7: androidSupportVersion美元" appcompatDesign=" com.android.support:设计:$ androidSupportVersion " constraintLayout=' com.android.support.constraint: constraint-layout: 1.0.2 ' } 存储库{ 谷歌() jcenter () mavenCentral () } 依赖关系{ 类路径的com.android.tools.build: gradle: 3.0.1 类路径“org.jetbrains.kotlin: kotlin-gradle-plugin: kotlin_version美元” 类路径org.greenrobot: greendao-gradle-plugin: 3.2.1之上的//注意:不要把你的应用程序依赖关系;他们属于//在单个模块中构建。gradle文件 类路径的org.aspectj: aspectjtools: 1.8.13 类路径的org.aspectj: aspectjweaver: 1.8.13 } } allprojects { 存储库{ 谷歌() jcenter () mavenCentral () } } 任务清洁(类型:删除){ 删除rootProject.buildDir } >之前看着一大堆,主要就是下面这几行配置,其他的是我自己项目中用到的,根据自己需要配置就行。
buildscript { 存储库{ mavenCentral () } 依赖关系{ 类路径的org.aspectj: aspectjtools: 1.8.13 类路径的org.aspectj: aspectjweaver: 1.8.13 } } 存储库{ mavenCentral () } >之前其实这几行配置在应用的build.gradle里也是可以的,但是因为项目下的构建。allprojects gradle里已经有buildscript{},{}}{存储库,就配置在这里了。
<>强然后有两种配置方式:强>
<强>第一种强>
只有一个主模块应用的情况下,配置应用的build.gradle:
应用插件:“com.android.application” 应用插件:“kotlin-android” 应用插件:“kotlin-android-extensions” 应用插件:“org.greenrobot.greendao” android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { applicationId”填入自己的applicationId” minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName“1.0”//λ配置//jackOptions。启用=true//android.compileOptions.sourceCompatibility 1.8 buildConfigField“布尔”、“日志”、“true”//显示日志 testInstrumentationRunner“android.support.test.runner.AndroidJUnitRunner”//支持矢量图 vectorDrawables。useSupportLibrary=true ndk {//选择要添加的对应的cpu类型的,所以库。 abiFilters‘armeabi’,‘armeabi-v7a’,‘arm64-v8a’,‘x86’,‘x86_64’,‘mips’,‘mips64’ } } buildTypes { 释放{ minifyEnabled假 buildConfigField“布尔”、“日志”、“false”//显示日志 proguardFiles getDefaultProguardFile (“proguard-android.txt”)、“proguard-rules.pro” } }//λ配置 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } 数据绑定{ 使真正的 } greendao { schemaVersion 1//数据库版本号 daoPackage com.test.qby.newtestapplication.greendao”//设置DaoMaster, DaoSession, Dao包名 targetGenDir的src/main/java//设置DaoMaster, DaoSession,刀目录//targetGenDirTest:设置生成单元测试目录//generateTests:设置自动生成单元测试用例 } lintOptions { abortOnError真实 } } 依赖关系{ 实现fileTree(包括:[' *。jar”, dir:“填词”) 实现rootProject.ext.kotlinVersion 实现rootProject.ext.appcompatV7 实现rootProject.ext.constraintLayout 编译rootProject.ext.appcompatDesign testImplementation“junit: junit: 4.12” androidTestImplementation com.android.support.test:跑步者:1.0.1的 androidTestImplementation com.android.support.test.espresso: espresso-core: 3.0.1的 编译的jp.wasabeef: glide-transformations: 3.0.1//如果你想使用GPU过滤器 编译“jp.co.cyberagent.android.gpuimage: gpuimage-library: 1.4.1”//腾讯bug 编译的com.tencent.bugly: crashreport: latest.release 编译的com.tencent.bugly: nativecrashreport: latest.release//更新 编译“com.squareup.retrofit2:改造:tripwire” 编译“com.squareup.retrofit2: converter-gson: tripwire” 编译“com.squareup.retrofit2: adapter-rxjava2:2.3.0” 编译的com.squareup.okhttp3:日志记录拦截器:3.9.0//rxJava 编译的io.reactivex.rxjava2: rxandroid: 2.0.1//因为RxAndroid版本是少之又少,建议你也//显式依赖>//AOP面向切面编程,加入这行就不用在填词下引入jar包了,不然要写成编译文件(libs/aspectjrt.jar) 编译的org.aspectj: aspectjrt: 1.8.13 进口org.aspectj.bridge.IMessage 进口org.aspectj.bridge.MessageHandler 进口org.aspectj.tools.ajc.Main 最后的日志=project.logger def 最后def变体=project.android.applicationVariants 变体。所有{变体→ 如果(! variant.buildType.isDebuggable ()) { log.debug(“跳过non-debuggable建立类型“$ {variant.buildType.name}”。) 返回 } JavaCompile JavaCompile=variant.javaCompile javaCompile。doLast { String[]参数=[“-showWeaveInfo”, “-1.5”, “-inpath javaCompile.destinationDir.toString (), null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullAndroidStudio配置AspectJ环境实现AOP的方法