-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote3.gradle
452 lines (396 loc) · 16.4 KB
/
remote3.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
def isRelease(){
for (String s : gradle.startParameter.taskNames) {
if (s.contains("ultiChannel") | s.contains("elease")) {
return true
}
}
return false
}
rootProject.buildscript {
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
/* if(isRelease()){
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}else{
classpath 'io.github.wurensen:gradle-android-plugin-aspectjx:2.0.11'
}*/
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
classpath 'io.github.didi.dokit:dokitx-plugin:3.5.0.1'
//lastversion:3.5.0;kotlin编译插件为1.4.32 ;支持Gradle 6.8及以上
//lastversion:3.5.0.1; kotlin编译插件为1.3.72; 支持Gradle 6.8及以下
}
}
def hasApplyAspectjx(Project project0){
return project0.plugins.findPlugin("com.hujiang.android-aspectjx") != null || project0.plugins.findPlugin("android-aspectjx") != null
}
def hasApplyDokit(Project project0){
return project0.plugins.findPlugin("com.didi.dokit") != null
}
def getPkgName(Project project0){
String pkgname = project0.android.defaultConfig.applicationId
if(pkgname.indexOf(".") != pkgname.lastIndexOf(".")){
return pkgname.substring(0,pkgname.lastIndexOf("."));
}
return pkgname;
/*project0.android.applicationVariants.all { variant ->
def applicationId = [variant.mergedFlavor.applicationId, variant.buildType.applicationIdSuffix].findAll().join()
if(applicationId != null){
return applicationId
}
}
return "null-->"*/
}
def isLocal(){
for(Project p : subprojects){
if( p.getName().contains("flipper")){
return true;
}
}
return false;
}
def doUseAspectjx(){
try {
return flipper_use_aspectjx.toBoolean()
}catch(Throwable throwable){
//throwable.printStackTrace()
return false;
}
}
def doUseDokitPlugin(){
try {
return flipper_use_dokit.toBoolean()
}catch(Throwable throwable){
// throwable.printStackTrace()
return false;
}
}
def getSlowMethodAsmPkgNames(Project project0){
try {
return DOKIT_METHOD_PKGS.toString().split(",")
}catch(Throwable throwable){
throwable.printStackTrace()
// return [getPkgName()];
return []
}
}
//println("<--------flipper_use_aspectjx 直接取 :"+doUseAspectjx());
//println("<--------flipper_use_aspectjx 从gradle.getProperties()里取:"+gradle.getProperties().get("flipper_use_aspectjx"));
def flipper
def flippernoop
def dokit
if(isLocal()){
dokit = project(':dokit')
flipper = project(':flipper')
flippernoop = project(':flipper-no-op')
}else {
flipper = 'com.github.hss01248.flipperUtil:flipper:1.4.5'
dokit = 'com.github.hss01248.flipperUtil:dokit:1.4.5'
flippernoop = 'com.github.hss01248.flipperUtil:flipper-no-op:1.4.5'
}
gradle.addBuildListener(new BuildListener() {
@Override
void buildStarted(Gradle gradle) {
}
@Override
void settingsEvaluated(Settings settings) {
println("---->settingsEvaluated:"+settings)
}
@Override
void projectsLoaded(Gradle gradle) {
println("---->projectsLoaded:"+gradle)
}
@Override
void projectsEvaluated(Gradle gradle) {
println("---->projectsEvaluated:"+gradle)
}
@Override
void buildFinished(BuildResult buildResult) {
}
})
def hasAddTimeListener = false;
gradle.addProjectEvaluationListener(new ProjectEvaluationListener() {
@Override
void beforeEvaluate(Project project) {
println("---->beforeEvaluate:"+project)
//println("---->beforeEvaluate:"+project.plugins)
/*if(project.name.contains("app") || project.name.contains("demo")
|| project.name.contains("Demo") || project.name.contains("sample")
|| project.name.contains("Sample")){
}*/
/* if(mybeApp(project.displayName)){
project.dependencies {
if(!isRelease()){
project.dependencies {
implementation flipper
}
}else {
project.dependencies {
implementation flippernoop
}
}
}
}*/
}
@Override
void afterEvaluate(Project project, ProjectState projectState) {
println("---->afterEvaluate:"+project)
//todo 遗留问题 如果app工程本身apply了 aspectjx插件,那么因为这里的implementation flipper在工程的apply plugin aspectjx之后,
// 那么里面的okhttp切面无法被插件切入到,必须是在apply 插件之前.
// 可以选择自己添加依赖到工程的dependencies块里
if(project.plugins.findPlugin("com.android.application") != null){
project.repositories {
maven { url 'https://jitpack.io' }
mavenCentral()
google()
jcenter()
}
if(!hasAddTimeListener){
hasAddTimeListener = true
gradle.addListener(new CollectTaskTimeListener())
println("---->gradle.addListener(new CollectTaskTimeListener()):"+project)
}
if(!isRelease()){
//添加自定义的监听
project.dependencies {
//implementation('com.github.hss01248:ActivityTaskView:5.0.1')
implementation flipper
implementation dokit
//implementation "com.github.simplepeng.SpiderMan:spiderman:v1.1.8"
// implementation 'com.facebook.flipper:flipper:0.135.0'
}
project.configurations {
all*.exclude group: 'me.weishu', module: 'free_reflection'
all {
resolutionStrategy {
force "androidx.sqlite:sqlite-framework:2.1.0"
force "com.squareup.okhttp3:okhttp:3.12.12"
}
}
}
}else {
project.dependencies {
implementation flippernoop
}
}
//UnsatisfiedLinkError: couldn't find DSO to load: libfbjni.so
//todo 判断: 如果(RN)工程本来有依赖 group: 'com.facebook.fbjni', module: 'fbjni-java-only',那么就排除module: 'fbjni'
//all*.exclude group: 'com.facebook.fbjni', module: 'fbjni'
//all*.exclude group: 'com.facebook.fbjni', module: 'fbjni-java-only'
println("---->isLocal():"+isLocal())
//一些中型工程项目使用aspctjx就很慢了,建议项目里使用自己的include
if (!isRelease() && !hasApplyAspectjx(project) ){
println("apply plugin: 'com.hujiang.android-aspectjx' by hss01248/flipperUtil, add flipper networkinterceptor to okhttpclient automatically")
project.apply plugin: 'com.hujiang.android-aspectjx'
//为加快编译速度,需要自己将扫描的包路径添加到include里. 性能差距: 4min vs 4s
project.aspectjx {
//排除所有package路径中包含`android.support`的class文件及库(jar文件):Invalid byte tag in constant pool
exclude 'com.google', 'com.taobao', 'module-info', 'com.squareup.haha', 'versions.9', 'com.tencent',
'androidx',
//'com.squareup',
'com.alipay',
'org.apache',
'com.alipay',
'cn.jiguang',
//'com.github',
'com.meizu',
'com.huawei',
'com.qiyukf',
'com.sina',
//'io.reactivex',
//'de.greenrobot.event',
'com.netease.neliveplayer',
'com.umeng',
'im.yixin',
'com.commonsware',
'io.fabric',
'rx.android',
'com.android'
//不使用include时,项目一大就很慢,分钟级别,所以提供开关
include 'okhttp3','com.hss01248','androidx.lifecycle','org.greenrobot.eventbus','com.facebook.react','com.facebook.flipper.plugins.databases.impl',
'com.didichuxing.doraemonkit.aop','com.hss01248.flipper.aop','wendu.dsbridge','com.mcuking.mwbpcontainer','org.greenrobot.eventbus',
'com.sensorsdata.analytics.android.sdk','io.reactivex'
}
}else {
println("not apply aspectjx ,not add flipper networkinterceptor to okhttpclient automatically," +
"because isRelease or hasApplyAspectjx before or flipper_use_aspectjx is not true :\n " +
"isRelease:"+isRelease()+",hasApplyAspectjx:"+hasApplyAspectjx(project)+",flipper_use_aspectjx:"+doUseAspectjx())
if(hasApplyAspectjx(project)){
println("to enable auto inject of aspectj to okhttpclientbulider to all libs,you should add implementation com.github.hss01248.flipperUtil:flipper:1.0.9 manually in depencies")
}
}
//dokit插件
if(!doUseDokitPlugin()){
println("dokit插件默认为关闭状态, 你可以在properties文件中配置flipper_use_dokit=true来打开它")
return
}
if(!isRelease()){
if(hasApplyDokit(project)){
println("已经应用了dokit插件,不再重复使用. 可以参考此脚本配置慢函数打印上报,阈值80ms. 超过阈值的,且运行在主线程的,被认为是慢函数")
return
}
println("你的项目没有应用dokit插件,使用默认配置. 推荐拷贝此处脚本,自行配置插桩的包名: packageNames")
println("应用包名往前一个:"+getPkgName(project)+" , 默认使用此作为插桩包名")
project.ext{
DOKIT_PLUGIN_SWITCH=true
DOKIT_METHOD_SWITCH=true
DOKIT_LOG_SWITCH=true
DOKIT_METHOD_STRATEGY=1
/*# dokit全局配置
# 插件开关
DOKIT_PLUGIN_SWITCH=true
# DOKIT读取三方库会和booster冲突 如果你的项目中也集成了booster 建议将开关改成false
DOKIT_THIRD_LIB_SWITCH=true
# 插件日志
DOKIT_LOG_SWITCH=true
# 自定义Webview的全限定名 主要是作用于h5 js抓包和数据mock
DOKIT_WEBVIEW_CLASS_NAME=com/didichuxing/doraemonkit/widget/webview/MyWebView
# dokit 慢函数开关
DOKIT_METHOD_SWITCH=true
# dokit 函数调用栈层级
DOKIT_METHOD_STACK_LEVEL=2
# 0:默认模式 打印函数调用栈 需添加指定入口 默认为application onCreate 和attachBaseContext
# 1:普通模式 运行时打印某个函数的耗时 全局业务代码函数插入
DOKIT_METHOD_STRATEGY=1*/
}
project.apply plugin: 'com.didi.dokit'
project.dokitExt {
//通用设置
comm {
//地图经纬度开关
gpsSwitch false
//网络开关
networkSwitch false
//大图开关
bigImgSwitch false
//webView js 抓包
webViewSwitch false
}
slowMethod {
//普通模式配置 对应gradle.properties中DOKIT_METHOD_STRATEGY=1
normalMethod {
//默认值为 80ms 小于该值的函数在运行时不会在控制台中被打印
thresholdTime 80
//需要针对函数插装的包名 千万不要用我默认的配置 如果有特殊需求修改成项目中自己的项目包名 假如不需要可以去掉该字段
//getSlowMethodAsmPkgNames(project) 不能动态,只能静态配置
packageNames = ["com.facebook","com.hss01248",getPkgName(project)]
//getPkgName(project)
//不需要针对函数插装的包名&类名 千万不要用我默认的配置 如果有特殊需求修改成项目中自己的项目包名 假如不需要可以去掉该字段
methodBlacklist = ["com.didichuxing.doraemondemo.dokit"]
}
}
}
println("插桩包名:" + project.dokitExt.slowMethod.normalMethod.packageNames)
}
}
}
})
allprojects {
repositories {
mavenLocal()
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
def mybeApp(name){
return name != null && ( name.contains("app") || name.contains("demo")
|| name.contains("Demo") || name.contains("ample") )
}
/*def mybeApp(name){
return name != null && ( name.contains("app") || name.contains("demo")
|| name.contains("Demo") || name.contains("ample") )
}
rootProject.subprojects {
println(project.plugins.findPlugin("com.android.application")+"---->"+project.displayName)
if(mybeApp(project.displayName)){
project.dependencies {
if(!isRelease()){
project.dependencies {
implementation flipper
}
}else {
project.dependencies {
implementation flippernoop
}
}
}
}
}*/
//统计各task时间并排序
class CollectTaskTimeListener implements TaskExecutionListener,BuildListener{
private long mStartTime;
private def timings = new HashMap<String, Long>() //存储所有task和其所发时间的对应关系
private def final MIN_COST = 5
@Override
void buildStarted(Gradle gradle) {
}
@Override
void settingsEvaluated(Settings settings) {
}
@Override
void projectsLoaded(Gradle gradle) {
}
@Override
void projectsEvaluated(Gradle gradle) {
}
@Override
void buildFinished(BuildResult result) {
//输出排序后的统计数据
outputHeader("Task timings(sorted): ")
outputProfile(sortProfileData(timings).iterator())
println("\n")
outputTotalTime(timings.iterator())
}
@Override
void beforeExecute(Task task) {
mStartTime = System.currentTimeMillis();
}
@Override
void afterExecute(Task task, TaskState state) {
long ms = System.currentTimeMillis()-mStartTime;
timings.put(task.path, ms)
task.project.logger.warn "${task.path} took ${ms}ms"
}
void outputHeader(String headerMessage) {
println("\n======================================================")
println(headerMessage)
}
//输出收集的数据
void outputProfile(Iterator<Map.Entry<String, Long>> it) {
for (entry in it) {
if (entry.value >= MIN_COST) {
printf("%-50s %-15s\n", entry.key, entry.value + "ms")
}
}
}
//对task所花费的时间进行排序
List<Map<String, Long>> sortProfileData(Map<String, Long> profileData) {
List<Map.Entry<String, Long>> data = new ArrayList<>()
for (timing in profileData) data.add(timing)
Collections.sort(data, new Comparator<Map.Entry<String, Long>>() {
@Override
int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
if (o1.value > o2.value) return -1
else if (o1.value < o2.value) return 1
return 0
}
})
return data
}
void outputTotalTime(Iterator<Map.Entry<String, Long>> it){
long totalTime
for (entry in it) {
totalTime += entry.value;
}
long minte = totalTime/1000/60;
long second = totalTime/1000-minte*60;
long milion = totalTime-second*1000-minte*60*1000;
println("Total Tasks took ${minte}min ${second}s ${milion}ms")
}
}