Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active May 24, 2018 05:24
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nolanlawson/f332ed32202fe3671769 to your computer and use it in GitHub Desktop.
Save nolanlawson/f332ed32202fe3671769 to your computer and use it in GitHub Desktop.
Helper script to convert an Ionic/Cordova app to a Crosswalk app

Convert a Cordova/Ionic project to a Crosswalk project

Converts an existing Cordova/Ionic project to a Crosswalk project. When the script is done running, you should have a fat 40MB APK file in platforms/android/ant-build.

This assumes you have the following already installed: cordova, ant, android, and that your ANDROID_HOME is set to point to the Android SDK.

This will work on both x86 and ARM. If you want to build two separate APKs so that they're smaller, then modify the script; the script at least shows how it works.

Step 1: download the "Cordova Android" libraries from the Crosswalk downloads page. You don't need anything except for the "Cordova Android" ones.

Step 2 Unzip them both.

Step 3 Navigate to your project directory and run:

./prepare_crosswalk \
  /path/to/crosswalk-cordova-VERSION-x86 \
  /path/to/crosswalk-cordova-VERSION-arm

Then run either:

ionic build android # for ionic

or

cordova build android # for cordova

Thanks to this article for figuring out how to get this to work.

#!/usr/bin/env bash
#
# usage: provide a path to a crosswalk-cordova-x86 and crosswalk-cordova-arm library,
# run in the current (project) directory, and it will set up crosswalk for you
#
# ./prepare_crosswalk.sh /path/to/crosswalk-cordova-VERSION-x86 /path/to/crosswalk-cordova-VERSION-arm
#
X86=$1
ARM=$2
if [[ -z $X86 ]]; then
echo 'usage: ./prepare_crosswalk /path/to/crosswalk-cordova-VERSION-x86 /path/to/crosswalk-cordova-VERSION-arm'
exit 1
fi
if [[ -z $ARM ]]; then
echo 'usage: ./prepare_crosswalk /path/to/crosswalk-cordova-VERSION-x86 /path/to/crosswalk-cordova-VERSION-arm'
exit 1
fi
cordova platform rm android
cordova platform add android@3.5.1
rm -fr platforms/android/CordovaLib/*
cp -a $X86/framework/* platforms/android/CordovaLib/
cp -a $ARM/framework/xwalk_core_library/libs/armeabi-v7a platforms/android/CordovaLib/xwalk_core_library/libs/
cp $X86/VERSION platforms/android/
cd platforms/android/CordovaLib/
android update project --subprojects --path . --target "android-19"
ant debug
cd -
# sed sucks on a mac, can't do sed -i
cat platforms/android/AndroidManifest.xml | sed 's/<\/manifest>/ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" \/>; <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" \/>;<\/manifest>/' > platforms/android/AndroidManifestNew.xml
# sed doesn't play nice with newlines either, so...
tr ';', '\n' < platforms/android/AndroidManifestNew.xml > platforms/android/AndroidManifest.xml
rm -f platforms/android/AndroidManifestNew.xml
@nraboy
Copy link

nraboy commented Nov 6, 2014

This looks REALLY good!

I do have some concerns though. Removing the Android platform using rm could create some problems. The correct way is to run cordova platform rm android. Extending upon this, I think removing the Android platform in an automated fashion in general could be a scary idea. I think a lot of people add custom code, libraries, files to their Android platform directory instead of using an Apache Cordova Hook. This could potentially wipe out a ton of their hard work.

Any thoughts?

@nolanlawson
Copy link
Author

To be honest; I don't know. These steps are basically following word-for-word the official Crosswalk documentation as well as that blog post, and they worked for me. Maybe the Crosswalk folks will eventually write a script that leverages the cordova tools themselves, but for now it looks like we kinda have to hack it.

@nolanlawson
Copy link
Author

Oh wait, it was your blog post. ;)

I'll fix it so that it's cordova platform rm; makes sense that that would be better than rm -fr.

@eskibars
Copy link

eskibars commented Dec 4, 2014

You should change line 23 to "cordova platform add android@3.5.1" instead. 9.38.208.10 is now stable and based on 3.5.1 and cordova @ 3.5 has a high security issue: http://cordova.apache.org/announcements/2014/08/04/android-351.html

@slj
Copy link

slj commented Dec 11, 2014

Great script Nolan!

For nicer formatting at the end, I would replace the last 2 lines with:

cat platforms/android/AndroidManifest.xml | sed 's/<\/manifest>/    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" \/>;    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" \/>;<\/manifest>/' > platforms/android/AndroidManifestNew.xml
# sed doesn't play nice with newlines either, so...
tr ';', '\n' < platforms/android/AndroidManifestNew.xml > platforms/android/AndroidManifest.xml
rm -rf platforms/android/AndroidManifestNew.xml

@nolanlawson
Copy link
Author

@eskibars @slj done. thanks!

@jnishiyama
Copy link

@nolanlawson Thank you for this! It is the only thing that worked for me. I would add that the relevant Cordova version is 3.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment