Skip to content

Instantly share code, notes, and snippets.

@jedi4ever
Last active January 2, 2016 14:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedi4ever/77f5e5f7183eb85cb732 to your computer and use it in GitHub Desktop.
Save jedi4ever/77f5e5f7183eb85cb732 to your computer and use it in GitHub Desktop.
Notes on auto crashlytics and cordova integration

After adding crashlytics code (crashlytics.sh) & Patching the xcodeproject file (crashlytics.rb)

  • It will work in xcode but not in cordova anymore.

  • This is because xcoder writes the .xcodeproj file in a format that node-xcode doesn't understand.

  • you need to 'beautify/sanitize' the file again.

    • brew install xcproj
    • and run xcproj -P <path to your xcodeproj file> touch
  • now you are ready to go in cordova

NOTE:

  • your app will not show up in crashlytics unless you first RUN it (building is not enough)
  • sometimes I found that I need to cordova build the project twice for the run script to kick in.
#!/bin/bash
CORDOVA_PROJECT="...."
CRASHLYTICS_KEY=....
ZIPFILE="src/Crashlytics-latest.zip"
# Requires:
# - PlistBuddy
# - build ios to have run (
# - Ruby to be installed
# - Gem xcoder
# - ZIPFILE to be downloaded
TEMP_DIR=$(mktemp -d -t crashlytics)
TEMP_Framework=$(mktemp -d -t crashlytics.Framework)
echo "Extracting latest Crashlytics"
ditto -xk "$ZIPFILE" "$TEMP_DIR"
build_id=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" $TEMP_DIR/Crashlytics.app/Contents/Info.plist)
version_id=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" $TEMP_DIR/Crashlytics.app/Contents/Info.plist)
echo "Extracting Framework $version_id build $build_id"
ditto -xk $TEMP_DIR/Crashlytics.app/Contents/Resources/com.crashlytics.sdk.ios-default.zip "$TEMP_Framework"
echo "Removing previous crashlytics framework"
rm -rf platforms/ios/Crashlytics.framework
echo "Copying the Crashlytics.framework"
mv "$TEMP_Framework/Crashlytics.framework" "platforms/ios/Crashlytics.framework"
flag=$(grep "Crashlytics/Crashlytics.h" "platforms/ios/$CORDOVA_PROJECT/Classes/AppDelegate.m")
if [ -z "$flag" ]
then
echo "Adding Crashlytics header to AppDelegate.m"
sed -i '' '1i\
#import <Crashlytics/Crashlytics.h>\
' "platforms/ios/$CORDOVA_PROJECT/Classes/AppDelegate.m"
else
echo "Crashlytics Header already found in AppDelegate.m"
fi
flag=$(grep "startWithAPIKey" "platforms/ios/$CORDOVA_PROJECT/Classes/AppDelegate.m")
if [ -z "$flag" ]
then
echo "Adding Crashlytics startWithAPIKey to AppDelegate.m"
sed -i '' "
/didFinishLaunchingWithOptions/ {
n
a\\
[Crashlytics startWithAPIKey:@\"$CRASHLYTICS_KEY\"];
}" "platforms/ios/$CORDOVA_PROJECT/Classes/AppDelegate.m"
else
echo "Crashlytics startWithAPIKey already in AppDelegate.m"
fi
echo "cleaning up"
rm -rf "$TEMP_DIR"
rm -rf "$TEMP_Framework"
# This requires the xcoder gem to be installed
require 'xcoder'
projectname = "...."
crashlytics_key = "....."
crashlytics_secret = "......."
project = Xcode.project("platforms/ios/#{projectname}.xcodeproj")
frameworks = project.frameworks_group
# Cordova Project We only have 1 target = The project name
target=project.targets[0]
# Add the framework as a path
# It assumes that Crashlytics is in the project Dir"
crashlytics = frameworks.create_framework 'name' => 'Crashlytics.framework' , 'path' => 'Crashlytics.framework', 'sourceTree' => '<group>'
target.framework_build_phase do |phase|
phase.add_build_file crashlytics
end
# Adding Framework search paths to both
['Release', 'Debug'].each do |s|
config = project.target(projectname).config(s)
config.set("FRAMEWORK_SEARCH_PATHS",[ "$(inherited)","$(PROJECT_DIR)"])
end
# Add the runscript during build
target.create_build_phase :run_script do |script|
script.shell_script = "echo 'sending to Crashlytics'\n" + "./Crashlytics.framework/run #{crashlytics_key} #{crashlytics_secret}\n"
# For easy debugging
script.show_env_vars_in_log = 1
end
project.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment