Files
youtube_personal/REPARER_HD_ANDROID_GRADLE.ps1
T

121 lines
3.2 KiB
PowerShell

param(
[string]$ProjectRoot = $PSScriptRoot
)
$ErrorActionPreference = "Stop"
$appGradle = Join-Path $ProjectRoot "android\app\build.gradle.kts"
if (-not (Test-Path $appGradle)) {
throw "Introuvable: $appGradle"
}
$backup = "$appGradle.before_hd_agp9_fix"
if (-not (Test-Path $backup)) {
Copy-Item $appGradle $backup
Write-Host "Backup: $backup"
}
$text = Get-Content $appGradle -Raw
# Flutter 3.44 / AGP 9 utilise le Kotlin intégré à AGP.
# Le bloc kotlin { compilerOptions { ... } } doit être dans android { ... }.
$topLevelKotlinPattern = '(?ms)^[ \t]*kotlin\s*\{\s*compilerOptions\s*\{\s*jvmTarget\s*=\s*org\.jetbrains\.kotlin\.gradle\.dsl\.JvmTarget\.JVM_17\s*\}\s*\}\s*'
if ($text -match $topLevelKotlinPattern) {
$text = [regex]::Replace(
$text,
$topLevelKotlinPattern,
'',
1
)
Write-Host "Bloc kotlin {} invalide retiré du niveau projet."
}
$androidKotlinBlock = @'
kotlin {
compilerOptions {
jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17
}
}
'@
# Insère le bloc Kotlin juste après compileOptions {} dans android {}.
if (
$text -notmatch '(?ms)android\s*\{.*?kotlin\s*\{\s*compilerOptions'
) {
$compileOptionsPattern = '(?ms)([ \t]*compileOptions\s*\{\s*sourceCompatibility\s*=\s*JavaVersion\.VERSION_17\s*targetCompatibility\s*=\s*JavaVersion\.VERSION_17(?:\s*isCoreLibraryDesugaringEnabled\s*=\s*true)?\s*\}\s*)'
$match = [regex]::Match(
$text,
$compileOptionsPattern
)
if (-not $match.Success) {
throw @"
Impossible de trouver compileOptions Java 17 dans:
$appGradle
Envoie-moi ce fichier et je te ferai un correctif exact.
"@
}
$replacement =
$match.Groups[1].Value +
"`r`n" +
$androidKotlinBlock
$text =
$text.Substring(0, $match.Index) +
$replacement +
$text.Substring(
$match.Index + $match.Length
)
Write-Host "Bloc kotlin {} déplacé dans android {}."
}
# NewPipe utilise des API Java qui nécessitent le desugaring sur Android.
if (
$text -notmatch 'isCoreLibraryDesugaringEnabled\s*=\s*true'
) {
$text = $text.Replace(
'targetCompatibility = JavaVersion.VERSION_17',
"targetCompatibility = JavaVersion.VERSION_17`r`n isCoreLibraryDesugaringEnabled = true"
)
Write-Host "Core library desugaring activé."
}
# Vérifie que les dépendances HD sont toujours présentes.
if (
$text -notmatch 'com\.github\.TeamNewPipe:NewPipeExtractor:v0\.26\.3'
) {
$dependencyBlock = @'
dependencies {
implementation("com.github.TeamNewPipe:NewPipeExtractor:v0.26.3")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs_nio:2.1.5")
}
'@
$text = $text.TrimEnd() + "`r`n" + $dependencyBlock + "`r`n"
Write-Host "Dépendances HD restaurées."
}
Set-Content -Path $appGradle -Value $text -Encoding UTF8
Write-Host ""
Write-Host "Correctif Gradle AGP 9 appliqué."
Write-Host "Fichier: $appGradle"
Write-Host ""
Write-Host "Lance maintenant:"
Write-Host " flutter clean"
Write-Host " flutter pub get"
Write-Host " flutter run -d f10778d8"