getAndroid.ps1 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (C) Contributors to the Suwayomi project
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
  6. # This is a windows only PowerShell script to create android.jar stubs
  7. # foolproof against running from AndroidCompat dir instead of running from project root
  8. if ($(Split-Path -Path (Get-Location) -Leaf) -eq "AndroidCompat" ) {
  9. Set-Location ..
  10. }
  11. Write-Output "Getting required Android.jar..."
  12. Remove-Item -Recurse -Force "tmp" -ErrorAction SilentlyContinue | Out-Null
  13. New-Item -ItemType Directory -Force -Path "tmp" | Out-Null
  14. $androidEncoded = (Invoke-WebRequest -Uri "https://android.googlesource.com/platform/prebuilts/sdk/+/6cd31be5e4e25901aadf838120d71a79b46d9add/30/public/android.jar?format=TEXT" -UseBasicParsing).content
  15. $android_jar = (Get-Location).Path + "\tmp\android.jar"
  16. [IO.File]::WriteAllBytes($android_jar, [Convert]::FromBase64String($androidEncoded))
  17. # We need to remove any stub classes that we have implementations for
  18. Write-Output "Patching JAR..."
  19. function Remove-Files-Zip($zipfile, $paths)
  20. {
  21. [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression') | Out-Null
  22. $stream = New-Object IO.FileStream($zipfile, [IO.FileMode]::Open)
  23. $mode = [IO.Compression.ZipArchiveMode]::Update
  24. $zip = New-Object IO.Compression.ZipArchive($stream, $mode)
  25. if ($paths.getType().Name -eq "Object[]")
  26. {
  27. $paths | ForEach-Object {
  28. $path = $_
  29. ($zip.Entries | Where-Object { $_.FullName -like $path }) | ForEach-Object { Write-Output "Deleting: $($_.FullName)"; $_.Delete() }
  30. }
  31. }
  32. else
  33. {
  34. ($zip.Entries | Where-Object { $_.FullName -like $paths }) | ForEach-Object { Write-Output "Deleting: $($_.FullName)"; $_.Delete() }
  35. }
  36. $zip.Dispose()
  37. $stream.Close()
  38. $stream.Dispose()
  39. }
  40. Write-Output "Removing org.json..."
  41. Remove-Files-Zip $android_jar 'org/json/*'
  42. Write-Output "Removing org.apache..."
  43. Remove-Files-Zip $android_jar 'org/apache/*'
  44. Write-Output "Removing org.w3c..."
  45. Remove-Files-Zip $android_jar 'org/w3c/*'
  46. Write-Output "Removing org.xml..."
  47. Remove-Files-Zip $android_jar 'org/xml/*'
  48. Write-Output "Removing org.xmlpull..."
  49. Remove-Files-Zip $android_jar 'org/xmlpull/*'
  50. Write-Output "Removing junit..."
  51. Remove-Files-Zip $android_jar 'junit/*'
  52. Write-Output "Removing javax..."
  53. Remove-Files-Zip $android_jar 'javax/*'
  54. Write-Output "Removing java..."
  55. Remove-Files-Zip $android_jar 'java/*'
  56. Write-Output "Removing overriden classes..."
  57. Remove-Files-Zip $android_jar 'android/app/Application.class'
  58. Remove-Files-Zip $android_jar 'android/app/Service.class'
  59. Remove-Files-Zip $android_jar 'android/net/Uri.class'
  60. Remove-Files-Zip $android_jar 'android/net/Uri$Builder.class'
  61. Remove-Files-Zip $android_jar 'android/os/Environment.class'
  62. Remove-Files-Zip $android_jar 'android/text/format/Formatter.class'
  63. Remove-Files-Zip $android_jar 'android/text/Html.class'
  64. function Dedupe($path)
  65. {
  66. Push-Location $path
  67. $classes = Get-ChildItem . *.* -Recurse | Where-Object { !$_.PSIsContainer }
  68. $classes | ForEach-Object {
  69. "Processing class: $($_.FullName)"
  70. Remove-Files-Zip $android_jar ("$($_.Name).class","$($_.Name)$*.class","$($_.Name)Kt.class","$($_.Name)Kt$*.class") | Out-Null
  71. }
  72. Pop-Location
  73. }
  74. Dedupe "AndroidCompat/src/main/java"
  75. Dedupe "server/src/main/kotlin"
  76. Write-Output "Copying Android.jar to library folder..."
  77. Move-Item -Force $android_jar "AndroidCompat/lib/android.jar"
  78. Write-Output "Cleaning up..."
  79. Remove-Item -Recurse -Force "tmp"
  80. Write-Output "Done!"