Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with adding fabric dependencies to a cordova built android project

Hello I am currently migrating the android part of my cordova/phonegap app from ant to gradle, and I am trying to add the fabric SDK.

In cordova projects the build.gradle seems to be autogenerated, and all changes should be added to the build-extras.gradle. How can I add additional dependencies in the buildscript block?

This is how the stuff for fabric should look in build.gradle, and it works if I put it here, but it is overwritten.

buildscript {
    repositories {
        mavenCentral()
        // this line needs to be added
        maven { url 'https://maven.fabric.io/public' }
    }

    // this block needs to be added
    dependencies {            
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

If I add this block in the build-extras.gradle, Groovy complains with

Failed to apply plugin [id 'io.fabric'] Plugin with id 'io.fabric' not found.

but if I add it into the build.gradle it might get overridden and will not be added to my colleagues builds (there is a warning at the top of the file that it should not be edited).

My build-extras.gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

android {
    dependencies {
        compile('com.facebook.android:facebook-android-sdk:3.21.1') {
            exclude module: 'support-v4'
        }
        compile 'com.google.android.gms:play-services:7.5.0'
    }

//    dexOptions {
//        preDexLibraries = false
//    }
}

apply plugin: 'io.fabric'

repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    // Crashlytics Kit
    compile('com.crashlytics.sdk.android:crashlytics:2.3.2@aar') {
        transitive = true
    }
}

I am pretty new to Gradle ... in the build.gradle there is this line ... maybe it can help me?

// Allow plugins to declare Maven dependencies via build-extras.gradle.
repositories {
    mavenCentral()
}
like image 435
Paul Weber Avatar asked Jun 15 '15 16:06

Paul Weber


1 Answers

To solve the problem I am currently using a before_build cordova hook that overrides the build.gradle and my MainActivity.java with my own custom version stored in a non platform folder in my cordova project repository.

The modified MainActivity.java just has this extra line added after loadUrl(launchUrl);

Fabric.with(this, new Crashlytics());

Here is the Cordova hook.

/hooks/before_build/addGradleExtras.sh

#!/bin/sh

if [ -d "platforms/android" ]; then

echo "copying extra gradle configuration to android directory"
cp resources/build-extras.gradle platforms/android/build-extras.gradle

echo "OVERWRITING GENERATED build.gradle IN PROJECT, AS LONG AS WE DO    NOT FIND A BETTER WAY"
cp resources/build.gradle platforms/android/build.gradle

echo "Adding fabric.properties file"
cp resources/fabric.properties platforms/android/fabric.properties

echo "Overwriting mainactivity with our changes"
cp resources/MainActivity.java platforms/android/src/com/updatemi/app2/MainActivity.java

In the build.gradle you just need to add this after the buildscript {

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
}

And in build-extras.gradle, in the last dependencies block

    compile('com.crashlytics.sdk.android:crashlytics:2.3.2@aar') {
        transitive = true
    }

This is not a final solution, and only a very hacky way to make it work. It might break on cordova updates.

I will answer again if I find a better solution. This bash scripts will work if you have a Linux and maybe if you have a Mac system.

like image 109
Paul Weber Avatar answered Sep 19 '22 02:09

Paul Weber