在Android Studio中的产品风格中使用Crashlytics

时间:2014-02-19 23:10:14

标签: gradle android-studio android-gradle crashlytics

我正在使用Android Studio开发我的应用程序并通过创建一些产品风格来利用gradle。产品口味是谷歌,测试版和实验室。

我只想将Crashlytics与一种产品风格( beta )集成在一起,但我遇到了一个我无法解决的问题。

使用当前的build.gradle文件,我可以获得构建变体betaDebug和betaRelease来编译,但是,我无法让其他人编译。

此外,在成功启动betaDebug构建变​​体后,我无法让Crashlytics识别我的应用程序。

切换到其他构建变体时出现的错误:

Gradle Running

Failed to complete Gradle exection.

Cause:
Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.10-all.zip'.

这是我的build.gradle文件:

buildscript {
    repositories {
        maven { url 'http://download.crashlytics.com/maven' }
    }

    dependencies {
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
    }
}
apply plugin: 'android'
apply plugin: 'crashlytics'
repositories {
    maven { url 'http://download.crashlytics.com/maven' }
}


android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        packageName "com.mcarrano.example"
    }

    buildTypes {
        debug {
            packageNameSuffix '.debug'
            versionNameSuffix '-DEBUG'
        }

        release {
            runProguard true
            debuggable false
            proguardFile 'proguard-rules.txt'
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
        }
    }

    productFlavors {
        google {
            packageName = android.defaultConfig.packageName
            versionName = android.defaultConfig.versionName
        }
        beta {
            packageName = android.defaultConfig.packageName + ".beta"
            versionName = android.defaultConfig.versionName + "-BETA"
        }
        lab {
            packageName = android.defaultConfig.packageName + ".lab"
            versionName = android.defaultConfig.versionName + "-LAB"
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
        google {
            java.srcDirs = ['src/google/java']
        }
        beta {
            manifest.srcFile 'src/beta/AndroidManifest.xml'
            java.srcDirs = ['src/beta/java']
        }
        lab {
            java.srcDirs = ['src/lab/java']
        }
    }

}

dependencies {
    compile 'com.android.support:support-v4:19.0.+'
    compile 'com.android.support:appcompat-v7:19.0.+'
    betaCompile 'com.crashlytics.android:crashlytics:1.+'
}

当应用程序无法编译时,这是Gradle控制台:

Executing tasks: [:example:generateGoogleDebugSources]

Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:example:preBuild
:example:preGoogleDebugBuild
:example:checkGoogleDebugManifest
:example:preBetaDebugBuild
:example:preBetaReleaseBuild
:example:preGoogleReleaseBuild
:example:preLabDebugBuild
:example:preLabReleaseBuild
:example:prepareComAndroidSupportAppcompatV71901Library UP-TO-DATE
:example:prepareGoogleDebugDependencies
:example:compileGoogleDebugAidl UP-TO-DATE
:example:compileGoogleDebugRenderscript UP-TO-DATE
:example:generateGoogleDebugBuildConfig UP-TO-DATE
:example:processGoogleDebugManifest UP-TO-DATE
:example:crashlyticsCleanupResourcesGoogleDebug
ERROR - Crashlytics Developer Tools error.
java.lang.IllegalArgumentException: Invalid API key: null. Check the Crashlytics plugin to make sure that the application has been added successfully! Contact support@crashlytics.com for assistance.
  at com.crashlytics.tools.android.DeveloperTools.processProperties(DeveloperTools.java:439)
  at com.crashlytics.tools.android.DeveloperTools.processArgs(DeveloperTools.java:325)
  at com.crashlytics.tools.android.DeveloperTools.main(DeveloperTools.java:285)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
  at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:43)
  at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:88)
  at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
  at com.crashlytics.tools.gradle.CrashlyticsTaskBuilder.callDevtoolsWrappingRuntimeExceptions(CrashlyticsTaskBuilder.groovy:136)
  at com.crashlytics.tools.gradle.CrashlyticsTaskBuilder.this$2$callDevtoolsWrappingRuntimeExceptions(CrashlyticsTaskBuilder.groovy)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
  at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:361)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
  at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
  at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
  at com.crashlytics.tools.gradle.CrashlyticsTaskBuilder$_crashlyticsCleanupResources_closure2.doCall(CrashlyticsTaskBuilder.groovy:57)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
  at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
  at groovy.lang.Closure.call(Closure.java:412)
  at groovy.lang.Closure.call(Closure.java:425)
  at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:502)
  at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:483)
  at org.gradle.api.internal.tasks.TaskStatusNagger$1.execute(TaskStatusNagger.java:77)
  at org.gradle.api.internal.tasks.TaskStatusNagger$1.execute(TaskStatusNagger.java:73)
  at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
  at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
  at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
  at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
  at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
  at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
  at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
  at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
  at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
  at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
  at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:289)
  at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
  at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
  at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
  at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$1.run(DefaultTaskPlanExecutor.java:33)
  at org.gradle.internal.Factories$1.create(Factories.java:22)
  at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(DefaultCacheAccess.java:198)
  at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(DefaultCacheAccess.java:266)
  at org.gradle.cache.internal.DefaultPersistentDirectoryStore.longRunningOperation(DefaultPersistentDirectoryStore.java:135)
  at org.gradle.api.internal.changedetection.state.DefaultTaskArtifactStateCacheAccess.longRunningOperation(DefaultTaskArtifactStateCacheAccess.java:95)
  at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:31)
  at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:86)
  at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:29)
  at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)
  at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
  at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:67)
  at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
  at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)
  at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:54)
  at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:166)
  at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:113)
  at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:81)
  at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:64)
  at org.gradle.tooling.internal.provider.BuildModelAction.run(BuildModelAction.java:70)
  at org.gradle.tooling.internal.provider.BuildModelAction.run(BuildModelAction.java:30)
  at org.gradle.tooling.internal.provider.ConfiguringBuildAction.run(ConfiguringBuildAction.java:108)
  at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:35)
  at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:45)
  at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:42)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:24)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.StartStopIfBuildAndStop.execute(StartStopIfBuildAndStop.java:33)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.ReturnResult.execute(ReturnResult.java:34)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:71)
  at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:69)
  at org.gradle.util.Swapper.swap(Swapper.java:38)
  at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:69)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:60)
  at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:60)
  at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:45)
  at org.gradle.launcher.daemon.server.DaemonStateCoordinator.runCommand(DaemonStateCoordinator.java:186)
  at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy.doBuild(StartBuildOrRespondWithBusy.java:49)
  at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:34)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.HandleStop.execute(HandleStop.java:36)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.DaemonHygieneAction.execute(DaemonHygieneAction.java:39)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.CatchAndForwardDaemonFailure.execute(CatchAndForwardDaemonFailure.java:32)
  at org.gradle.launcher.daemon.server.exec.DaemonCommandExecution.proceed(DaemonCommandExecution.java:125)
  at org.gradle.launcher.daemon.server.exec.DefaultDaemonCommandExecuter.executeCommand(DefaultDaemonCommandExecuter.java:51)
  at org.gradle.launcher.daemon.server.DefaultIncomingConnectionHandler$ConnectionWorker.handleCommand(DefaultIncomingConnectionHandler.java:155)
  at org.gradle.launcher.daemon.server.DefaultIncomingConnectionHandler$ConnectionWorker.receiveAndHandleCommand(DefaultIncomingConnectionHandler.java:128)
  at org.gradle.launcher.daemon.server.DefaultIncomingConnectionHandler$ConnectionWorker.run(DefaultIncomingConnectionHandler.java:116)
  at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
  at java.lang.Thread.run(Thread.java:695)
usage: com.crashlytics.tools.android.DeveloperTools
 -androidManifest <androidManifestPath>   Path to AndroidManifest.xml)
 -androidRes <androidResPath>             Path to Android resources (res/
                                          folder)
 -apiSecret <key>                         API Secret for the project
 -buildEvent                              Notify Crashlytics that a build
                                          event has occurred
 -cleanupResourceFile                     Remove Crashlytics-generated
                                          resource files
 -generateResourceFile                    Generate Crashlytics-required
                                          resources for the project.
 -help                                    Display command help.
 -obfuscator <obfuscatorId>               Optionally specify an obfuscator
                                          vendor identifier for use with
                                          storeDeobs.
 -obVer <obfuscatorVersion>               Optionally specify the
                                          obfuscator vendor software
                                          version for use with obfuscator.
 -projectPath <path>                      Path to Android project root
 -properties <propertiesarg>              Properties file that overrides
                                          the input properties
 -quiet                                   Silent command line output
 -requireUploadSuccess                    Throw an exception if the
                                          deobfuscation upload was not
                                          successful
 -resourceCheck                           Check if a resource file already
                                          exists.
 -storeDeobs <file>                       Store the specified
                                          deobfuscation file in
                                          preparation for upload.
 -tool <toolarg>                          Name of the build tool
 -uploadDeobs                             Attempt to upload deobfuscation
                                          file(s) to Crashlytics servers.
 -uploadDist <file>
 -verbose                                 Verbose command line output
 -version <versionarg>                    Version of the build tool

5 个答案:

答案 0 :(得分:40)

有一个类似的问题:我不得不在调试版本中关闭crashlytics的报告。 Michael的回答对我没有帮助:在我将ext.enableCrashlytics = false添加到gradle配置后,crashlytics在启动时销毁了应用程序。感谢Github我找到了一个有效的解决方案:

的build.gradle:

//...
android {
    buildTypes {
        debug {
            // enable crashlytics where you need
            buildConfigField "boolean", "USE_CRASHLYTICS", "false"
            ext.enableCrashlytics = false
        }
    }
}
// ...

应用程序类

@Override
public void onCreate() {
    //...
    if ( BuildConfig.USE_CRASHLYTICS ) {
        Crashlytics.start(this);
    }
    //...
}

修改

根据slott's comment:使用Fabric,你应该使用这样的东西:

Fabric.with(this, new Crashlytics.Builder().core(new CrashlyticsCore.Builder().disabled(!BuildConfig.USE_CRASHLYTICS).build()).build());

答案 1 :(得分:17)

非常感谢@bonnell通过电子邮件回答我的问题。

只需将 ext.enableCrashlytics = false 添加到您不希望Crashlytics成为其中一员的任何构建/风格中。

获得解决我问题的完整答案:

buildscript {
    repositories {
        maven { url 'http://download.crashlytics.com/maven' }
    }

    dependencies {
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
    }
}
apply plugin: 'android'
apply plugin: 'crashlytics'
repositories {
    maven { url 'http://download.crashlytics.com/maven' }
}


android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        packageName "com.mcarrano.example"
    }

    buildTypes {
        debug {
            packageNameSuffix '.debug'
            versionNameSuffix '-DEBUG'
        }

        release {
            runProguard true
            debuggable false
            proguardFile 'proguard-rules.txt'
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
        }
    }

    productFlavors {
        google {
            packageName = android.defaultConfig.packageName
            versionName = android.defaultConfig.versionName
            ext.enableCrashlytics = false
        }
        beta {
            packageName = android.defaultConfig.packageName + ".beta"
            versionName = android.defaultConfig.versionName + "-BETA"
        }
        lab {
            packageName = android.defaultConfig.packageName + ".lab"
            versionName = android.defaultConfig.versionName + "-LAB"
            ext.enableCrashlytics = false
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
        google {
            java.srcDirs = ['src/google/java']
        }
        beta {
            manifest.srcFile 'src/beta/AndroidManifest.xml'
            java.srcDirs = ['src/beta/java']
        }
        lab {
            java.srcDirs = ['src/lab/java']
        }
    }

}

dependencies {
    compile 'com.android.support:support-v4:19.0.+'
    compile 'com.android.support:appcompat-v7:19.0.+'
    betaCompile 'com.crashlytics.android:crashlytics:1.+'
}

答案 2 :(得分:3)

对于遇到此问题的其他人,我的问题是我的AndroidManifest.xml文件中没有定义Crashlytics键,例如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapp"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application android:label="@string/app_name">
        <activity android:name="MyActivity" android:label="@string/app_name" />

        <meta-data android:name="com.crashlytics.ApiKey" 
                   android:value="YOUR_API_KEY"/>
    </application>
</manifest> 

答案 3 :(得分:0)

如果我想通过crashlytics分享构建。

我需要安装Fabric插件&amp;寄存器 在那之后..

**

  

debug {               ext.enableCrashlytics = true           }

**

在构建应用程序之后,我们可以通过crashlytics轻松共享

由于

答案 4 :(得分:0)

停止针对调试版本的崩溃,我在第一个(开始)活动

中的初始化代码之后添加了下面的代码
CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
    Fabric.with(this, new Crashlytics.Builder().core(core).build(), new Crashlytics());