|
1 | 1 | /* eslint-disable no-console */
|
2 | 2 | const path = require('path');
|
3 | 3 | const fs = require('fs');
|
| 4 | +const { execSync } = require('child_process'); |
4 | 5 |
|
5 | 6 | const buildFilePath = path.resolve(__dirname, '../build.json');
|
6 | 7 | const copyToPath = path.resolve(__dirname, '../platforms/android/build.json');
|
@@ -28,6 +29,127 @@ deleteDirRecursively(resPath, [
|
28 | 29 | 'xml',
|
29 | 30 | ]);
|
30 | 31 | copyDirRecursively(localResPath, resPath);
|
| 32 | +enableLegacyJni(); |
| 33 | +enableStaticContext(); |
| 34 | +patchTargetSdkVersion(); |
| 35 | + |
| 36 | + |
| 37 | +function patchTargetSdkVersion() { |
| 38 | + const prefix = execSync('npm prefix').toString().trim(); |
| 39 | + const gradleFile = path.join(prefix, 'platforms/android/app/build.gradle'); |
| 40 | + |
| 41 | + if (!fs.existsSync(gradleFile)) { |
| 42 | + console.warn('[Cordova Hook] ⚠️ build.gradle not found'); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + let content = fs.readFileSync(gradleFile, 'utf-8'); |
| 47 | + |
| 48 | + const sdkRegex = /targetSdkVersion\s+(cordovaConfig\.SDK_VERSION|\d+)/; |
| 49 | + |
| 50 | + if (sdkRegex.test(content)) { |
| 51 | + let api = "35"; |
| 52 | + const froidFlag = path.join(prefix, 'fdroid.bool'); |
| 53 | + |
| 54 | + if (fs.existsSync(froidFlag)) { |
| 55 | + const fdroid = fs.readFileSync(froidFlag, 'utf-8').trim(); |
| 56 | + if (fdroid == "true") { |
| 57 | + api = "28"; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + content = content.replace(sdkRegex, 'targetSdkVersion ' + api); |
| 62 | + fs.writeFileSync(gradleFile, content, 'utf-8'); |
| 63 | + console.log('[Cordova Hook] ✅ Patched targetSdkVersion to ' + api); |
| 64 | + } else { |
| 65 | + console.warn('[Cordova Hook] ⚠️ targetSdkVersion not found'); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +function enableLegacyJni() { |
| 71 | + const prefix = execSync('npm prefix').toString().trim(); |
| 72 | + const gradleFile = path.join(prefix, 'platforms/android/app/build.gradle'); |
| 73 | + |
| 74 | + if (!fs.existsSync(gradleFile)) return; |
| 75 | + |
| 76 | + let content = fs.readFileSync(gradleFile, 'utf-8'); |
| 77 | + // Check for correct block to avoid duplicate insertion |
| 78 | + if (content.includes('useLegacyPackaging = true')) return; |
| 79 | + |
| 80 | + // Inject under android block with correct Groovy syntax |
| 81 | + content = content.replace(/android\s*{/, match => { |
| 82 | + return ( |
| 83 | + match + |
| 84 | + ` |
| 85 | + packagingOptions { |
| 86 | + jniLibs { |
| 87 | + useLegacyPackaging = true |
| 88 | + } |
| 89 | + }` |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + fs.writeFileSync(gradleFile, content, 'utf-8'); |
| 94 | + console.log('[Cordova Hook] ✅ Enabled legacy JNI packaging'); |
| 95 | +} |
| 96 | + |
| 97 | +function enableStaticContext() { |
| 98 | + try { |
| 99 | + const prefix = execSync('npm prefix').toString().trim(); |
| 100 | + const mainActivityPath = path.join( |
| 101 | + prefix, |
| 102 | + 'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java' |
| 103 | + ); |
| 104 | + |
| 105 | + if (!fs.existsSync(mainActivityPath)) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + let content = fs.readFileSync(mainActivityPath, 'utf-8'); |
| 110 | + |
| 111 | + // Skip if fully patched |
| 112 | + if ( |
| 113 | + content.includes('WeakReference<Context>') && |
| 114 | + content.includes('public static Context getContext()') && |
| 115 | + content.includes('weakContext = new WeakReference<>(this);') |
| 116 | + ) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + // Add missing imports |
| 121 | + if (!content.includes('import java.lang.ref.WeakReference;')) { |
| 122 | + content = content.replace( |
| 123 | + /import org\.apache\.cordova\.\*;/, |
| 124 | + match => |
| 125 | + match + |
| 126 | + '\nimport android.content.Context;\nimport java.lang.ref.WeakReference;' |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + // Inject static field and method into class body |
| 131 | + content = content.replace( |
| 132 | + /public class MainActivity extends CordovaActivity\s*\{/, |
| 133 | + match => |
| 134 | + match + |
| 135 | + `\n\n private static WeakReference<Context> weakContext;\n\n` + |
| 136 | + ` public static Context getContext() {\n` + |
| 137 | + ` return weakContext != null ? weakContext.get() : null;\n` + |
| 138 | + ` }\n` |
| 139 | + ); |
| 140 | + |
| 141 | + // Insert weakContext assignment inside onCreate |
| 142 | + content = content.replace( |
| 143 | + /super\.onCreate\(savedInstanceState\);/, |
| 144 | + `super.onCreate(savedInstanceState);\n weakContext = new WeakReference<>(this);` |
| 145 | + ); |
| 146 | + |
| 147 | + fs.writeFileSync(mainActivityPath, content, 'utf-8'); |
| 148 | + } catch (err) { |
| 149 | + console.error('[Cordova Hook] ❌ Failed to patch MainActivity:', err.message); |
| 150 | + } |
| 151 | +} |
| 152 | + |
31 | 153 |
|
32 | 154 | /**
|
33 | 155 | * Copy directory recursively
|
|
0 commit comments