appcenter-sdk-react-native
appcenter-sdk-react-native copied to clipboard
Setting AppSecret to be different with build variants
I have a situation where I am having separate apps on AppCenter but all of the apps use same repo. So these different apps are just white label versions of the same app.
Now I am facing problem where I would like to change AppSecret per ios build config. But since AppCenter-Config.plist
is custom plist file I cant use user defined variables with them. (https://stackoverflow.com/questions/34067120/use-user-defined-build-settings-in-custom-plist-file)
If however this SDK would read the app secret from Info.plist I would be able to use user defined variables. Is there any reason why app secret has to be in own plist file? Is there drawback if it would be in Info.plist?
@henrikra for a current work around you could add a pre-build script that replaces the AppCenter-Config.plist depending on the build. It will copy you old config for backup, then replace the current config with the one for your white label. I haven't tried this with AppCenter but used similar setups for white label apps or different stages (dev/prod).
Go to your project > select target > select build phases > New Run Script Phase (plus button) > Move it to before compiles (pre-build)
# Backing up original plist
cp AppCenter-Config.plist AppCenter-Config.plist.bak
if [ ${TARGET_NAME} = "WhiteLabel A" ]; then
echo "Setting AppCenter for WhiteLabel A"
cp AppCenter-Config-WhiteLabelA AppCenter-Config.plist
elif [ ${TARGET_NAME} = "WhiteLabel B" ]; then
echo "Setting AppCenter for WhiteLabel B"
cp AppCenter-Config-WhiteLabelB AppCenter-Config.plist
else
echo "Not specified"
fi
Even better you could save this as config_appcenter.sh and just call it in the prebuild for each target.
@hammadzz Yeah we did something similar. Only down side is that you will get unwanted changes to git because you are basically modifying file that is tracked by git
@henrikra you can make sure to undo the changes in the script by doing a post compile script that cleans up.
Thanks for the above, works great for iOS. Also trying to set it up on Android, but it looks like here that Android looks for the first appcenter-config.json
file it finds or always looks in the main
folder, but I have a debug
folder and a beta
folder for the other variants of our Android app. Any ideas how to either update the appcenter-config.json
on build like above, or to tell AppCenter where to find those specific variant files?
Hi @CodyBrouwers,
App Center doesn't support specifying different build variants (we're tracking this as as feature request).
Maybe you can add a script that copies the right appcenter-config.json
into the main folder based on what variant you are building?
You should be able to do this as a gradle task (check out the gradle docs for file operations), like this:
task copyAppCenterConfig(type: Copy) {
// maybe also delete the appcenter-config.json in main first?
from file("${buildDir}/path-to-your-beta-folder/appcenter-config.json")
into file("${buildDir}/path-to-main-folder/appcenter-config.json")
}
and run this before build?
@TroubleMakerBen that is a great idea, thanks so much for the quick response! I ended up doing this earlier today:
buildTypes {
debug {
if (getCurrentBuildType() == "debug") {
println "=========== Configuring appcenter-config.json for Development ==========="
def appCenterSecret = "..."
new File("$projectDir/src/main/assets/appcenter-config.json").text = "{ \"app_secret\": \"${appCenterSecret}\" }"
}
}
... // Same in the rest of my build types
}
Using the getCurrentBuildType
method from this gist.
Using a task like you suggested might be a better approach though so thanks a lot for the recommendation!
this is our example:
if [ "$APPCENTER_BRANCH" == "master" ]
then
echo "Building with prod config"
cp "environments/config.prod.js" "src/services/config/index.js"
cp "environments/AppCenter-Config.prod.plist" "ios/NordstromON/AppCenter-Config.plist"
cp "environments/appcenter-config.prod.json" "android/app/src/main/assets/appcenter-config.json"
else
echo "Building with nonprod config"
cp "environments/config.nonprod.js" "src/services/config/index.js"
cp "environments/AppCenter-Config.nonprod.plist" "ios/NordstromON/AppCenter-Config.plist"
cp "environments/appcenter-config.nonprod.json" "android/app/src/main/assets/appcenter-config.json"
fi
@CodyBrouwers and @ElektrojungeMS I was able to get around this issue without any code in the gradle file that might be helpful
we have several product flavors (client1
, client2
) and build types (qa
, release
), so I was able to create a directory for each build variant that needed a different AppCenter config like this...
/android/app/src/client1Qa/assets/appcenter-config.json
/android/app/src/client1Release/assets/appcenter-config.json
/android/app/src/client2Qa/assets/appcenter-config.json
/android/app/src/client2Release/assets/appcenter-config.json
this allows you to have a default /android/app/src/main/assets/appcenter-config.json
for any other build variant that doesn't need a specific api key.
@tkiefhaber getting this error with your suggested solution - `Found more than one appcenter-config.json. How did you fixed this?
You can have a appcenter-pre-build.sh
in the root folder
#!/usr/bin/env bash
echo $CODE_PUSH_ANDROID_SECRET | base64 --decode > "$APPCENTER_SOURCE_DIRECTORY/android/app/src/main/assets/appcenter-config.json"
echo $CODE_PUSH_IOS_SECRET | base64 --decode > "$APPCENTER_SOURCE_DIRECTORY/ios/Shyn/AppCenter-Config.plist"
And for each environment, you can use a different file and base64 -w 0 appcenter-config.json
and add as the env var.
@ElektrojungeAtWork hi...just curious: did you guys implement the feature by now? the documentation doesnt mention anything.
Hi @sammysium , could you please confirm that you're asking about migrating app secret from AppCenter-Config.plist
to the default Info.plist
file?
It is not yet implemented if such, but thanks for bumping this thread, we will discuss it within the team.
@DmitriyKirakosyan that would be nice, I am also not getting why you need a separate Plist file for storing just one key-value.
on our side, for ios projects, we handled such problem with using a pre-built script, as @hammadzz mentioned, however, our approach for this sh is a little bit different. below you can find our approach.
first, we have 3 build schemes for both android and ios named as 'Release', 'DevTest', 'ProdTest'
on iOS project folder, we have created AppCenter folder along with Release, DevTest and ProdTest folders. place each appcenter-info.plist file to designated folders.
add APPCENTER_CONFIG user-defined string in your xcode -> build settings for each your schemes. we have 4 schemes, 3 build settings. debug, devtest, prodtest and release. set these fields in the way you want to configure your build schemes.
then add a build script from xcode build phases, with the following code piece:
#!/bin/sh
APPCENTER_INFO=${PROJECT_DIR}/AppCenter/${APPCENTER_CONFIG}/AppCenter-Config.plist
# Make sure of AppCenter-Config.plist exists
echo "Looking for ${APPCENTER_INFO}"
if [ ! -f $APPCENTER_INFO ]
then
echo "No AppCenter-Config.plist found. Please ensure it's in the proper directory."
exit 1
fi
# Get a reference to the destination location for the AppCenter-Config.plist
PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/AppCenter-Config.plist
echo "Will copy ${APPCENTER_INFO} to final destination: ${PLIST_DESTINATION}"
# Copy over the prod AppCenter-Config.plist for Release builds
echo "Using ${APPCENTER_INFO}"
cp "${APPCENTER_INFO}" "${PLIST_DESTINATION}"
@DmitriyKirakosyan that would be nice, I am also not getting why you need a separate Plist file for storing just one key-value.
I also wonder the same thing, why the extra file for ios and android. If can be done in plist file and string.xml, i think it'd look a lot cleaner with more flexibility. Or give the ability to define on the fly like codepush would be nice too.