Android GeckoView compiling How-To
Ok you want to use a WebView in your Android App… I express my concerns about webviews-abuse before in particular cause respect to other Android libraries you don’t have absolutely any control on that and when shit happens the only thing you can do is report the issue and wait that the Google God release and update with a fix.
What if you can integrate another webview engine, something that at least you have more control on and that could be more powerful than the Android one, well good news a solution exists and is called GeckoView!!
I will write more about this project but for now I want to start in a bit different way, indeed I want to discuss on how to build it from source cause this ultimately this means have control right?
The documentation reported in the former website is pretty good, following is just my personal steps 🙂
As a side node all the steps reported here refers to a Mac Os environment but in essence is exactly the same on every os
Get set up with Mozilla Central
First thing is setup the repo, Mozilla doesn’t use Git as source version system but Mercurlial, so let’s start installing Mercurial
brew install mercurial
Check that Mercurial has been successfully installed
hg --version
Install git cinnabar for users that know git and not mercurial
brew install git-cinnabar
initialize an empty git repository
git init gecko && cd gecko
now let’s configure Git
git config fetch.prune true
git config push.default upstream
Add remotes for your repositories. There are several to choose from, central, inbound, beta, release etc. but in reality, if you plan on using Phabricator, which is Firefox’s preferred patch submission system, you only need to set up central. It might be advisable to have access to inbound however, if you want to work on a version of Firefox that is queued for release. This guide will be focussed on Phabricator.
git remote add central hg::https://hg.mozilla.org/mozilla-central -t branches/default/tip
git remote add inbound hg::https://hg.mozilla.org/integration/mozilla-inbound -t branches/default/tip
git remote set-url --push central hg::ssh://hg.mozilla.org/mozilla-central
git remote set-url --push inbound hg::ssh://hg.mozilla.org/integration/mozilla-inbound
Expose the branch tip to get quick access with some easy names.
git config remote.central.fetch +refs/heads/branches/default/tip:refs/remotes/central/default
Setup a remote for the try server.
git remote add try hg::https://hg.mozilla.org/try
git config remote.try.skipDefaultUpdate true
git remote set-url --push try hg::ssh://hg.mozilla.org/try
git config remote.try.push +HEAD:refs/heads/branches/default/tip
Now update all the remotes
git remote update
Bootstrap Gecko
Ensure you have mozilla-central checked out. If this is the first time you are doing this, it may take some time.
git checkout central/default
If you are on a mac, you will need to have the Xcode build tools installed. You can do this by either installing Xcode or installing only the tools from the command line by running xcode-select –install and following the on screen instructions.
I had to install the entire Xcode suite and after that also defined the Xcode path
sudo xcode-select --switch /Applications/Xcode.app
Use the –no-interactive
argument to automatically accept any license agreements.
In my case the option -no-interactive was not working properly so I did
./mach bootstrap
I replied yes to everything except to build the system telemetry
At the end I created a mozconfig file and paste the following content into it
#Build GeckoView/Firefox for Android:
ac_add_options --enable-application=mobile/android
#Targeting the following architecture.
#For regular phones, no --target is needed.
#For x86 emulators (and x86 devices, which are uncommon):
#ac_add_options --target=i686
#For newer phones.
#ac_add_options --target=aarch64
#For x86_64 emulators (and x86_64 devices, which are even less common):
ac_add_options --target=x86_64
As you can see from the mozconfig file I enabled the aarch64
Now it’s time to configure your build.
./mach configure
I got an error:
The target may be unsupported, or you may not have
0:08.33 a rust std library for that target installed. Try:
0:08.33 rustup target add aarch64-linux-android
First of all need to install rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
And after that to configure the current shell to use rustup
source $HOME/.cargo/env
Finally I can do
rustup target add aarch64-linux-android
and then
./mach build
And now let’s go for a run … 😉
Finally after that, if everything went well, we can run
./mach package
Congrats you’ve successfully built GeckoView!!
And for Android Studio?
Pretty easy, just open AS and disable the Instant Run (from the preferences), This is because Fennec and the Geckoview Example app cannot deploy with Instant Run on.
Uncheck the box that reads Enable Instant Run to hot swap code/resource changes on deploy.
Now select Open and pick the root directory that contains the gecko files (the initial folder named gecko)
Wait for the project to index and gradle to sync. Once synced, the workspace will reconfigure to display the different projects.
- annotations contains custom annotations used inside GeckoView and Fennec.
- app is Fennec – Firefox for Android. Here is where you will find code specific to that app.
- geckoview is the GeckoView project. Here is all the Java files related to GeckoView
- geckoview_example is an example browser built using GeckoView.
- omnijar contains the parts of Gecko and GeckoView that are not written in Java or Kotlin
- thirdparty contains third party code that Fennec and GeckoView use.
And now you’re ready to get your hands dirty!!