Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pnpm-lock.yaml
.zed
.idea
ace-builds
fdroid.bool
90 changes: 90 additions & 0 deletions hooks/post-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ deleteDirRecursively(resPath, [
]);
copyDirRecursively(localResPath, resPath);
enableLegacyJni()
enableStaticContext()
patchTargetSdkVersion()


function patchTargetSdkVersion() {
const prefix = execSync('npm prefix').toString().trim();
const gradleFile = path.join(prefix, 'platforms/android/app/build.gradle');

if (!fs.existsSync(gradleFile)) {
console.warn('[Cordova Hook] ⚠️ build.gradle not found');
return;
}

let content = fs.readFileSync(gradleFile, 'utf-8');

const sdkRegex = /targetSdkVersion\s+(cordovaConfig\.SDK_VERSION|\d+)/;

if (sdkRegex.test(content)) {
const fdroid = fs.readFileSync(path.join(prefix,'fdroid.bool'), 'utf-8');
var api = "34"
if(fdroid === "true"){
api = "28"
}else{

}

content = content.replace(sdkRegex, 'targetSdkVersion '+api);
fs.writeFileSync(gradleFile, content, 'utf-8');
console.log('[Cordova Hook] ✅ Patched targetSdkVersion to '+api);
} else {
console.warn('[Cordova Hook] ⚠️ targetSdkVersion not found');
}
}


function enableLegacyJni() {
Expand Down Expand Up @@ -59,6 +92,63 @@ function enableLegacyJni() {
console.log('[Cordova Hook] ✅ Enabled legacy JNI packaging');
}

function enableStaticContext() {
try {
const prefix = execSync('npm prefix').toString().trim();
const mainActivityPath = path.join(
prefix,
'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java'
);

if (!fs.existsSync(mainActivityPath)) {
return;
}

let content = fs.readFileSync(mainActivityPath, 'utf-8');

// Skip if fully patched
if (
content.includes('WeakReference<Context>') &&
content.includes('public static Context getContext()') &&
content.includes('weakContext = new WeakReference<>(this);')
) {
return;
}

// Add missing imports
if (!content.includes('import java.lang.ref.WeakReference;')) {
content = content.replace(
/import org\.apache\.cordova\.\*;/,
match =>
match +
'\nimport android.content.Context;\nimport java.lang.ref.WeakReference;'
);
}

// Inject static field and method into class body
content = content.replace(
/public class MainActivity extends CordovaActivity\s*\{/,
match =>
match +
`\n\n private static WeakReference<Context> weakContext;\n\n` +
` public static Context getContext() {\n` +
` return weakContext != null ? weakContext.get() : null;\n` +
` }\n`
);

// Insert weakContext assignment inside onCreate
content = content.replace(
/super\.onCreate\(savedInstanceState\);/,
`super.onCreate(savedInstanceState);\n weakContext = new WeakReference<>(this);`
);

fs.writeFileSync(mainActivityPath, content, 'utf-8');
} catch (err) {
console.error('[Cordova Hook] ❌ Failed to patch MainActivity:', err.message);
}
}


/**
* Copy directory recursively
* @param {string} src Source directory
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ yarn setup
2. Build the project:

```shell
yarn build <platform (android)> <free|paid> <p|prod|d|dev>
yarn build <free|paid> <p|prod|d|dev> [fdroid]
```

**Note**: Add the fdroid flag only if you want to build the F-Droid-compatible version of Acode.
Omit this flag to build the regular APK (Play Store or normal version).

## • Contributing

Acode Editor is an open-source project, and we welcome contributions from the community. To contribute, follow these steps:
Expand Down
21 changes: 14 additions & 7 deletions utils/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#! /bin/bash

platform="$1"
app="$2"
mode="$3"
app="$1"
mode="$2"
fdroidFlag="$3"
webpackmode="development"
cordovamode=""

if [ -z "$platform" ]
then
platform="android"
root=$(npm prefix)


if [[ "$fdroidFlag" == "fdroid" ]]; then
echo "true" > "$root/fdroid.bool"
cordova plugin remove com.foxdebug.acode.rk.exec.proot

else
echo "false" > "$root/fdroid.bool"
cordova plugin add src/plugins/proot/
fi

if [ -z "$mode" ]
Expand All @@ -33,7 +40,7 @@ NC=''
script1="node ./utils/config.js $mode $app"
script2="webpack --progress --mode $webpackmode "
script3="node ./utils/loadStyles.js"
script4="cordova build $platform $cordovamode"
script4="cordova build android $cordovamode"
eval "
echo \"${RED}$script1${NC}\";
$script1;
Expand Down