zoukankan      html  css  js  c++  java
  • Android gradle 相关配置

    有时候我们需要重命名输出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 Snapshots
    def isReleaseBuild() {
        return !android.defaultConfig.versionName.contains("SNAPSHOT")
    }
    
    // 获取仓库url
    def 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:1.0.0@aar'  
    }
    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() }
  • 相关阅读:
    LeetCode Array Easy 1. Two Sum
    关于VS2015 发布.net mvc 网站失败的问题
    2016计蒜之道复赛 百度地图的实时路况 floyd+cdq分治
    2016计蒜之道复赛 菜鸟物流的运输网络 网络流EK
    HDU5715 XOR 游戏 二分+字典树+dp
    HDU5697 刷题计划 dp+最小乘积生成树
    codeforces 687D Dividing Kingdom II 带权并查集(dsu)
    codeforces 687C
    codeforces 687B
    HDU 5693 D Game 区间dp
  • 原文地址:https://www.cnblogs.com/popqq520/p/9237985.html
Copyright © 2011-2022 走看看