@@ -30,6 +30,39 @@ deleteDirRecursively(resPath, [
3030] ) ;
3131copyDirRecursively ( localResPath , resPath ) ;
3232enableLegacyJni ( )
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 = / t a r g e t S d k V e r s i o n \s + ( c o r d o v a C o n f i g \. S D K _ V E R S I O N | \d + ) / ;
49+
50+ if ( sdkRegex . test ( content ) ) {
51+ const fdroid = fs . readFileSync ( path . join ( prefix , 'fdroid.bool' ) , 'utf-8' ) ;
52+ var api = "34"
53+ if ( fdroid === "true" ) {
54+ api = "28"
55+ } else {
56+
57+ }
58+
59+ content = content . replace ( sdkRegex , 'targetSdkVersion ' + api ) ;
60+ fs . writeFileSync ( gradleFile , content , 'utf-8' ) ;
61+ console . log ( '[Cordova Hook] ✅ Patched targetSdkVersion to ' + api ) ;
62+ } else {
63+ console . warn ( '[Cordova Hook] ⚠️ targetSdkVersion not found' ) ;
64+ }
65+ }
3366
3467
3568function enableLegacyJni ( ) {
@@ -59,6 +92,63 @@ function enableLegacyJni() {
5992 console . log ( '[Cordova Hook] ✅ Enabled legacy JNI packaging' ) ;
6093}
6194
95+ function enableStaticContext ( ) {
96+ try {
97+ const prefix = execSync ( 'npm prefix' ) . toString ( ) . trim ( ) ;
98+ const mainActivityPath = path . join (
99+ prefix ,
100+ 'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java'
101+ ) ;
102+
103+ if ( ! fs . existsSync ( mainActivityPath ) ) {
104+ return ;
105+ }
106+
107+ let content = fs . readFileSync ( mainActivityPath , 'utf-8' ) ;
108+
109+ // Skip if fully patched
110+ if (
111+ content . includes ( 'WeakReference<Context>' ) &&
112+ content . includes ( 'public static Context getContext()' ) &&
113+ content . includes ( 'weakContext = new WeakReference<>(this);' )
114+ ) {
115+ return ;
116+ }
117+
118+ // Add missing imports
119+ if ( ! content . includes ( 'import java.lang.ref.WeakReference;' ) ) {
120+ content = content . replace (
121+ / i m p o r t o r g \. a p a c h e \. c o r d o v a \. \* ; / ,
122+ match =>
123+ match +
124+ '\nimport android.content.Context;\nimport java.lang.ref.WeakReference;'
125+ ) ;
126+ }
127+
128+ // Inject static field and method into class body
129+ content = content . replace (
130+ / p u b l i c c l a s s M a i n A c t i v i t y e x t e n d s C o r d o v a A c t i v i t y \s * \{ / ,
131+ match =>
132+ match +
133+ `\n\n private static WeakReference<Context> weakContext;\n\n` +
134+ ` public static Context getContext() {\n` +
135+ ` return weakContext != null ? weakContext.get() : null;\n` +
136+ ` }\n`
137+ ) ;
138+
139+ // Insert weakContext assignment inside onCreate
140+ content = content . replace (
141+ / s u p e r \. o n C r e a t e \( s a v e d I n s t a n c e S t a t e \) ; / ,
142+ `super.onCreate(savedInstanceState);\n weakContext = new WeakReference<>(this);`
143+ ) ;
144+
145+ fs . writeFileSync ( mainActivityPath , content , 'utf-8' ) ;
146+ } catch ( err ) {
147+ console . error ( '[Cordova Hook] ❌ Failed to patch MainActivity:' , err . message ) ;
148+ }
149+ }
150+
151+
62152/**
63153 * Copy directory recursively
64154 * @param {string } src Source directory
0 commit comments