Automate Android APK Signing using Gulp for React Capacitor App (KeyStore, JarSigner, KeyTool)

Hello , in this article we are going to discuss how we can automate android APK signing process using gulp.

If you would like to know how we can automate build process of react-capacitor app you may check  Build Automation for React-Ionic-Capacitor App using Gulp

We are going to use same app mentioned above, to create a signed APK. You can check Ionicframework Playstore deployment page to basic steps that are involved in this process.

As a first step we are going to create the KeyStore file, this step should not be included in automation process as it should be done only once. So let's create.

We will use command : keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

You may change alias name, keystore filename etc as you wish.

Once you put the command in command promt or terminal it will ask few details : Password, First Name/Last Name,


As you can see keystore file has been saved in project folder.




Well now as our keystore file is created, we can start writing gulp task for JarSigner and KeyTool.

JarSigner command is  : jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore HelloWorld-release-unsigned.apk alias_name

If we run JarSigner command in cmd/terminal it will expect password that we mentioned while generating KeyStore file.


But as we are automating the process we need to pass the password along with the same command. There are to flags available we can use to pass the password : keypass and storepass

jarsigner -verbose -sigalg SHA1withRSA -keypass "recompile" -storepass "recompile" -digestalg SHA1 -keystore my-release-key.keystore ./android/app/build/outputs/apk/release/app-release-unsigned.apk alias_name


Above command can be executed at once and won't interrupt the user for password.

So lets write the gulp task for the same.


gulp.task('jarSign',function (cb) {      
    exec('jarsigner -verbose -sigalg SHA1withRSA -keypass "recompile" 
-storepass "recompile" -digestalg SHA1 -keystore my-release-key.keystore 
./android/app/build/outputs/apk/release/app-release-unsigned.apk alias_name',
{ stdio:'inherit'},function (err,stdout,stderr) {
        console.log(stderr)         
        console.log(stdout)         
        cb(err)     
    }) 
})

Also we need to write a main 'ApkSiging' task


gulp.task('ApkSigning',gulp.series('jarSign',function (done) {
    done() 
}))

Well let's test the main/signer task :





Everything working upto this point. Now we need to create signed apk which is the final step using KeyTool. Commend is : zipalign -v 4 react-capacitor-release-unsigned.apk Test-Signed.apk

Here is a problem if we trying execute zipalign command in project path using cmd or terminal, it will not recognise this command. So we need to specify full path of zipalign executable in this command. It is found under android sdk folder. Usually under build-tools/{SDK_Version}/zipalign .



But for automation process it's not proper to give a static path to a command, as this may change.

So as a solution what we can do, we can copy zipalign executable to our project path, so that we get a direct access, also we won't have to specify a static path.

Let's do that.


ZipAlign Command is : zipalign -v 4 app-release-unsigned.apk React-Capacitor-Signed.apk

Now, we can write the gulp task for zipalign.


gulp.task('ZipAlign',function (cb) {      
    exec('./zipalign -v 4 ./android/app/build/outputs/apk/release/app-release-unsigned.apk React-Capacitor-Signed.apk',{ stdio:'inherit'},function (err,stdout,stderr) {
        console.log(stderr)         
        console.log(stdout)         
        cb(err)     
    }) 
}) 

Also let's update 'ApkSigning' or main process by adding 'ZipAlign' task to queue


gulp.task('ApkSigning',gulp.series('jarSign','ZipAlign',function (done) {
    done() 
}))

Now lets run it:



Great..now you can see its working and creating a signed apk and everything is automated.

Also notice Sign APK is generated in project's root path(This you can change) :



So as a last tip if you like to automate build process along with apk signing with a single gulp script, we can write main task like this :


gulp.task('AutomateBuildSigning',gulp.series('buildApp','performSync',
'cleanGradle','debugBuild','releaseBuild','jarSign','ZipAlign',function (done) {
    done() 
}))

What above task will do, it will build your project with latest code update, create debug and release build, also it will jarSign and zipAlign your release build to create a signed apk ready to be published at PlayStore.

Hope you like this article...Kindly subscribe to our blog. Thank you.

Comments

  1. Hello, is there a way of contacting the writer of this article? We would like to make him a proposal to work with us expanding on this. Please write us at careers (at) yourcompanyinestonia (dot) com. Thanks!

    ReplyDelete

Post a Comment

Popular posts from this blog

Best coding practice and structure for redux saga with react hooks | Separate watcher and worker saga | React clean code

How to use redux with React Hooks - Creating TodoList

Setting up Redux Devtools for React applications