当android gradle插件从3.0.1更新到3.1.0时,发生错误,gradle代码用于固定资源id

时间:2018-04-08 02:01:49

标签: android-gradle gradle-plugin

以前的代码是(这是public.xml固定资源id的public-xml.gradle)

afterEvaluate {  
    for (variant in android.applicationVariants) {  
        def scope = variant.getVariantData().getScope()  
        String mergeTaskName = scope.getMergeResourcesTask().name  
        def mergeTask = tasks.getByName(mergeTaskName)  

        mergeTask.doLast {  
            copy {  
                int i=0  
                from(android.sourceSets.main.res.srcDirs) {  
                    include 'values/public.xml'  
                    rename 'public.xml', (i++ == 0? "public.xml": "public_${i}.xml")  
                }  

                into(mergeTask.outputDir)  
            }  
        }  
    }  
}

现在错误是

No signature of method: com.android.build.gradle.internal.scope.VariantScopeImpl.getMergeResourcesTask()is applicable for argument types: () values: []
Possible solutions: getMergeJavaResourcesTask()

我检查VariantScopeImp.java的代码,在3.0.1中找到代码

 @Nullable private AndroidTask<MergeResources> mergeResourcesTask;    
3.1.0中的

代码是

@Nullable private MergeResources mergeResourcesTask;    

当我将public-xml.gradle代码转换为

def scope = variant.getVariantData().getScope()
def mergeTask = scope.mergeResourcesTask    

新错误

Cannot invoke method doLast() on null object    

我该怎么做?感谢

1 个答案:

答案 0 :(得分:1)

尝试: def mergeTask = variant.getMergeResources() 为3.1.4工作