variantOutput.getPackageApplication()已过时

问题描述 投票:16回答:6

随着Gradle 4.10.1和Android Gradle插件更新到3.3.0,我收到以下警告:

警告:API'variantOutput.getPackageApplication()'已过时,已被'variant.getPackageApplicationProvider()'取代。

该行,与周围的上下文(由构建变量分配输出文件名):

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来避免这些弃用警告?

gradle android-gradle build.gradle deprecation-warning build-variant
6个回答
9
投票

variantOutput.getPackageApplication()由更改的变体API引起。

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

来源:@Selvin


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

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

来源:116408637; confirmation承诺修复。


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都没有以任何方式改变行为。


4
投票

将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警告。


1
投票

你可以使用更简单的一个,类似于这个例子:

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()
..

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,同步,清理...

然后我找到了一种写入方式,它只会出现在构建中,但不会每次弹出。

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
            }
        }
    }
}

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


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
    }
}

0
投票

所以我得到了同样的问题(截至此日期,运行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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.