Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grade Plugin 3-alpha1 outputFile causes error

I'm trying to update a project to Android Studio 3.

The following snippet is no longer accepted in a build.gradle file.

applicationVariants.all { variant ->

    variant.outputs.each { out ->

        def oFile =out.outputFile  // This line causes failure

        //...
    }
}

The error is a simple "Not Valid" yet the intellisense suggests it is as it autocompletes fine.

Checking the idea.log shows the following exception:

Caused by: java.lang.RuntimeException: Not valid.
at com.android.ide.common.build.ApkData.getMainOutputFile(ApkData.java:136)
at com.android.build.gradle.internal.api.BaseVariantOutputImpl.getOutputFile(BaseVariantOutputImpl.java:60)
at com.android.build.gradle.internal.api.ApkVariantOutputImpl_Decorated.getOutputFile(Unknown Source)
at org.gradle.internal.metaobject.BeanDynamicObject$MetaClassAdapter.getProperty(BeanDynamicObject.java:228)
at org.gradle.internal.metaobject.BeanDynamicObject.tryGetProperty(BeanDynamicObject.java:171)
at org.gradle.internal.metaobject.CompositeDynamicObject.tryGetProperty(CompositeDynamicObject.java:55)
at org.gradle.internal.metaobject.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:59)
at com.android.build.gradle.internal.api.ApkVariantOutputImpl_Decorated.getProperty(Unknown Source)

I can find no documentation on Gradle 4. Would this be a bug or a function that is deprecated perhaps?

Also filed at: https://issuetracker.google.com/issues/38408231

like image 676
Kuffs Avatar asked May 18 '17 09:05

Kuffs


2 Answers

Update: Fix for APK renaming:

Use all iterators instead of each:

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

Previous answer, still valid: It's a known problem with the new plugin:

This build error occurs because variant-specific tasks are no longer created during the configuration stage. This results in the plugin not knowing all of its outputs up front, but it also means faster configuration times. As an alternative, we will introduce new APIs to provide similar functionality.

We need to wait for an alternative way of doing that, according to the Migration Guide.

like image 163
adamus Avatar answered Oct 22 '22 05:10

adamus


If your failing plugin support explicitly setting file path, it might be a work around.

I had problems with Fabrics crashlyticsUploadDistributionRelease task, giving me the same stack trace as above. I fixed it by explicitly setting the output file path property in app/build.gradle:

ext.betaDistributionApkFilePath = "app/build/outputs/apk/release/app-release.apk"

like image 32
jpgw Avatar answered Oct 22 '22 04:10

jpgw