@@ -30,6 +30,37 @@ 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' ) . trim ( ) ;
52+ var api = "34"
53+ if ( fdroid == "true" ) {
54+ api = "28"
55+ }
56+
57+ content = content . replace ( sdkRegex , 'targetSdkVersion ' + api ) ;
58+ fs . writeFileSync ( gradleFile , content , 'utf-8' ) ;
59+ console . log ( '[Cordova Hook] ✅ Patched targetSdkVersion to ' + api ) ;
60+ } else {
61+ console . warn ( '[Cordova Hook] ⚠️ targetSdkVersion not found' ) ;
62+ }
63+ }
3364
3465
3566function enableLegacyJni ( ) {
@@ -59,6 +90,63 @@ function enableLegacyJni() {
5990 console . log ( '[Cordova Hook] ✅ Enabled legacy JNI packaging' ) ;
6091}
6192
93+ function enableStaticContext ( ) {
94+ try {
95+ const prefix = execSync ( 'npm prefix' ) . toString ( ) . trim ( ) ;
96+ const mainActivityPath = path . join (
97+ prefix ,
98+ 'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java'
99+ ) ;
100+
101+ if ( ! fs . existsSync ( mainActivityPath ) ) {
102+ return ;
103+ }
104+
105+ let content = fs . readFileSync ( mainActivityPath , 'utf-8' ) ;
106+
107+ // Skip if fully patched
108+ if (
109+ content . includes ( 'WeakReference<Context>' ) &&
110+ content . includes ( 'public static Context getContext()' ) &&
111+ content . includes ( 'weakContext = new WeakReference<>(this);' )
112+ ) {
113+ return ;
114+ }
115+
116+ // Add missing imports
117+ if ( ! content . includes ( 'import java.lang.ref.WeakReference;' ) ) {
118+ content = content . replace (
119+ / i m p o r t o r g \. a p a c h e \. c o r d o v a \. \* ; / ,
120+ match =>
121+ match +
122+ '\nimport android.content.Context;\nimport java.lang.ref.WeakReference;'
123+ ) ;
124+ }
125+
126+ // Inject static field and method into class body
127+ content = content . replace (
128+ / 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 * \{ / ,
129+ match =>
130+ match +
131+ `\n\n private static WeakReference<Context> weakContext;\n\n` +
132+ ` public static Context getContext() {\n` +
133+ ` return weakContext != null ? weakContext.get() : null;\n` +
134+ ` }\n`
135+ ) ;
136+
137+ // Insert weakContext assignment inside onCreate
138+ content = content . replace (
139+ / 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 \) ; / ,
140+ `super.onCreate(savedInstanceState);\n weakContext = new WeakReference<>(this);`
141+ ) ;
142+
143+ fs . writeFileSync ( mainActivityPath , content , 'utf-8' ) ;
144+ } catch ( err ) {
145+ console . error ( '[Cordova Hook] ❌ Failed to patch MainActivity:' , err . message ) ;
146+ }
147+ }
148+
149+
62150/**
63151 * Copy directory recursively
64152 * @param {string } src Source directory
0 commit comments