11
2- import components.*
2+ import components.PrintUtils
3+ import components.config
4+ import components.createExtension
5+ import components.defaultConfig
6+ import components.registerTask
7+ import components.registerVariantObfuscationTasks
8+ import components.absolutePath
9+ import components.tempPath
310import org.gradle.api.Plugin
411import org.gradle.api.Project
512
@@ -26,7 +33,6 @@ open class StringCare : Plugin<Project> {
2633
2734 @JvmStatic
2835 internal var variantMap = mutableMapOf<String , String >()
29-
3036 }
3137
3238 private lateinit var project: Project
@@ -38,107 +44,26 @@ open class StringCare : Plugin<Project> {
3844 extension = project.createExtension()
3945 absoluteProjectPath = project.absolutePath()
4046
41- this .project.afterEvaluate {
42- this .project.applicationVariants()?.forEach { variant ->
43- variantMap[variant.name] = variant.applicationId
47+ project.plugins.withId(" com.android.application" ) {
48+ // Must register onVariants early; cannot call it from afterEvaluate.
49+ val config = project.config(extension)
50+ configuration = config
51+ project.registerVariantObfuscationTasks(extension, config)
52+ project.afterEvaluate {
53+ if (configuration.debug) {
54+ PrintUtils .print (" PATH" , absoluteProjectPath)
55+ }
56+ project.registerTask(configuration)
4457 }
45- configuration = this .project.config(extension)
58+ }
4659
47- if (configuration.debug) {
48- PrintUtils .print (" PATH" , absoluteProjectPath)
60+ // Fallback when applied to non-application project: still register preview/test tasks
61+ if (! project.plugins.hasPlugin(" com.android.application" )) {
62+ project.afterEvaluate {
63+ configuration = project.config(extension)
64+ project.registerTask(configuration)
4965 }
50- this .project.registerTask(configuration)
5166 }
52- this .project.gradle.addBuildListener(
53- ExecutionListener (
54- debug = configuration.debug,
55- dataFound = { _, _ ->
56- // nothing to do here
57- },
58- mergeResourcesStart = { module, variant ->
59- configuration.name = module
60- if (variantMap.containsKey(variant)) {
61- configuration.applicationId = variantMap[variant] ? : " "
62- }
63- PrintUtils .print (" " , " ApplicationId: ${configuration.applicationId} " , tab = true )
64- fingerPrint(module, variant, configuration) { key ->
65- if (configuration.skip) {
66- PrintUtils .print (module, " Skipping $variant " )
67- return @fingerPrint
68- }
69-
70- if (" none" == key || key.trim().isEmpty()) {
71- PrintUtils .print (" No SHA1 key found for :$module :$variant " )
72- return @fingerPrint
73- }
74-
75- PrintUtils .print (module, " $variant :$key " )
76- PrintUtils .print (module, backupStringRes)
77- backupResourceFiles(absoluteProjectPath, configuration)
78-
79-
80- val files = locateResourceFiles(absoluteProjectPath, configuration)
81- files.forEach { file ->
82- modifyXML(file.file, key, configuration)
83- }
84- }
85- PrintUtils .print (module, obfuscateStringRes)
86- },
87- mergeResourcesFinish = { module, _ ->
88- if (configuration.skip) {
89- return @ExecutionListener
90- }
91- PrintUtils .print (module, restoreStringRes)
92- restoreResourceFiles(absoluteProjectPath, module)
93- },
94- mergeAssetsStart = { module, variant ->
95- configuration.name = module
96- if (variantMap.containsKey(variant)) {
97- configuration.applicationId = variantMap[variant] ? : " "
98- }
99- PrintUtils .print (" " , " ApplicationId: ${configuration.applicationId} " , tab = true )
100- fingerPrint(module, variant, configuration) { key ->
101- if (configuration.skip) {
102- PrintUtils .print (module, " Skipping $variant " )
103- return @fingerPrint
104- }
105-
106- if (" none" == key || key.trim().isEmpty()) {
107- PrintUtils .print (" No SHA1 key found for :$module :$variant " )
108- return @fingerPrint
109- }
110-
111- PrintUtils .print (module, " $variant :$key " )
112- PrintUtils .print (module, backupAssets)
113- backupAssetsFiles(absoluteProjectPath, configuration)
114-
115-
116- val files = locateAssetsFiles(absoluteProjectPath, configuration)
117- files.forEach { file ->
118- if (configuration.debug) {
119- PrintUtils .print (null , file.file.getContent())
120- }
121- obfuscateFile(
122- key,
123- file.file,
124- configuration.applicationId
125- )
126- if (configuration.debug) {
127- PrintUtils .print (null , file.file.getContent())
128- }
129- }
130- PrintUtils .print (module, obfuscateAssets)
131- }
132- },
133- mergeAssetsFinish = { module, _ ->
134- if (configuration.skip) {
135- return @ExecutionListener
136- }
137- PrintUtils .print (module, restoreAssets)
138- restoreAssetsFiles(absoluteProjectPath, module)
139- }
140-
141- ))
14267 }
14368
14469 open class Extension {
@@ -159,7 +84,4 @@ open class StringCare : Plugin<Project> {
15984 var applicationId = " "
16085 var mockedFingerprint = " "
16186 }
162-
16387}
164-
165-
0 commit comments