# Quick Setup Guide (Android & iOS)

## Android

* [Reference](https://bugfender.com/blog/how-to-add-your-first-android-job-to-jenkins/)

### Prerequisite

* Java 8 SDK

  ```sh
  $ sudo apt-get install java-8-openjdk
  ```

  

* snapd

  ```sh
  $ sudo apt install snapd
  
  # this will change Java jdk to 11java 
  $ sudo apt install android-sdk
  
  
  
      /home/AccountName/Android/Sdk
  
      /usr/lib/android-sdk
  
      /Library/Android/sdk/
  
      /Users/[USER]/Library/Android/sdk
  
  wget https://dl.google.com/android/repository/commandlinetools-linux-6609375_latest.zip
  unzip commandlinetools-linux-6609375_latest.zip -d cmdline-tools
  sudo mv cmdline-tools $ANDROID_HOME/
  export PATH=$ANDROID_HOME/cmdline-tools/tools/bin:$PATH
  
  sudo /usr/lib/android-sdk/cmdline-tools/tools/bin/sdkmanager --licenses
  
  $ sudo ./gradlew assembleRelease
  ```

  

* Android SDK

  ```sh
  sudo apt-get install unzip
  
  # here you paste the link you grabbed in the developer.android.com site
  
  sudo -iu jenkins wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
  
  sudo -iu jenkins mkdir android-sdk
  
  sudo -iu jenkins unzip sdk-tools-linux-3859397.zip -d android-sdk
  
  # this step is important to accept the Android SDK license
  
  yes |sudo -iu jenkins android-sdk/tools/bin/sdkmanager --licenses
  ```

  

* Android Emulator (Integration Test)

  ```sh
  # Download emulator
  $ sudo -iu jenkins android-sdk/tools/bin/sdkmanager --list --verbose
  
  # create Android Virtual Device instance
  $ echo no | $ANDROID_SDK_ROOT/tools/bin/avdmanager -v create avd --force --package 'system-images;android-25;google_apis;armeabi-v7a' --name Android25 --tag google_apis --abi armeabi-v7a
  
  # install Android Emulator as service (Using Supervisor)
  $ sudo apt-get install supervisor
  
  # apply configuration file
  $ sudo service supervisor restart
  
  $ sudo -iu jenkins android-sdk/platform-tools/adb devices
  ```

* `Supervisord` configuration file `/etc/supervisor/conf.d/emulator.conf` to run emulator as service

  ```ini
  [program:emulator]
  
  command=/var/lib/jenkins/android-sdk/emulator/emulator -avd Android25 -no-window -noaudio -no-boot-anim -accel on -ports 5556,5557
  
  autostart=true
  
  user=jenkins
  
  environment=ANDROID_SDK_ROOT=/var/lib/jenkins/android-sdk
  ```

* Jenkins Script for running integration test

  ```text
  ANDROID_SERIAL=emulator-5556
  
  # wait for emulator to be up and fully booted, unlock screen
  
  $ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done; input keyevent 82'
  
  ./gradlew connectedAndroidTest
  ```

### Jenkins build

* Once you have the source code in Jenkins, it’s time to build. If your project has a Gradle wrapper, that’s what you should use
  * Task : 
  
    * `assembleDebug`: APK Debug build
    * `assembleRelease` : APK release build
    * `bundleRelease`: [Bundle APK release](https://developer.android.com/guide/app-bundle) Size Limit : 150MB
      * [info](https://medium.com/androiddevelopers/building-your-first-app-bundle-bbcd228bf631)
  
  * [Signing bundle](https://medium.com/androiddevelopers/building-your-first-app-bundle-bbcd228bf631)
  
    * ```sh
      $ jarsigner -keystore $pathToKeystore app-release.aab $keyAlias
      ```
  
    * ```
      In android studio, to generate signed bundle
      Build => Generate Signed Bundle / APK
      ```
  
  * Flutter
  
    * `flutter build apk` will output to `./build\app\outputs\apk\release`
  
    * `flutter build appbundle` will generated a publishing format aab
  
      ```sh
      # build\app\outputs\bundle\release\app-release.apk
      $ flutter build apk
      
      # build\app\outputs\bundle\release\app-release.aab
      $ flutter build appbundle
      ```
  
      

### Next

* Automate app signing & publishing

* Improve build time (Accelerated Gradle Build)

  ```sh
  $ mkdir -p /var/lib/jenkins/.gradle
  
  $ echo org.gradle.daemon=true | sudo -iu jenkins tee -a /var/lib/jenkins/.gradle/gradle.properties
  ```

  

### Configure Jenkins

* Go to Manage Jenkins > Configure System
* Check “Environment variables”
* Add Name: ANDROID_HOME
* Add Value: /var/lib/jenkins/android-sdk
* Click “Apply” then “Save”



# IOS

* [Reference](https://bugfender.com/blog/how-to-add-your-first-ios-job-to-jenkins/)

### Prerequiste

* Apple Mac H/W

* Xcode

  ```sh
  $ xcode-select --install
  ```

### Sample script

* Build Script

  ```text
  cd Example # again, we need to move directory
  
  mkdir -p output # this directory will contain the output of the tests
  
  xcrun xcodebuild -workspace BugfenderExample.xcworkspace \
  
      -scheme "BugfenderExample" \
  
      -sdk iphonesimulator \
  
      -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.0' \
  
      -derivedDataPath './output' \
  
      test
  ```

  