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"

    }

}

更多相关文章

  1. aes文件读取
  2. 把android assets文件夹内的文件存储到sd卡中
  3. android使用sharedPreferences()方法读写文件操作
  4. Android android.support.v4.widget.SlidingPaneLayout 侧滑示例
  5. 在系统里设置文件默认打开APP

随机推荐

  1. anaconda tensorflow-gpu 安装简易指南
  2. IDEA使用总结
  3. 写博客的第一天,学习C语言,嗯.......就这吧
  4. bc用法
  5. linux下如何格式化NTFS分区
  6. awk用法
  7. Eclipse无法安装ADT插件的解决
  8. AWS-EC2遗失秘钥或密码如何重置密码或秘
  9. 计算机网络面试知识点(2)物理层
  10. 数据库面试题(开发者必看)