@@ -116,6 +116,124 @@ android {
116116 }
117117 }
118118 }
119+
120+ packaging {
121+ resources {
122+ resources. pickFirsts. add(' com/facebook/react/modules/network/ProgressiveStringDecoder.class' )
123+ }
124+ }
125+
126+ // delete the duplicate class before R8 (only for release builds)
127+ applicationVariants. all { variant ->
128+ // only run during release
129+ if (variant. name. contains(' release' )) {
130+ def r8Task = tasks. findByName(" minify${ variant.name.capitalize()} WithR8" )
131+ if (r8Task) {
132+ r8Task. doFirst {
133+ // Search and handle all react-android related JAR file
134+ def processedJars = []
135+
136+ // Search Gradle Cached JAR file - support multiple env
137+ def gradleCachePaths = [
138+ file(" ${ System.getProperty('user.home')} /.gradle/caches" ),
139+ file(" ${ gradle.gradleUserHomeDir} /caches" ),
140+ file(" ${ System.getProperty('GRADLE_USER_HOME', System.getProperty('user.home') + '/.gradle')} /caches" )
141+ ]
142+
143+ def gradleCache = gradleCachePaths. find { it. exists() }
144+ if (gradleCache) {
145+ println " Using Gradle cache directory: ${ gradleCache.absolutePath} "
146+ gradleCache. eachFileRecurse { file ->
147+ if (file. name. contains(' react-android' ) && file. name. endsWith(' .jar' )) {
148+ def tmpDir = new File (" ${ buildDir} /tmp/modifiedReactAndroid_${ file.name} /" )
149+ def backupFile = new File (" ${ buildDir} /tmp/backup_${ file.name} " )
150+
151+ tmpDir. deleteDir()
152+ tmpDir. mkdirs()
153+ backupFile. parentFile. mkdirs()
154+
155+ println " Processing cached react-android JAR: ${ file.absolutePath} "
156+
157+ try {
158+ // backup JAR file
159+ copy {
160+ from file
161+ into backupFile. parent
162+ rename { backupFile. name }
163+ }
164+ if (! project. ext. has(' backupMap' )) {
165+ project. ext. backupMap = [:]
166+ }
167+ project. ext. backupMap[file. absolutePath] = backupFile. absolutePath
168+ println " Backed up ${ file.name} to ${ backupFile.absolutePath} "
169+
170+ // unzip JAR
171+ copy {
172+ from zipTree(file)
173+ into tmpDir
174+ }
175+
176+ // Delete duplicated class file
177+ def conflictClass = new File (" ${ tmpDir} /com/facebook/react/modules/network/ProgressiveStringDecoder.class" )
178+ if (conflictClass. exists()) {
179+ conflictClass. delete()
180+ println " Removed conflicting ProgressiveStringDecoder.class from ${ file.name} "
181+
182+ // RePackage JAR
183+ ant. jar(destfile : file) {
184+ fileset(dir : tmpDir)
185+ }
186+
187+ processedJars. add(file. name)
188+ }
189+ } catch (Exception e) {
190+ println " Warning: Could not process ${ file.name} : ${ e.message} "
191+ }
192+ }
193+ }
194+ } else {
195+ println " No Gradle cache directory found in any of the expected locations"
196+ }
197+
198+ if (processedJars. size() > 0 ) {
199+ println " Successfully processed ${ processedJars.size()} react-android JAR files: ${ processedJars} "
200+ } else {
201+ println " No react-android JAR files found to process"
202+ }
203+ }
204+
205+ // revert the changes
206+ r8Task. doLast {
207+ def backupMap = project. ext. has(' backupMap' ) ? project. ext. backupMap : [:]
208+ backupMap. each { originalPath , backupPath ->
209+ try {
210+ def originalFile = new File (originalPath)
211+ def backupFile = new File (backupPath)
212+
213+ if (backupFile. exists()) {
214+ copy {
215+ from backupFile
216+ into originalFile. parent
217+ rename { originalFile. name }
218+ }
219+ backupFile. delete()
220+ println " Restored ${ originalFile.name} from backup"
221+ }
222+ } catch (Exception e) {
223+ println " Warning: Could not restore ${ originalPath} : ${ e.message} "
224+ }
225+ }
226+
227+ if (backupMap. size() > 0 ) {
228+ println " Successfully restored ${ backupMap.size()} JAR files from backup"
229+ if (project. ext. has(' backupMap' )) {
230+ project. ext. backupMap = [:]
231+ }
232+ }
233+ }
234+ }
235+ }
236+ }
119237}
120238
121239dependencies {
0 commit comments