1
+ /*
2
+ * Copyright (C) 2016 ceabie (https://github.com/ceabie/)
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ package com.ceabie.dexknife
17
+
18
+ import org.gradle.api.Plugin
19
+ import org.gradle.api.Project
20
+
21
+ /**
22
+ * the spilt tools plugin.
23
+ */
24
+ public class DexKnifePlugin implements Plugin<Project > {
25
+
26
+ @Override
27
+ void apply (Project project ) {
28
+
29
+ DexKnifeExtension dexKnifeExtension = project. extensions. create(' dexKnife' , DexKnifeExtension )
30
+ project. afterEvaluate {
31
+ boolean hasApp = project. plugins. hasPlugin(" com.android.application" )
32
+ print (" -hasApp = ${ hasApp} \n " )
33
+ if (! hasApp) {
34
+ throw new IllegalStateException (" 'android' plugin required." )
35
+ }
36
+
37
+ final def variants = project. android. applicationVariants
38
+ variants. each{variant ->
39
+ if (dexKnifeExtension. enabled) {
40
+ boolean checkProductFlavor = dexKnifeExtension. productFlavor == " " || dexKnifeExtension. productFlavor. equalsIgnoreCase(variant. flavorName)
41
+ boolean checkBuildType = dexKnifeExtension. buildType == " " || dexKnifeExtension. buildType. equalsIgnoreCase(variant. buildType. name)
42
+ if (checkProductFlavor && checkBuildType) {
43
+ printf " -DexKnifePlugin Enable = true\n " ;
44
+ printf " -DexKnifePlugin checkProductFlavor = ${ checkProductFlavor} \n " ;
45
+ printf " -DexKnifePlugin checkBuildType = ${ checkBuildType} \n " ;
46
+ printf " -DexKnifePlugin buildType.name = ${ variant.buildType.name} \n " ;
47
+ printf " -DexKnifePlugin flavorName = ${ variant.flavorName} \n " ;
48
+
49
+ filterActivity(project);
50
+
51
+ if (isMultiDexEnabled(variant)) {
52
+ if (SplitToolsFor130 . isCompat(variant)) {
53
+ System . err. println (" DexKnife: Compat 1.3.0." );
54
+ SplitToolsFor130 . processSplitDex(project, variant)
55
+ } else if (SplitToolsFor150 . isCompat()) {
56
+ SplitToolsFor150 . processSplitDex(project, variant)
57
+ } else {
58
+ System . err. println (" DexKnife Error: DexKnife is not compatible your Android gradle plugin." );
59
+ }
60
+ } else {
61
+ System . err. println (" DexKnife : MultiDexEnabled is false, it's not work." );
62
+ }
63
+ }
64
+ }
65
+ printf " -DexKnifePlugin Enable = false\n " ;
66
+ }
67
+ }
68
+ }
69
+
70
+ // filter Activity
71
+ private static void filterActivity (Project project ) {
72
+ File file = project. file(DexSplitTools . DEX_KNIFE_CFG_TXT )
73
+ if (file != null ) {
74
+ def justActivitys = [];
75
+ file. eachLine { line ->
76
+ // printf "read line ${line}\n";
77
+ if (line. startsWith(' -just activity' )) {
78
+ line = line. replaceAll(' -just activity' , ' ' ). trim();
79
+ justActivitys. add(line)
80
+ }
81
+ }
82
+ printf " -just activity size = ${ justActivitys.size()} \n " ;
83
+ if (justActivitys. size() != 0 ) {
84
+ project. tasks. each { task ->
85
+ if (task. name. startsWith(' collect' ) && task. name. endsWith(' MultiDexComponents' )) {
86
+ println " main-dex-filter: found task $task . name "
87
+ task. filter { name , attrs ->
88
+ String componentName = attrs. get(' android:name' )
89
+ if (' activity' . equals(name)) {
90
+ def result = justActivitys. find {
91
+ componentName. endsWith(" ${ it} " )
92
+ }
93
+ def bool = result != null ;
94
+ if (bool) {
95
+ printf " main-dex-filter: keep ${ componentName} \n "
96
+ }
97
+ return bool
98
+ }
99
+ return true
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+
107
+ private static boolean isMultiDexEnabled (variant ) {
108
+ def is = variant. buildType. multiDexEnabled
109
+ if (is != null ) {
110
+ return is;
111
+ }
112
+
113
+ is = variant. mergedFlavor. multiDexEnabled
114
+ if (is != null ) {
115
+ return is;
116
+ }
117
+
118
+ return false
119
+ }
120
+
121
+ }
0 commit comments