温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android Studio3.0以后outputfile不能用更改

发布时间:2020-08-06 13:09:17 来源:网络 阅读:406 作者:专注地一哥 栏目:编程语言

Android Studio自从更新3.0gradle更新3.1.3之后,build.gradle文件中outputfile就不可用了,会报错,既Cannot set the value of read-only property 'outputFile' for object of type com.android.build.gradle.internal.api.LibraryVariantOutputImpl.

所以如果要打包aar,使用自定义路径和文件名称,需要使用新的方法。
如果使用:

apply plugin: 'com.android.library'

就是打包aar
以下是具体的代码,可以直接使用。直接放在build.gradle文件最外面即可使用

android.libraryVariants.all { variant ->

    variant.outputs.all {

        // 自定义输出路径

// variant.getPackageApplication().outputDirectory = new File("C:\\1")

        // 自定义文件名{示例:AppName-Flavor-debug-v1.0.0_201807301409}

        outputFileName = "test.aar"

    }

}

//挂接自定义task到构建过程中

this.project.afterEvaluate { project ->

//    获得build task

    def buildTask = project.tasks.getByName('build')

    if (buildTask == null) {

        throw GradleException('the build task is not found')

    }

    buildTask.doLast {

        copyTask.execute()

    }

}

//自定义copyApk task

task copyTask {

    doLast {

        def fileName = "test.aar"

//        拷贝文件的始发地

    function(){ //交易品种 http://www.fx61.com/faq/muniu/447.html

        def sourceFile = "/build/outputs/aar/" + fileName

//        指定文件拷贝的目的地

        def destationFile = new File("C:\\1 ")

        try {

//            判断文件夹是否存在

            if (!destationFile.exists()) {

                destationFile.mkdir()

            }

            //拷贝

            copy {

                from sourceFile

                into destationFile

                rename {

                    fileName

                }

            }

        } catch (Exception e) {

            e.printStackTrace()

        }

    }

}

上面build之后就在c:\1目录下面去查找对应的aar即可
当然如果使用

apply plugin: 'com.android.application'

就更简单了,直接在最外围放以下代码即可

android.applicationVariants.all { variant ->

    variant.outputs.all {

        // 自定义输出路径

        variant.getPackageApplication().outputDirectory = new File("C:\\1")

        // 自定义文件名{示例:AppName-Flavor-debug-v1.0.0_201807301409}

        outputFileName = "test.aar"

    }

}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI