Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment in AWS codebuild for Android build

I am trying install Android environment in AWS server. Here it is buildspec.yml file that contain set of command to install Android SDK, NDK and gradlew for generate android build . SDK and NDK for Android successfully downloaded and installed at AWS Ubuntu system. Even I can see a log for ./gradlew build that build successfully.

buildspec.yml

version: 0.1

phases:
  install:
    commands:
      - echo Nothing to do in the install phase...
      - sudo apt-get -y install wget
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
      - wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
      - tar zxvf android-sdk_r24.4.1-linux.tgz
      - mkdir -p android-sdk-linux/licenses
      - cp android-sdk-license ./android-sdk-linux/licenses/
      - echo sdk.dir='pwd'/android-sdk-linux > local.properties
      - wget https://dl.google.com/android/repository/android-ndk-r13b-linux-x86_64.zip
      - unzip android-ndk-r13b-linux-x86_64.zip
      - export ANDROID_NDK_HOME=`pwd`/android-ndk-r13b
      - export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools:${ANDROID_NDK_HOME}
      - echo "sdk.dir=$ANDROID_HOME" > local.properties
      - echo "ndk.dir=$ANDROID_NDK_HOME" >> local.properties
  build:
    commands:
      - root/./gradlew --debug --stacktrace build
      - root/./gradlew assemble

  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
   files:
    - Monoca/app/**/*

While I try to execute ./gradlew assemble command for Android build it can generate application file (.apk) in Android output directory in Android Studio but unfortunately it's not working in AWS CodeBuild environment.

Anyone have deep insight regarding this issue ? Please suggest if you find anything wrong in set up part.

I have followed this tutorial for AWS CodeBuild.

like image 710
QuokMoon Avatar asked Mar 01 '26 10:03

QuokMoon


1 Answers

I think the problem here is with propagating the environment variable values across commands that CodeBuild does not follow. CodeBuild executes each commands in fresh shell. The last 2 commands would essentially set sdk.dir and ndk.dir variables to be empty (you can validate this by running cat local.properties).

- echo "sdk.dir=$ANDROID_HOME" > local.properties - echo "ndk.dir=$ANDROID_NDK_HOME" >> local.properties

The right approach here would be to move your build logic to a script that gets executed from the buildspec.yml

- ./my-gradle-build.sh

or

Not use the environment variables to set variables in local.properties, but use paths instead.

- echo sdk.dir='pwd'/android-ndk-r13b > local.properties - echo ndk.dir='pwd'/android-ndk-r13b >> local.properties

I hope this helps.

Thanks!

like image 95
awsnitin Avatar answered Mar 03 '26 01:03

awsnitin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!