variantOutput.getPackageApplication()已过时

时间:2019-01-15 21:13:43

标签: gradle android-gradle build.gradle deprecation-warning build-variant

在Gradle 4.10.1和Android Gradle插件更新为3.3.0的情况下,我收到以下警告:

  

警告:API'variantOutput.getPackageApplication()'已过时,并已替换为'variant.getPackageApplicationProvider()'。

该行以及周围的上下文(该上下文通过build变体分配输出文件名):

applicationVariants.all { variant ->
    variant.outputs.all { output ->

        if (variant.getBuildType().getName() in rootProject.archiveBuildTypes) {

            def buildType = variant.getBuildType().getName()
            if (variant.versionName != null) {

                def baseName = output.baseName.toLowerCase()
                String fileName = "${rootProject.name}_${variant.versionName}-${baseName}.apk"

                // this is the line:
                outputFileName = new File(output.outputFile.parent, fileName).getName()
            }
        }
    }
}

migration guide并没有太大帮助;尽管variant.outputs.all可能有错-只是不知道要替换的内容-迁移指南仅涉及任务而不是构建变体。禁用File → Settings → Experimental → Gradle → Only sync the active variant时,我会收到更多的弃用警告(要点是,这些方法都没有被直接调用):

WARNING: API 'variant.getAssemble()' is obsolete and has been replaced with 'variant.getAssembleProvider()'.
WARNING: API 'variantOutput.getProcessResources()' is obsolete and has been replaced with 'variantOutput.getProcessResourcesProvider()'.
WARNING: API 'variantOutput.getProcessManifest()' is obsolete and has been replaced with 'variantOutput.getProcessManifestProvider()'.
WARNING: API 'variant.getMergeResources()' is obsolete and has been replaced with 'variant.getMergeResourcesProvider()'.
WARNING: API 'variant.getMergeAssets()' is obsolete and has been replaced with 'variant.getMergeAssetsProvider()'.
WARNING: API 'variant.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'.
WARNING: API 'variant.getExternalNativeBuildTasks()' is obsolete and has been replaced with 'variant.getExternalNativeBuildProviders()'.
WARNING: API 'variantOutput.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'.

问:通过迁移到新的API如何避免这些过时的警告?

10 个答案:

答案 0 :(得分:9)

variantOutput.getPackageApplication()是由已更改的变量API引起的。

  

output.outputFile.parent更改为variant.getPackageApplicationProvider().get().outputs.files[1]至少是一个临时解决方法。

来源:@Selvin


variant.getExternalNativeBuildTasks()io.fabric插件引起。

  

下一版io.fabric插件将使用variant.getExternalNativeBuildProviders()

来源:116408637confirmation获得承诺的修复。


registerResGeneratingTask已过时,使用registerGeneratedResFolders(FileCollection)com.google.gms.google-services引起。

blog post说明了如何通过添加该插件生成的XML资源来完全摆脱com.google.gms.google-services插件。从build/generated/res/google-services/debug/values/values.xml到常规debug/values/values.xml


最简单,最省力的方法可能是:

buildscript {
    repositories {
        google()
        maven { url "https://maven.fabric.io/public" }
    }
    dependencies {
        //noinspection GradleDependency
        classpath "com.android.tools.build:gradle:3.2.1"
        classpath "io.fabric.tools:gradle:1.28.1"
    }
}

这些warnings均未以任何方式改变行为。

答案 1 :(得分:3)

将Fabric gradle插件更新为 1.28.1

dependencies {
   classpath 'io.fabric.tools:gradle:1.28.1'
}

变更日志: https://docs.fabric.io/android/changelog.html#march-15-2019

  

通过切换到Gradle的避免任务配置API(如果有)来消除过时的API警告。

答案 2 :(得分:1)

所以我遇到了同样的问题(截至目前,运行Gradle 5.4.1)。此外,我没有看到一个有效涵盖应用程序项目和库项目的答案。

因此,我想做一些理论上可以在任何项目中使用的东西,以便在需要时为整个项目创建一个build.gradle。因为结果非常好,所以我认为我会添加它,以防有人想要对应用程序和库项目都适用的东西。

相关部分在这里。

android {
    if (it instanceof com.android.build.gradle.AppExtension) {
        it.applicationVariants.all { com.android.build.gradle.api.ApplicationVariant variant ->
            configureOutputFileName(variant, project)
        }
    } else if (it instanceof com.android.build.gradle.LibraryExtension) {
        it.libraryVariants.all { com.android.build.gradle.api.LibraryVariant variant ->
            configureOutputFileName(variant, project)
        }
    }
}

简单地调用下面的方法。

@SuppressWarnings("UnnecessaryQualifiedReference")
private void configureOutputFileName(com.android.build.gradle.api.BaseVariant variant, Project project) {
    variant.outputs.all { output ->
        def buildType = variant.buildType.name
        String tmpOutputFileName = outputFileName
        if (variant instanceof com.android.build.gradle.api.ApplicationVariant) {
            String fileName = "${project.name}-${variant.versionName}_${buildType}.apk"
            def defaultOutputDir = variant.packageApplicationProvider.get().outputDirectory
            tmpOutputFileName = new File(defaultOutputDir.absolutePath, fileName).name
        }
        if (variant instanceof com.android.build.gradle.api.LibraryVariant) {
            String fileName = "${project.name}_${buildType}.aar"
            def defaultOutputDir = variant.packageLibraryProvider.get().destinationDirectory.asFile.get()
            tmpOutputFileName = new File(defaultOutputDir.absolutePath, fileName).name
        }
        println(tmpOutputFileName)
        outputFileName = tmpOutputFileName
    }
}

答案 3 :(得分:0)

我以前经常这样写:

android.applicationVariants.all { variant ->
    if ("release" == variant.buildType.name) {
        variant.outputs.all { output ->
            outputFileName = output.outputFile.name.replace("-release", "")
        }
        variant.assemble.doLast {
            variant.outputs.all { output ->
                delete output.outputFile.parent + "/output.json"
                copy {
                    from output.outputFile.parent
                    into output.outputFile.parentFile.parent
                }
                delete output.outputFile.parent
            }
        }
    }
}

每次都会弹出警告,例如打开AS,同步,清理...

然后我找到了一种写方法,它只会出现在build中,而不会每次弹出。

android.applicationVariants.all { variant ->
    if ("release" == variant.buildType.name) {
        assembleRelease.doLast {
            variant.outputs.all { output ->
                delete output.outputFile.parent + "/output.json"
                copy {
                    from output.outputFile.parent
                    into output.outputFile.parentFile.parent
                    rename { filename ->
                        filename.replace("-release", "")
                    }
                }
                delete output.outputFile.parent
            }
        }
    }
}

如果您不想每次都弹出警告,则这些提示可能会为您提供一些提示。

答案 4 :(得分:0)

您可以使用更简单的示例,类似于以下示例:

applicationVariants.all { variant ->
            variant.outputs.all { output ->
                outputFileName = "${globalScope.project.name}-${variant.versionName}_${output.baseName}.apk"
            }
        }

,结果将为my_app-1.9.8_flavor1-release.apk

在您的代码中,有问题的部分(生成警告)为output.outputFile

..
outputFileName = new File(output.outputFile.parent, fileName).getName()
..

答案 5 :(得分:0)

问题是output.outputFile在内部调用getPackageApplication()

我通过自己设置输出文件的目录和名称来解决此问题。

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputDir = new File("${project.buildDir.absolutePath}/outputs/apk/${variant.flavorName}/${variant.buildType.name}")
        def outputFileName = "app-${variant.flavorName}-${variant.buildType.name}.apk"
        // def outputFile = new File("$outputDir/$outputFileName")

        variant.packageApplicationProvider.get().outputDirectory = new File("$outputDir")
        output.outputFileName = outputFileName
    }
}

答案 6 :(得分:0)

我没有在gradle中使用output.outputFile.parentvariantOutput.getPackageApplication()过时警告的原因是dex计数插件。我将其更新为0.8.6,警告消失了。

'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.8.6'

答案 7 :(得分:0)

以下警告的根源是 output.outputFile

  

警告:API'variantOutput.getPackageApplication()'已过时,并已替换为'variant.getPackageApplicationProvider()'。

要摆脱针对Android Gradle插件 3.4.0 + 的警告,您可以按以下步骤手动组装输出路径:

def selfAssembledOutputPath = new File("${project.buildDir.absolutePath}/outputs/apk/${variant.flavorName}/${variant.buildType.name}")

然后将下面的行替换为上面定义的selfAssembledOutputPath

// this is the line:
outputFileName = selfAssembledOutputPath

答案 8 :(得分:0)

您还可以使用较早版本的gradle。我将gradle版本从3.5.0更改为3.2.1,并且可以正常工作。enter image description here

答案 9 :(得分:0)

少一些狡猾的解决方案:

def variant = findYourVariantSomehow()
def output = findCorrectOutputInVariant(variant)
def fileName = output.outputFileName

def fileDir = variant.packageApplicationProvider.get().outputDirectory.get()

def apkFile = file("$fileDir/$fileName")

source