Pinwheeler
Pinwheeler

Reputation: 1111

How do I specify an environment variable while building an Android App Bundle

It looks like App Bundle is the preferred way of doing things (over APK) so I'd like to try to use it.

I need some way to specify an ENVFILE argument for building my React Native application. This is easily done when generating APKs through ENVFILE=.env.production assembleRelease but I cannot find an equivalent for App Bundles.

In addition, the Android Studio App Bundle wizard appears to be pretty scant when it comes to specifying options for the build.

Any ideas?

Upvotes: 4

Views: 12223

Answers (2)

Pinwheeler
Pinwheeler

Reputation: 1111

Turns out it's a pretty simple command with a little bit of setup.

In your app.build.gradle you need to have some config

signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
        release {
            if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
                storeFile file(MYAPP_UPLOAD_STORE_FILE)
                storePassword MYAPP_UPLOAD_STORE_PASSWORD
                keyAlias MYAPP_UPLOAD_KEY_ALIAS
                keyPassword MYAPP_UPLOAD_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://facebook.github.io/react-native/docs/signed-apk-android.
            signingConfig signingConfigs.release

        }
    }

These environment variables are set in the gradle.properties

...
MYAPP_UPLOAD_STORE_FILE=keystore-file.jks
...

Once that's all set up, it's as simple as the following

ENVFILE=.env.production gradle bundleRelease

Upvotes: 2

Rajan
Rajan

Reputation: 1568

Your problem will address by this Node package module -> react-native-config

I have used this module and pass my ENVFILE variable to string.xml as below

<resources>
    <string name="app_name">@string/APP_NAME</string>
    <string name="facebook_app_id">@string/FB_APP_ID</string>
    <string name="fb_login_protocol_scheme">@string/FB_APP_LOGIN_SCHEME</string>
    <string name="map_api_key">@string/Google_map_key</string>

</resources>

in app.gradle file

  defaultConfig {
        applicationId project.env.get("APP_ID")
        versionCode project.env.get("ANDROID_VERSION_CODE").toInteger()
        versionName project.env.get("ANDROID_VERSION_NAME")
        multiDexEnabled true
        missingDimensionStrategy "RNN.reactNativeVersion", "reactNative57"
        ndk.abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
        missingDimensionStrategy 'react-native-camera', 'general'
        vectorDrawables.useSupportLibrary = true
        renderscriptTargetApi rootProject.ext.renderscriptVersion
        renderscriptSupportModeEnabled true
        minSdkVersion 23
    }

For iOS in Objective-c

 NSString *apiUrl = [ReactNativeConfig envFor:@"Google_map_key"];

My .env file

Base_url=
InstaBase_url=https://api.instagram.com/oauth/
InstaGraph_url=https://graph.instagram.com/
Insta_Redirect_url=
Insta_AppID=
Insta_App_secret=
Google_Near_By_Restaurant=https://maps.googleapis.com/maps/api/place/nearbysearch/json?
Google_Restaurant_Photo=https://maps.googleapis.com/maps/api/place/photo?
Google_Place_auto_Base_url=https://maps.googleapis.com/maps/api/place/autocomplete/json?
Google_Place_details_Base_url=https://maps.googleapis.com/maps/api/place/details/json?
ANDROID_VERSION_CODE=6
ANDROID_VERSION_NAME=0.2
FB_APP_ID=
FB_APP_LOGIN_SCHEME=
Google_map_key=
APP_NAME=
appleAppId=
GooglePackageName=

I too have different env files as below enter image description here

I have written a script for the setting environment. Below is my debug.sh

echo $3
if [ "$2" != "android" ] && [ "$2" != "ios" ]; then
    echo "Please enter the platform either iOS or Android"
    exit
fi
if [ "$1" != "dev" ] && [ "$1" != "prod" ] && [ "$1" != "qa" ] && [ "$1" != "stage" ]; then
    echo "Please enter the enviornment one of the following"
    echo "1. prod"
    echo "2. dev"
    echo "3. stage"
    echo "4. qa"
    exit
fi

case $1 in
prod)
    clear
    if [ "$2" = "android" ]; then
        ENVFILE=.env react-native run-android --variant=prodDebug
    elif [ "$2" = "ios" ]; then
        cp Plist_JSON/Info.plist ./ios/Fomoyolo/Info.plist
        cp Plist_JSON/GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
        ENVFILE=.env react-native run-ios "$3"
    fi
    break
    ;;
dev)
    clear
    if [ "$2" = "android" ]; then
        ENVFILE=.env.dev react-native run-android --variant=devDebug --appIdSuffix 'dev'
    elif [ "$2" = "ios" ]; then
        cp Plist_JSON/Info_Dev.plist ./ios/Fomoyolo/Info.plist
        cp Plist_JSON/Dev_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
        ENVFILE=.env.dev react-native run-ios "$3"
    fi
    break
    ;;
stage)
    clear
    if [ "$2" = "android" ]; then
        ENVFILE=.env.staging react-native run-android --variant=stageDebug --appIdSuffix 'stage'
    elif [ "$2" = "ios" ]; then
        cp Plist_JSON/Info_Stage.plist ./ios/Fomoyolo/Info.plist
        cp Plist_JSON/Stage_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
        ENVFILE=.env.staging react-native run-ios "$3"
    fi
    break
    ;;
esac

For Release

echo $3
if [ "$2" != "android" ] && [ "$2" != "ios" ]; then
    echo "Please enter the platform either iOS or Android"
    exit
fi
if [ "$1" != "dev" ] && [ "$1" != "prod" ] && [ "$1" != "qa" ] && [ "$1" != "stage" ]; then
    echo "Please enter the enviornment one of the following"
    echo "1. prod"
    echo "2. dev"
    echo "3. stage"
    echo "4. qa"
    exit
fi

case $1 in
prod)
    if [ "$2" = "android" ]; then
        cp .env.prod .env
        npm run android-prod
    elif [ "$2" = "ios" ]; then
        cp Plist_JSON/Info.plist ./ios/Fomoyolo/Info.plist
        cp Plist_JSON/GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
        cp .env.prod .env
        rm -r ios/assets/node_modules/react-native/package.json
        npm run ios-release
    fi
    break
    ;;
dev)
    if [ "$2" = "android" ]; then
        cp .env.dev .env
        npm run android-dev
    elif [ "$2" = "ios" ]; then
        cp Plist_JSON/Info_Dev.plist ./ios/Fomoyolo/Info.plist
        cp Plist_JSON/Dev_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
        cp .env.dev .env
        rm -r ios/assets/node_modules/react-native/package.json
        npm run ios-release
    fi
    break
    ;;
stage)

    if [ "$2" = "android" ]; then
        cp .env.staging .env
        npm run android-stage
    elif [ "$2" = "ios" ]; then
        cp Plist_JSON/Info_Stage.plist ./ios/Fomoyolo/Info.plist
        cp Plist_JSON/Stage_GoogleService-Info.plist ./ios/Fomoyolo/GoogleService-Info.plist
        cp .env.staging .env
        rm -r ios/assets/node_modules/react-native/package.json
        npm run ios-release
    fi
    break
    ;;
esac
{
  "name": "Fomoyolo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint .",
    "fullSetup": "npm install && react-native link && cd ios/ && pod install && cd ..",
    "postinstall": "node ./android-release-gradle-fix.js",
    "android-dev": "ENVFILE=.env.dev && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ &&  cd ./android && ./gradlew app:assembleDevRelease",
    "android-stage": "ENVFILE=.env.staging && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ &&  cd ./android && ./gradlew app:assembleStageRelease",
    "android-prod": "ENVFILE=.env && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ &&  cd ./android && ./gradlew app:assembleRelease"
  },
}

Upvotes: 4

Related Questions