在 Android 模块中使用 CrashAnalytics

Using CrashAnalytics within an Android Module

我的应用程序中有 Crashlytics。

我在名为 'engine-module' 的应用程序中添加了一个 Android 模块,它有自己的 Gradle 依赖项。

我不知道如何正确导入 Crashlytics 依赖项,我只收到以下错误:

Error:(31, 13) Failed to resolve: com.crashlytics.sdk.android:crashlytics:2.6.5

我想要实现的是能够从 'engine-module' 中的 Class 访问 'Crashlytics' 例如:

Crashlytics.setUserName("temp user name");

这可能吗?如果可以,如何实现?

项目:build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

subprojects {
    apply from: '../jacoco.gradle'
}

App/build.gradle

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

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

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

apply plugin: 'com.neenbedankt.android-apt'

/**
 * Default values for configuration options
 */
def suffixDefault = ""
def versionCodeDefault = 1
def versionNameDefault = "developerBuilt"

/**
 * Android-specific configuration
 */
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.flowmellow.projectx
        minSdkVersion 16
        targetSdkVersion 25
        versionCode project.getProperties().get('versionCode') ? project.getProperties().get('versionCode').toInteger() : versionCodeDefault
        versionName project.getProperties().get('versionName') ?  project.getProperties().get('versionName') : versionNameDefault
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            // Configurable options
            applicationIdSuffix project.getProperties().get('appIdSuffix', suffixDefault) + ".debug"

            // Common options
            testCoverageEnabled true
        }
        release {
            // Configurable options
            applicationIdSuffix project.getProperties().get('appIdSuffix', suffixDefault)

            // Common options
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

/**
 * Dependencies
 */
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'

    //Dependency injection
    apt 'com.google.dagger:dagger-compiler:2.7'
    compile 'com.google.dagger:dagger:2.7'
    provided 'javax.annotation:jsr250-api:1.0'

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.1'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'org.robolectric:robolectric:3.1.2'
    testCompile 'org.hamcrest:hamcrest-library:1.1'
    compile 'com.android.support.constraint:constraint-layout:+'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
        transitive = true;
    }
    compile project(path: ':engine-module')

}

模块:engine-module/build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:support-annotations:25.0.0'

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'org.robolectric:robolectric:3.1.2'
    compile 'com.crashlytics.sdk.android:crashlytics:2.6.5'
}

已解决:

项目:build.gradle

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

subprojects {
    apply from: '../jacoco.gradle'
}

模块:engine-module/build.gradle

apply plugin: 'com.android.library'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:support-annotations:25.0.0'

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'org.robolectric:robolectric:3.1.2'

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

您没有为 engine-module 模块定义任何存储库,这意味着它无法解析关联的依赖项。

通常人们通过将以下代码添加到根目录来为他们的整个项目定义存储库 build.gradle

subprojects {
    repositories {
        // Add repositories here
    }
}

这使得这些存储库可用于所有子项目。