Skip to content

Commit

Permalink
Install Android NDK to $sdk/ndk-bundle (#80)
Browse files Browse the repository at this point in the history
* Install Android NDK to $sdk/ndk-bundle

* Use ioutil.ReadDir() for compatibility
  • Loading branch information
ofalvai authored Jul 28, 2021
1 parent e17ff54 commit 34ea253
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 21 deletions.
71 changes: 64 additions & 7 deletions e2e/bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ workflows:
#!/usr/bin/env bash
set -ex
yarn
after_run:
- _run_and_build
- path::./:
title: Execute step
inputs:
- ndk_revision: 21
- gradle-runner:
inputs:
- gradle_task: assembleDebug

test_ndk_install:
summary: Test installing multiple NDK revisions
summary: Test installing multiple NDK revisions and compare installed version numbers to expected values
envs:
- SAMPLE_APP_URL: https://github.com/bitrise-io/android-ndk-sample-project.git
- BRANCH: ndk22-compatible
Expand All @@ -57,20 +62,72 @@ workflows:
- path::./:
title: Execute step with NDK r18
inputs:
- ndk_revision: "18"
- ndk_revision: 18
- script:
title: Check installed NDK version
inputs:
- content: |-
EXPECTED_VERSION=18.0.5002713
NDK_VERSION=`cat $ANDROID_HOME/ndk-bundle/source.properties | grep Pkg.Revision | cut -d'=' -f2 | cut -d' ' -f2`
if [ $NDK_VERSION != $EXPECTED_VERSION ]; then
echo "Unexpected installed NDK version: $NDK_VERSION"
echo "Expected: $EXPECTED_VERSION"
exit 1
else
echo "NDK version is correct: $NDK_VERSION"
fi
- path::./:
title: Execute step again with NDK r18
inputs:
- ndk_revision: "18"
- ndk_revision: 18
- script:
title: Check installed NDK version
inputs:
- content: |-
EXPECTED_VERSION=18.0.5002713
NDK_VERSION=`cat $ANDROID_HOME/ndk-bundle/source.properties | grep Pkg.Revision | cut -d'=' -f2 | cut -d' ' -f2`
if [ $NDK_VERSION != $EXPECTED_VERSION ]; then
echo "Unexpected installed NDK version: $NDK_VERSION"
echo "Expected: $EXPECTED_VERSION"
exit 1
else
echo "NDK version is correct: $NDK_VERSION"
fi
- path::./:
title: Execute step with NDK r17
inputs:
- ndk_revision: "17"
- ndk_revision: 17
- script:
title: Check installed NDK version
inputs:
- content: |-
EXPECTED_VERSION=17.0.4754217
NDK_VERSION=`cat $ANDROID_HOME/ndk-bundle/source.properties | grep Pkg.Revision | cut -d'=' -f2 | cut -d' ' -f2`
if [ $NDK_VERSION != $EXPECTED_VERSION ]; then
echo "Unexpected installed NDK version: $NDK_VERSION"
echo "Expected: $EXPECTED_VERSION"
exit 1
else
echo "NDK version is correct: $NDK_VERSION"
fi
- path::./:
title: Execute step with NDK r22
description: NDK r22 doesn't contain the platforms dir anymore
inputs:
- ndk_revision: "22"
- ndk_revision: 22
- script:
title: Check installed NDK version
inputs:
- content: |-
EXPECTED_VERSION=22.0.7026061
NDK_VERSION=`cat $ANDROID_HOME/ndk-bundle/source.properties | grep Pkg.Revision | cut -d'=' -f2 | cut -d' ' -f2`
if [ $NDK_VERSION != $EXPECTED_VERSION ]; then
echo "Unexpected installed NDK version: $NDK_VERSION"
echo "Expected: $EXPECTED_VERSION"
exit 1
else
echo "NDK version is correct: $NDK_VERSION"
fi
- gradle-runner:
inputs:
- gradle_task: assembleDebug
Expand Down
54 changes: 40 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -66,15 +68,15 @@ func ndkHome() string {
return v
}
if v := os.Getenv("ANDROID_HOME"); v != "" {
return filepath.Join(v, "android-ndk-bundle")
return filepath.Join(v, "ndk-bundle")
}
if v := os.Getenv("ANDROID_SDK_ROOT"); v != "" {
return filepath.Join(v, "android-ndk-bundle")
return filepath.Join(v, "ndk-bundle")
}
if v := os.Getenv("HOME"); v != "" {
return filepath.Join(v, "android-ndk-bundle")
return filepath.Join(v, "ndk-bundle")
}
return "android-ndk-bundle"
return "ndk-bundle"
}

func inPath(path string) bool {
Expand All @@ -85,40 +87,63 @@ func updateNDK(revision string) error {
ndkURL := ndkDownloadURL(revision)
ndkHome := ndkHome()

if currentRevision := installedNDKVersion(ndkHome); currentRevision == revision {
log.Donef("NDK r%s already installed", revision)
currentRevision := installedNDKVersion(ndkHome)
if currentRevision == revision {
log.Donef("NDK r%s already installed at %s", revision, ndkHome)
return nil
}

log.Printf("NDK home: %s", ndkHome)
log.Printf("Cleaning")
if currentRevision != "" {
log.Printf("NDK version %s found at: %s", currentRevision, ndkHome)
}

log.Printf("Removing NDK...")
if err := os.RemoveAll(ndkHome); err != nil {
return err
}
log.Printf("Done")

if err := pathutil.EnsureDirExist(ndkHome); err != nil {
log.Printf("Downloading NDK r%s...", revision)
// The NDK archive contents are wrapped in an extra subdirectory, so we unzip to the parent directory,
// then rename the subdirectory to ndk-bundle
subDirPattern, err := regexp.Compile("android-ndk-.*")
if err != nil {
return err
}

log.Printf("Downloading")
unzipTarget := filepath.Dir(ndkHome)
if err := pathutil.EnsureDirExist(unzipTarget); err != nil {
return err
}
if err := command.DownloadAndUnZIP(ndkURL, unzipTarget); err != nil {
return err
}

if err := command.DownloadAndUnZIP(ndkURL, ndkHome); err != nil {
var unzippedDirName string
dirs, err := ioutil.ReadDir(unzipTarget)
for _, dir := range dirs {
if subDirPattern.MatchString(dir.Name()) {
unzippedDirName = dir.Name()
}
}
if err := os.Rename(filepath.Join(unzipTarget, unzippedDirName), ndkHome); err != nil {
return err
}

log.Printf("Done")

if !inPath(ndkHome) {
log.Printf("Append to $PATH")
log.Printf("Appended NDK folder to $PATH")
if err := tools.ExportEnvironmentWithEnvman("PATH", fmt.Sprintf("%s:%s", os.Getenv("PATH"), ndkHome)); err != nil {
return err
}
}

if os.Getenv(androidNDKHome) == "" {
log.Printf("Export %s: %s", androidNDKHome, ndkHome)
if err := tools.ExportEnvironmentWithEnvman(androidNDKHome, ndkHome); err != nil {
return err
}
log.Printf("Exported $%s: %s", androidNDKHome, ndkHome)
}

return nil
Expand Down Expand Up @@ -164,7 +189,8 @@ func main() {
}

// Initialize Android SDK
log.Printf("Initialize Android SDK")
fmt.Println()
log.Infof("Initialize Android SDK")
androidSdk, err := sdk.NewDefaultModel(sdk.Environment{
AndroidHome: config.AndroidHome,
AndroidSDKRoot: config.AndroidSDKRoot,
Expand Down

0 comments on commit 34ea253

Please sign in to comment.