7

I'm splitting my app based on ABI, not on density, like so:

    splits {
       abi {
           enable true
           reset()
           include 'x86', 'armeabi', 'armeabi-v7a', 'mips', 'arm64-v8a'
           universalApk true
       }
    }

I have multiple flavors, and 2 build types (debug and release). I want to put the universal apk file, that has native libs for all platforms, up on fabric beta. From what I understand, this is supported through the ext.betaDistributionApkFilePath attribute.

I can define this either at the buildType level, or at the flavor level. The problem is I need both build type and flavor to pick up my variant - something like this:

android.applicationVariants.all { variant ->
   variant.ext.betaDistributionApkFilePath = "${buildDir}/outputs/apk/app-${variant.productFlavors[0].name}-universal-${variant.buildType.name}.apk"
}

or

gradle.taskGraph.beforeTask { Task task ->
if(task.name ==~ /crashlyticsUploadDistribution.*/) {
   System.out.println("task name: ${task.name}");
   android.applicationVariants.all { variant ->
       System.out.println("match: crashlyticsUploadDistribution${variant.name}");
       if(task.name ==~ /(?i)crashlyticsUploadDistribution${variant.name}/){
           ext.betaDistributionApkFilePath = "${buildDir}/outputs/apk/app-${variant.productFlavors[0].name}-universal-${variant.buildType.name}.apk"
           System.out.println(ext.betaDistributionApkFilePath);
       }
   }
}

Unfortunately this doesn't seem to work - is there any way to do this currently?

Mark
  • 2,362
  • 18
  • 34
  • This behaves just like not having the variable defined - warns me about multiple apk files, and then exits. – Mark Nov 05 '15 at 18:04
  • Thanks Mark, this should be working as long as the path to universal APK is known. When building is that path being populated correctly? – Mike Bonnell Nov 05 '15 at 21:27
  • If I hard-code the ext.betaDistributionApkFilePath in the config, with no variables, it finds the universal apk file just fine, e.g. ext.betaDistributionApkFilePath="app/build/outputs/apk/app-flavor1-universal-debug.apk". What I'm trying to do is set that path dynamically based on both the build flavor (lite/pro), and the build type (release/debug). Each build flavor and type combination have a unique applicationId. – Mark Nov 06 '15 at 17:13
  • If I set the ext.betaDistributionApkFilePath within the build flavor, I can reference the flavor name with a variable, but I can't set the build type. If I set the variables inside the buildTypes, I can reference the build type as a variable, but I can't reference the flavor name as a variable. – Mark Nov 06 '15 at 17:19
  • The productFlavor part of the name wasn't correct, but that's been fixed above. And the System.out.println() is pointing to a path that exists - doesn't seem to matter to the upload task. – Mark Nov 10 '15 at 19:35
  • Did @MikeBonnell end up getting back to you on this? – Forrest Bice Nov 30 '16 at 17:25
  • @ForrestBice - yes, his answer is marked as the answer below. – Mark Dec 05 '16 at 18:06

1 Answers1

8

For every existing variant you can add an action that will execute before Crashlytics tasks and set ext.betaDistributionApkFilePath according to the variant name. That's how it looks like:

android.applicationVariants.all { variant ->
  variant.outputs.each { output ->
    // Filter is null for universal APKs.
    def filter = output.getFilter(com.android.build.OutputFile.ABI)

    if (filter == null) {
      tasks.findAll {
        it.name.startsWith(
            "crashlyticsUploadDistribution${variant.name.capitalize()}")
      }.each {
        it.doFirst {
          ext.betaDistributionApkFilePath = output.outputFile.absolutePath
        }
      }

      tasks.findAll {
        it.name.startsWith(
            "crashlyticsUploadSymbols${variant.name.capitalize()}")
      }.each {
        it.doFirst {
          ext.betaDistributionApkFilePath = output.outputFile.absolutePath
        }
      }
    }
  }
}
Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59
Michael
  • 53,859
  • 22
  • 133
  • 139
  • Why is `crashlyticsUploadSymbols` required? I can't find that task when running `./gradlew tasks --all` – Forrest Bice Nov 30 '16 at 17:24
  • This task is created when you use Crashlytics NDK. I wrote this code a year ago so now I'm not sure it's really necessary. You can drop this part and check if it works. – Michael Nov 30 '16 at 17:31
  • I ended up taking a different route but from my testing I think might be extraneous now. Thanks for the speedy response! – Forrest Bice Nov 30 '16 at 20:40