Skip to content

Commit 6c65cc3

Browse files
fdroid flavor
1 parent 49c3067 commit 6c65cc3

File tree

5 files changed

+115
-8
lines changed

5 files changed

+115
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pnpm-lock.yaml
1313
.zed
1414
.idea
1515
ace-builds
16+
fdroid.bool

hooks/post-process.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,39 @@ deleteDirRecursively(resPath, [
3030
]);
3131
copyDirRecursively(localResPath, resPath);
3232
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+
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

3568
function 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+
/import org\.apache\.cordova\.\*;/,
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+
/public class MainActivity extends CordovaActivity\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+
/super\.onCreate\(savedInstanceState\);/,
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

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ yarn setup
6060
2. Build the project:
6161

6262
```shell
63-
yarn build <platform (android)> <free|paid> <p|prod|d|dev>
63+
yarn build <free|paid> <p|prod|d|dev> [fdroid]
6464
```
6565

66+
**Note**: Add the fdroid flag only if you want to build the F-Droid-compatible version of Acode.
67+
Omit this flag to build the regular APK (Play Store or normal version).
68+
6669
## • Contributing
6770

6871
Acode Editor is an open-source project, and we welcome contributions from the community. To contribute, follow these steps:

utils/scripts/build.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
#! /bin/bash
22

3-
platform="$1"
4-
app="$2"
5-
mode="$3"
3+
app="$1"
4+
mode="$2"
5+
fdroidFlag="$3"
66
webpackmode="development"
77
cordovamode=""
88

9-
if [ -z "$platform" ]
10-
then
11-
platform="android"
9+
root=$(npm prefix)
10+
11+
12+
if [[ "$fdroidFlag" == "fdroid" ]]; then
13+
echo "true" > "$root/fdroid.bool"
14+
cordova plugin remove com.foxdebug.acode.rk.exec.proot
15+
16+
else
17+
echo "false" > "$root/fdroid.bool"
18+
cordova plugin add src/plugins/proot/
1219
fi
1320

1421
if [ -z "$mode" ]
@@ -33,7 +40,7 @@ NC=''
3340
script1="node ./utils/config.js $mode $app"
3441
script2="webpack --progress --mode $webpackmode "
3542
script3="node ./utils/loadStyles.js"
36-
script4="cordova build $platform $cordovamode"
43+
script4="cordova build android $cordovamode"
3744
eval "
3845
echo \"${RED}$script1${NC}\";
3946
$script1;

0 commit comments

Comments
 (0)