android studio 命令行打包

android studio 打包的通用做法是在build 下生成签名包。

这里写图片描述

在些主要记录一下 用命令行打包的方式

1.首先要配置keystore 的路径以及密码,可以直接在app下的gradle下直接配置,也可以打开project Structure 选择app-singing 按照提示填写 Name(release或者debug) 、 key alias等信息,然后在buildType配置签名信息

这里写图片描述

 buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            minifyEnabled true //启用Proguard
            shrinkResources true //是否清理无用资源,依赖于minifyEnabled
            zipAlignEnabled true //是否启用zipAlign压缩
            signingConfig signingConfigs.release
        }

    }

2.这样的方式有一个弊端就是把签名信息全部暴露出去了,所以我们可以把签名信息放到一个properties文件中 并在提交项目到github 或者SVN上时忽略掉提交该文件

我们在根目录下新建一个signing.properties文件来进行配置签名信息
RELEASE_KEY_PASSWORD=****
RELEASE_KEY_ALIAS=****
RELEASE_STORE_PASSWORD=****
RELEASE_STORE_FILE=E:/Android/studio/***
下来我们将从文件中读取stroeFile storePassWord 等信息并赋值给signingConfigs.release 
/**
 * 加载签名配置文件
 */
def loadSigningConfigs() {
    def Properties props = new Properties()
    def propFile = file('../signing.properties') //加载properties文件
    if (propFile.canRead()) {
        props.load(new FileInputStream(propFile))
        if (props != null && props.containsKey('RELEASE_STORE_FILE') && props.containsKey('RELEASE_STORE_PASSWORD') &&
                props.containsKey('RELEASE_KEY_ALIAS') && props.containsKey('RELEASE_KEY_PASSWORD')) {
            android.signingConfigs.release.storeFile = file(props['RELEASE_STORE_FILE'])
            android.signingConfigs.release.storePassword = props['RELEASE_STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['RELEASE_KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['RELEASE_KEY_PASSWORD']
        } else {
            android.buildTypes.release.signingConfig = null
        }
    } else {
        android.buildTypes.release.signingConfig = null
    }
}

然后gradle 中就可以这样来写

android {
    signingConfigs {
        release {
        }
    }
    loadSigningConfigs() //加载签名信息

    }
}

有时候我们想让在运行debug 的时候也使用的是正式签名可以在gradle中配置
统一使用signingConfigs下的release 配置信息

   buildTypes {
        release {
            minifyEnabled true
            signingConfig signingConfigs.release
        }
        debug {
            signingConfig signingConfigs.release
        }
    }

配置完成 我们可以使用as 自带的命令行工具 进行打包, 因为使用的是gradlew 命令所以要在外部打开cmd命令的话一定要进入项目根目录下使用命令 gradlew assembleRelease 进行打包。打包完成后可以在app->build->outputs->apk下看到打包成功的apk
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值