標籤:不必要 oca 才有 命名 osi cti pass arch each
有時候我們需要重新命名輸出apk檔案名稱,在Android studio 3.0以前我們是這樣寫的:
applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith(‘.apk‘)) { //這裡修改apk檔案名稱 def fileName = getProject().name + "-" + variant.baseName + "-${defaultConfig.versionName}.apk" output.outputFile = new File(outputFile.parent, fileName) } } }
但是在android studio 3.0之後這個寫法就會報錯:
Cannot set the value of read-only property ‘outputFile‘ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.Open File
這個錯誤大概意思就是outputFile這個引用現在是“read-only ”(唯讀)的,不能重新賦予新的對象。
經過多方折騰驗證,最後正確的寫法是這樣的:
applicationVariants.all { variant -> variant.outputs.all { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith(‘.apk‘)) { //這裡修改apk檔案名稱 def fileName = getProject().name + "-" + variant.baseName + "-${defaultConfig.versionName}.apk" outputFileName = fileName } } }
需要注意的是原先的 variant.outputs.each 一定要改成 variant.outputs.all,不然也會報錯的哦。
那麼如果你的項目是SDK而不是app呢,那就騷味改動下:
libraryVariants.all { variant -> variant.outputs.all { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith(‘.aar‘)) { //這裡修改apk檔案名稱 def fileName = getProject().name + "-" + variant.baseName + "-${defaultConfig.versionName}.aar" outputFileName = fileName } } }
嗯,沒錯就是把 applicationVariants改成libraryVariants就好了。
接下來說說maven的發布指令碼:
首先在頭部添加:
apply plugin: "maven"apply plugin: ‘signing‘
然後:
//============發布指令碼=================//maven 倉庫地址
//ext.RELEASE_URL = ‘file://localhost/Users/Admin/mySDK‘//本地倉庫
//ext.SNAPSHOT_URL = ‘file://localhost/Users/Admin/maven-snapshots‘
ext.RELEASE_URL = ‘http://127.0.0.1:8081/repository/maven-releases/‘ext.SNAPSHOT_URL = ‘http://127.0.0.1:8081/repository/maven-snapshots/‘//使用者名稱和密碼ext.USERNAME = ‘username‘ext.PWD = ‘pwd‘// 判斷版本是Release or Snapshotsdef isReleaseBuild() { return !android.defaultConfig.versionName.contains("SNAPSHOT")}// 擷取倉庫urldef getRepositoryUrl() { return isReleaseBuild() ? RELEASE_URL : SNAPSHOT_URL}uploadArchives { repositories { mavenDeployer { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } repository(url: RELEASE_URL) { authentication(userName: USERNAME, password: PWD) } snapshotRepository(url: SNAPSHOT_URL) { authentication(userName: USERNAME, password: PWD) } pom.project { version android.defaultConfig.versionName //版本號碼 artifactId ‘projectName‘ //項目名 groupId ‘com.demo.test‘ //包名,也可以是其他的唯一標識 packaging ‘aar‘ //打包方式 description ‘test‘ //描述 } } }}// 進行數位簽章signing { // 當 發布版本 & 存在"uploadArchives"任務時,才執行 required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } sign configurations.archives}
當然,maven倉庫地址也可以改成你本地地址,不用裝maven環境也可以。
這裡說下碰到的一個坑,一旦你通過maven發布後,發現有點小問題,改了下代碼,但是並沒有改版本號碼再執行指令碼重新發布,雖然指令碼執行成功,實際上伺服器上的包並沒有更新,哪怕你把伺服器上的包刪掉再發布也沒用,必須要更新版本號碼才有效,所以在發布release版本前先發SNAPSHOT版驗證,驗證好再發布release,以免遇到不必要的問題。
最後就是引用:
dependencies { ..... implementation ‘com.demo.test:test:[email protected]‘ }repositories {
//maven { url ‘file://D:/Users/Admin/mySDK‘ }//本地倉庫
//maven { url ‘file://D:/Users/Admin/maven-snapshots‘ } maven { url ‘http://127.0.0.1:8081/repository/maven-releases/‘ } maven { url ‘http://127.0.0.1:8081/repository/maven-snapshots/‘ } mavenCentral()}
Android gradle 相關配置