Problems with adding fabric dependencies to a cordova built android project

2k views Asked by At

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()
}
2

There are 2 answers

1
Paul Weber On

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.

0
Krishna On

Based on a similar workaround in https://github.com/phonegap/phonegap-plugin-push/issues/1878, I could find a less intrusive approach.

Ran into a similar issue for google-services plugin. The error was

Plugin with id 'com.google.gms.google-services' not found.

Instead of replacing the entire generated build.gradle, added the classpath dependency to build.gradle, right next to the line that references android build tools, using hooks:

classpath 'com.android.tools.build:gradle:1.5.0'

The hook script is available in https://gist.github.com/kkleokrish/ac794fc3280bf23e81cce9b6a7f138f9