Ionic CircleCI Android - Failed to find 'ANDROID_HOME' environment variable


#1

I want to integrate my ionic app with CircleCI for continuous integration.
I can build the app on my local machine and it creates the android apk file.
I want to replicate this in CircleCI. I’m able to install the dependencies and I also set the ANDROID_HOME and PATH as environment variables pointing to opt/android/sdk that it is the folder I have found out Android is installed.
ANDROID_HOME AND PATH are set correctly. I use android 25 (but I also tried with 24 and 28), but I always get the same error. I build correctly and then I get this:

[09:53:04] build dev finished in 11.45 s

cordova build android

You have been opted out of telemetry. To change this, run: cordova telemetry on.
(node:1807) UnhandledPromiseRejectionWarning: CordovaError: Failed to find ‘ANDROID_HOME’ environment variable. Try setting setting it manually.
Failed to find ‘android’ command in your ‘PATH’. Try update your ‘PATH’ to include path to valid SDK directory.
at /home/circleci/project/platforms/android/cordova/lib/check_reqs.js:222:19
at _fulfilled (/home/circleci/project/platforms/android/cordova/node_modules/q/q.js:834:54)
at self.promiseDispatch.done (/home/circleci/project/platforms/android/cordova/node_modules/q/q.js:863:30)
at Promise.promise.promiseDispatch (/home/circleci/project/platforms/android/cordova/node_modules/q/q.js:796:13)
at /home/circleci/project/platforms/android/cordova/node_modules/q/q.js:857:14
at runSingle (/home/circleci/project/platforms/android/cordova/node_modules/q/q.js:137:13)
at flush (/home/circleci/project/platforms/android/cordova/node_modules/q/q.js:125:13)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:1807) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1807) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This is my config.yml

version: 2
jobs:
build:
docker:
- image: circleci/android:api-25-node8-alpha

environment:
  ANDROID_HOME: /opt/android/sdk/

steps:
  - checkout

  - restore_cache:
      name: Restore Yarn Package Cache
      keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
  - run:
      name: install yarn
      command: sudo yarn install
  - save_cache:
      name: Save Yarn Package Cache
      key: yarn-packages-{{ checksum "yarn.lock" }}
      paths:
        - /project/  #~/.cache/yarn
  - run: 
      name: Install ionic cordova
      command: sudo npm install -g ionic cordova
  
  - run:
      name: print ANDROID_HOME
      command: |
        echo ANDROID_HOME: ${ANDROID_HOME}
  - run:
      name: print PATH
      command: |
        echo PATH: ${PATH}

  - run:
      name: export
      command: export PATH=${PATH}:$ANDROID_HOME/tools:${PATH}:$ANDROID_HOME:$ANDROID_HOME/platform-tools
 
  - run: 
    name: run
    command: ionic run android

  - run: 
      name: save android app into artifacts
      command: cp -r platforms/android/build/outputs/apk $CIRCLE_ARTIFACTS