您好,登錄后才能下訂單哦!
Android Studio自從更新3.0,gradle更新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到構(gòu)建過程中
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"
// ???????拷貝文件的始發(fā)地
? ??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目錄下面去查找對應(yīng)的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"
????}
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。