如何用gradle执行ionic build android –release的时候自动签名

/ 0评 / 0

假设android的platform和keystore已经有了,我们可以看到如下目录结构

NewImage

其中release-signing.properties这个时候应该是没有的,现在我们新建一个release-signing.properties

storeFile=YiShangpu.keystore
key.store.password=your password
key.alias=YiShangpu
key.alias.password=your password

为什么要新建这个文件呢?

可以参考官方的文档https://cordova.apache.org/docs/en/edge/guide_platforms_android_tools.md.html

cdvReleaseSigningPropertiesFile (default: release-signing.properties)

Path to a .properties file that contains signing information for release builds. The file should look like:

<span class="pln" style="color: #ffffff;">storeFile</span><span class="pun" style="color: #ffffff;">=</span><span class="pln" style="color: #ffffff;">relative</span><span class="pun" style="color: #ffffff;">/</span><span class="pln" style="color: #ffffff;">path</span><span class="pun" style="color: #ffffff;">/</span><span class="pln" style="color: #ffffff;">to</span><span class="pun" style="color: #ffffff;">/</span><span class="pln" style="color: #ffffff;">keystore</span><span class="pun" style="color: #ffffff;">.</span><span class="pln" style="color: #ffffff;">p12<br />storePassword</span><span class="pun" style="color: #ffffff;">=</span><span class="pln" style="color: #ffffff;">SECRET1<br />storeType</span><span class="pun" style="color: #ffffff;">=</span><span class="pln" style="color: #ffffff;">pkcs12<br />keyAlias</span><span class="pun" style="color: #ffffff;">=</span><span class="typ" style="color: #f5896f;">DebugSigningKey</span><span class="pln" style="color: #ffffff;"><br />keyPassword</span><span class="pun" style="color: #ffffff;">=</span><span class="pln" style="color: #ffffff;">SECRET2<br /></span>

storePassword and keyPassword are optional, and will be prompted for if omitted.

对比我们新建的内容和官方文档是有出入的,那为什么我们的需要用key.store.password这种写法呢?

我们回到项目中,打开build.gradle,找到 addSigningProps。


def addSigningProps(propsFilePath, signingConfig) {
def propsFile = file(propsFilePath)
def props = new Properties()
propsFile.withReader { reader ->
props.load(reader)
}

def storeFile = new File(props.get('key.store') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'storeFile'))
if (!storeFile.isAbsolute()) {
storeFile = RelativePath.parse(true, storeFile.toString()).getFile(propsFile.getParentFile())
}
if (!storeFile.exists()) {
throw new FileNotFoundException('Keystore file does not exist: ' + storeFile.getAbsolutePath())
}
signingConfig.keyAlias = props.get('key.alias') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'keyAlias')
signingConfig.keyPassword = props.get('keyPassword', props.get('key.alias.password', signingConfig.keyPassword))
signingConfig.storeFile = storeFile
signingConfig.storePassword = props.get('storePassword', props.get('key.store.password', signingConfig.storePassword))
def storeType = props.get('storeType', props.get('key.store.type', ''))

if (!storeType) {
def filename = storeFile.getName().toLowerCase();
if (filename.endsWith('.p12') || filename.endsWith('.pfx')) {
storeType = 'pkcs12'
} else {
storeType = signingConfig.storeType // "jks"
}
}
signingConfig.storeType = storeType
}

红色的部分便是原因了。

注意:keystore的路径需要根据实际情况设置。

最后我们执行如下命令即可获得签名后的apk文件。

$ ionic build android —release
your password

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注