Wikimedia Apps/Team/Android/App hacking

We welcome volunteer contributions!
The Wikipedia Android app is completely open source and welcomes contributions from all. Follow the steps below to get started, and feel free to ping any member of the WMF Android dev team (bearND, dbrant, mdholloway, niedzielski) on for assistance.

Open bugs and feature requests
Work items are tracked on our kaban board. Fist time app contributors are encouraged to consider an Easy task. Unassigned tasks in the "Bug Backlog", "Tech Debt Backlog", and "Product Backlog" columns are generally suitable for development and roughly organized by descending priority. (Please do not work on tasks in other columns like "Open Questions" and "Needs Triage".)

Create a developer account
See https://www.mediawiki.org/wiki/Developer_access.

Add your SSH key to Gerrit
See https://www.mediawiki.org/wiki/Gerrit/Tutorial#Set_Up_SSH_Keys_in_Gerrit.

Install JDK 8
JDK 8 is needed to build the Android app. To find your current version:

$ javac -version javac 1.8.0_102

The open source OpenJDK may be installed on Debian / Ubuntu by executing. The closed source Oracle JDK may be installed from here.

Download and install Android Studio
https://developer.android.com/sdk/index.html

Run the setup wizard. Standard installation is fine. Installation will take a while.

Download the project source
git clone ssh://@gerrit.wikimedia.org:29418/apps/android/wikipedia

Note that this requires developer access.

If you prefer, you may anonymously download a clone git clone https://git.wikimedia.org/git/apps/android/wikipedia.git

Import into Android Studio
Open Android Studio > Import project > select build.gradle from the downloaded repository.

Checkstyle-IDEA
IntelliJ's Checkstyle-IDEA plugin is useful in ensuring that your code will meet the project style guidelines.

Recent versions of this plugin require Android Studio to run on Java 8; if Java 8 is unavailable on your system, a version compatible with Java 7 can be downloaded here and installed from disk: Android Studio > Configure > Plugins > Install plugin from disk.

Restart to complete the installation, then add the checkstyle configuration file:

Android Studio > Configure > Preferences > Other Settings > click on the '+' in the upper pane and select /config/checkstyle.xml.

Make sure you activate the new configuration file by checking the checkbox next to it, so our checkstyle rules are enabled and deviations show up as errors in the code editor.

Install and configure git-review
Install git-review:

pip install git-review

(If you need to install pip, you can find instructions here.)

Set the default remote to origin:

git config --global gitreview.remote origin

Make changes and submit for review via Gerrit
Make changes and squash into a single commit, then:

git review -R

See here for details.

Setup
cd scripts pip install -r requirements.txt Note: the lxml Python module seems to have system package dependencies on Debian and Ubuntu. If you're missing headers, try.

Troubleshooting Script Setup
On OS X a few of us ran into the issue that `libxml` couldn't get compiled. Running the following command helped: xcode-select --install If this still doesn't work, you might also want to try: pip install sh jinja2 unicodecsv

Update bundled CSS files
The various CSS files for this project are generated by the *.less files found in the MobileFrontend and MobileApp MediaWiki extensions.

You'll need a MediaWiki installation, MediaWiki-Vagrant recommended, to generate them.

In the ./vagrant directory:

vagrant enable-role mobileapp mobilefrontend vagrant provision

Change the url in scripts/make-css-assets.bash to http://127.0.0.1:8080/w instead of the bits url, then run the script and test.

Note: your system may be unable to run the Android emulator and a Vagrant / VirtualBox instance simultaneously.

Update bundled JavaScript
Portions of JavaScript code run inside the WebView component that displays articles.

This code is prepackaged using Grunt, which must be re-run every time the master .js files are edited before building.

Preparing:

First, install the Grunt CLI tool:

npm install -g grunt-cli

Install dependencies for packaging:

cd www npm install

Building: cd www grunt

This will produce output files under  which will be included in the apk file.

Update icons from SVG
Many of our icons are maintained as SVG originals, rasterized to PNG at the various output resolutions via a script. This rasterization is not part of the main build process, so needs to be re-run when adding new icons.

Setup
Ensure you have librsvg and the 'rsvg-convert' command:


 * On Ubuntu:
 * On Mac OS X using Homebrew:

You also need to have ImageMagick (for flipping of icons):


 * On Ubuntu:
 * On Mac OS X using Homebrew:

Lastly, you'll need the pngcrush utility, which optimizes PNG files:


 * On Ubuntu:
 * On Mac OS X using Homebrew:

Run
python scripts/convert-icons.py

Original files from icon-svgs/*/*.svg are rendered and copied into the res/ subdirectories. Note that they are not automatically added to git!

Run
cd scripts python make-templates.py mv *.java ../app/src/main/java/org/wikipedia/staticdata/

Automated Tests
The codebase supports two kinds of tests:
 * 1) JVM JUnit (preferred, off device, fast, less flaky)
 * 2) Android instrumentation (on device)

Command line usage

 * JVM JUnit
 * Android instrumentation
 * Both JVM JUnit and Android instrumentation
 * Both JVM JUnit and Android instrumentation
 * Both JVM JUnit and Android instrumentation

Tips

 * When running, add the   flag to run the Android instrumentation tests even if the JVM JUnit tests failed.
 * To run a task across all configurations, exclude the build variant. e.g.,  instead of.

Android Studio setup
Both kinds of tests are supported in Android Studio. You may toggle between them by changing the Test Artifact from View -> Tool Windows -> Build Variants. "Unit Tests" are JVM JUnit tests and "Android Instrumentation Tests" are Android instrumentation tests. A run configuration must be made to prior to executing the tests.

JVM JUnit

 * 1) Click Run -> Edit Configurations...
 * 2) Click Add New Configuration (a large plus icon) and select JUnit.
 * 3) Name the configuration.
 * 4) Change the Test kind to All in package.
 * 5) Set the package to org.wikipedia.
 * 6) Ensure the working directory is set to $MODULE_DIR$.
 * 7) Set Use classpath of module to  . (TODO: update screenshot)



Conventions

 * Java code must run cleanly against Checkstyle
 * JavaScript must run cleanly against JSHint
 * We're working to be lint free! Currently Android Linting has many offenders but new contributions should help us progress towards zero.
 * Python changes should run cleanly against flake8.
 * All Android and JVM unit tests should pass.
 * Git commit messages should conform to the MediaWiki commit message guidelines
 * Documentation should conform to the Wikipedia manual of style
 * When making changes to any strings.xml, corresponding explanatory text should be added to values-qq/strings.xml
 * Third-party libraries must meet acceptance criteria

WebView debugging in Chrome
Wikipedia for Android makes extensive use of WebViews. To debug WebView activity, navigate Google Chrome to chrome://inspect/#devices, then click on the topmost “inspect” link under “WebView in org.wikipedia.” From there you can debug the WebView like any other web site in Chrome.

Useful Gradle commands
If you prefer command line use the wrapper script in the root of the repo: ./gradlew

To run a clean debug build: ./gradlew -q clean assembleDevDebug

You can skip the clean part usually, which makes it much faster (from 1m:05s to 7s on my box): ./gradlew -q assembleDevDebug

To install build on device/emulator: ./gradlew -q installDevDebug

To see ProGuard output: ./gradlew clean --info proguardDevRelease

To run checkstyle: ./gradlew checkstyle

To run Lint: ./gradlew lintDevDebug

To refresh dependencies (usually not needed): ./gradlew --refresh-dependencies

To list dependencies: ./gradlew app:dependencies --configuration compile

Making apt work in Ubuntu 64-bit
If you're using 64-bit versions of Ubuntu, you might see this error when building with gradle: "Cannot run program '~/.android-sdk/build-tools/22.0.1/aapt': error=2, No such file or directory". To fix this error, install the following packages:

sudo apt-get install lib32stdc++6 lib32z1

Help make it better!
Testing:
 * Alpha release
 * Beta release
 * Production release

See pending/recent code reviews:
 * Wikipedia Android app
 * Java MediaWiki-API
 * MediaWiki MobileApp extension

Publishing to Sonatype OSSRH
This is for our libraries; currently only java-mwapi but there could be more in the future.

Create GPG key
See hints at http://central.sonatype.org/pages/working-with-pgp-signatures.html#generating-a-key-pair.

Usually accept the default. But I would use the max value  for the requested keysize. Don't forget to add a passphrase.

Towards the end of the output you'll see the public key in a line similar to this: pub   4096R/F13983AB 2015-07-16

The highlighted portion would be your public key you want to enter in the signing config below as the.

Create a new Sonatype JIRA account
Sonatype account registration. Use these values for nexus* in the Signing configuration step below. This will be used for the  Gradle task. Hint: It's good to avoid special characters in the username and password that could be misinterpreted by Gradle or the shell.

Signing configuration
A GPG signature is necessary for signing but is independent of uploading. Add the following to your : signing.keyId= signing.password= signing.secretKeyRingFile= nexusUsername= nexusPassword=

Snapshots
If this was done for the java-mwapi library you should see a new SNAPSHOT version on OSSRH.
 * Update the version in lib/build.gradle.
 * Execute the following commands

Promotion

 * 1) Merge snapshot code.
 * 2) Promote artifacts from the web interface. After uploading a non-snapshot version, click staging repositories and search for "mediawiki". Select the repository and and at the top click "close". After refreshing, click release.
 * 3) Push and +2 the two version bump patches.
 * 4) Push the version tag.
 * 1) Push and +2 the two version bump patches.
 * 2) Push the version tag.