- Table of Contents
- News Reader
- App Architecture
- Getting Started
- Setting up Prerequisites
- Running Quality Gates and Deployment Commands
- CI-CD - Build via Jenkins (Jenkinsfile)
- TODO
- License
News Reader is simple app to hit the NY Times Most Popular Articles API and show a list of articles, that shows details when items on the list are tapped (a typical master/detail app).
We'll be using the most viewed section of this API.
http://api.nytimes.com/svc/mostpopular/v2/mostviewed/{section}/{period}.json?api-key=sample-key To test this API, you can use all-sections for the section path component in the URL above and 7 for period (available period values are 1, 7 and 30, which represents how far back, in days, the API returns results for).
http://api.nytimes.com/svc/mostpopular/v2/mostviewed/all-sections/7.json?api-key=sample-key
Note: App icon generated using https://romannurik.github.io/AndroidAssetStudio
- Android Studio
- Java 8 - Ensure JAVA_HOME environment variable is set
An API key is necessary to successfully connect to the API that the app uses. Once an API key has been aquired, change the API_KEY
property in com.android.nytimes.service.UrlConstants
and run the app.
This sample follows a basic MVP architecture
It is an interface responsible for managing data.
- A main activity that handles navigation between the fragments.
- A fragment to display the list of articles.(ArticleListFragmentView and ArticleListFragment)
- A fragment to display a article detail( ArticleDetailFragment : Basically it has a webview, that loads the url in it)
- Presenter is the middle-man between the View and Model. It encapsulates all the Presentation logic. The presenter is responsible for querying the model and updating the view, reacting to user interactions updating the model.
This repository implements the following quality gates:
- Static code checks: running lint to check the code for any issues. Static code analysis using SonarQube
- Unit testing: running the unit tests
- Code coverage: generating code coverage reports using the JaCoCo gradle plugin
- Functional testing: running the functional tests using Appium
- Beta distribution: distributing the mobile app using Fabric
These steps can be run manually or using a Continous Integration tool such as Jenkins. More information about setting up Jenkins and SonarQube are explained in the Build via Jenkins (Jenkinsfile) section below
Checkout and run the code
git clone git@github.com:omerio/most-popular-android.git
cd most-popular-android
Category | Library/Tool | Link |
---|---|---|
Development | Android - Java | https://developer.android.com/guide/ |
Build & Dependencies Management | Gradle | https://developer.android.com/studio/build/ |
Automate Build & Release | Fastlane | https://docs.fastlane.tools/getting-started/android/setup/ |
Unit Testing | JUnit | https://developer.android.com/training/testing |
Code Coverage | JaCoCo | https://docs.gradle.org/current/userguide/jacoco_plugin.html |
Static Code Check | Gradle Lint | https://developer.android.com/studio/write/lint |
Functional Testing | Appium | http://appium.io/downloads.html |
Beta Distribution | Fabric | https://fabric.io |
Continous Integration | Jenkins | https://jenkins.io/ |
Static Code Analysis Integration | SonarQube | https://www.sonarqube.org/ |
To setup Fastlane please read the README.md file inside the ./fastlane
folder
./gradlew lint
or using Fastlane:
fastlane lint
Linting results are available at most-popular-android/app/build/reports/lint-results.html
Tests in Android are separated into 2 types:
Located at most-popular-android/app/src/test/java/
- These are tests that run on your machine’s local Java Virtual Machine (JVM). Use these tests to minimize execution time when your tests have no Android framework dependencies or when you can mock the Android framework dependencies.
Located at most-popular-android/app/src/androidTest/java/
- These are tests that run on a hardware device or emulator. These tests have access to Instrumentation APIs, give you access to information such as the Context of the app you are testing, and let you control the app under test from your test code. Use these tests when writing integration and functional UI tests to automate user interaction, or when your tests have Android dependencies that mock objects cannot satisfy.
Unit testing for Android applications is fully explained in the Android documentation. In this repository, jUnit test case has been written for Presenter
From the commandline run:
./gradlew clean test
or using Fastlane:
fastlane tests
Unit tests results are available at
most-popular-android/app/build/reports/tests/testDebugUnitTest/index.html
From Android Studio
- Right Clicking on the Class and select "Run
- To see the coverage we have t the select "Run with Coverage"
The test coverage uses the JaCoCo library
From the commandline
./gradlew clean jacocoTestReport
Test coverage results are available at
most-popular-android/app/build/reports/jacoco/jacocoTestReport/html/index.html
To setup Appium and to run the functional tests follow the README.md inside the ./appium
folder
Fabric is a service for mobile app distribution, beta testing and crash reporting. Fastlane is used to deploy the app to Fabric, the settings are summarised here. To start using Fabric signup for the service, then follow these steps:
- Go to the Settings, click on Organization and create a new Organization
- Click on the organization, copy the API Key and Build Secret
- Configure the application as detailed here. Note this is already done in this app you just need to update the files below
- Update the ./fastlane/Fastfile with these details:
# upload to Beta by Crashlytics
crashlytics(
api_token: "bfe46de5e3fasdfasdfaafd3cd0a692053bf6d",
build_secret: "b5e444e36ba2f0a844f4fasdfasdfa4e9baf5bb19f804a7b34768d8ded"
)
- Update the most-popular-android/app/src/main/AndroidManifest.xml with the Fabric API key shown on the install page:
<meta-data
android:name="io.fabric.ApiKey"
android:value="f5f26ce539a22aasdfadfba93162b6884444dd8af4cef"
/>
- From Android Studio generate a keystore for signing the release app. Click Build -> Generate Signed APK... -> Next -> Create New.
-
Save the generated key to a folder in your home directory, e.g.
~/keyvault/keystore.jks
-
Enter the same details as in the ./fastlane/Fastfile:
properties: {
"android.injected.signing.store.file" => "keystore.jks",
"android.injected.signing.store.password" => "keystore_password",
"android.injected.signing.key.alias" => "alias",
"android.injected.signing.key.password" => "alias_password",
}
- For some reason the properties passed to Gradle from fastlane don't seem to accept a path, to get around the problem create a symbolic link to the key. From the root of the project:
ln -s ~/keyvault/keystore.jks keystore.jks
ln -s ~/keyvault/keystore.jks app/keystore.jks
- To deploy the application to Fabric run
fastlane beta
To send the Android code to SonarQube you need to setup the pre-requisites below.
- Ensure Android SDK Platform 27 is installed or update the app/build.gradle with the SDK version you have installed. To instal SDK platform 27 go to Android Studio -> Preferences -> Appearance & Behavior -> System Settings -> Android SDK and check the Android 8.1 (Oreo) then click Ok.
sonarqube {
properties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.login", "1f1d98cf48bded3cfc941e633bc8d8d3815f9c89"
def libraries = project.android.sdkDirectory.getPath() + "/platforms/android-27/android.jar," +
"build/intermediates/classes/*"
- Run the following command from the project root folder
./gradlew --info sonarqube
This repo contains a Jenkinsfile https://jenkins.io/doc/book/pipeline/jenkinsfile/, which is used to define a Jekins declarative pipeline for CI-CD to build the code, run the quality gates, code coverage, static analysis and deploy to Fabric. For Windows, use the Jenkinsfile-windows
Here is an example structure of the Jenkinsfile declarative pipeline:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
Below is an illustration of the pipeline that Jenkins will execute
These steps should be followed to automated the app build using Jenkins:
- Ensure
JAVA_HOME
andANDROID_HOME
environment variables are set to the correct SDK locations - Follow the steps to setup Jenkins and SonarQube, which can be found here https://github.com/omerio/mobile-devops-infra
- Add Windows support, since Fastlane doesn't support Windows.
- Better way to manage the API keys and passwords in the build files, see here
Add SonarQube analysis
Apache License, Version 2.0