Android Studio has introduced a lot of new features that make our lifes a lot easier. One of those features is generating signed apks using Gradle. The basic idea is that you provide all the required properties (keystore password, alias and alias password) in the build.gradle file of the application you want to sign. But what happens if you don’t wan’t to add the keystore and/or credentials in the Version Control System (VCS) ? The following tutorial presents a solution to this problem.
Gradle allows us to include configurations from external files using the “apply from:” keyword, so, we could define the block that contains the app signing configuration in a file that can be omitted from VCS. The project will compile and launch normally but the user can generate a signed apk file only if he has a keystore and the appropriate credentials.
Because I’ve seen more than one solution, and not all of them worked for me, I decided to create a little tutorial and try to explain the process.
To add external signing configuration to your project you need to :
- have the keystore and credentials
- extract signing configurations to a separate file
- add a property to gradle that will point to the location of the signing config
- use the signing configuration
Keystore and credentials
You need to have a keystore and the credentials to access it. You can place the keystore file anywhere you want. The example project contains a sample keystore located in the .signing folder.
Signing configuration
A project can have more than one signing configurations and they need to be defined in the signingConfigs block . A valid configuration defines the following properties : storeFile, storePassword, keyAlias and keyPassword.
Additionally, for every signing configuration you need to define a build type. Android Studio provides default signing configuration and build type for debug buids and you don’t need to declare them in the build.gradle file.
So, create a file and name it MyApp.gradle and add the following content :
android { signingConfigs { release { storeFile file(project.property("MyApp.signing") + ".jks") storePassword "android" keyAlias "tutorial" keyPassword "tutorial" } } buildTypes { release { signingConfig signingConfigs.release } } } |
The credentials from the above snippet apply to the key store provided in the example. You can see that the path for the key store is specified as a property, we’ll get back to this a little later.
I usually place this signing configuration file in the same directory with the key store, and this has some advantages described in the next step.
Gradle property
Now that we have a key store and a signing configuration, we need to tell our project to use them. We can add any number of properties to a project as long as they are defined in a gradle.properties file located at the root of your project. If you are using Android Studio 0.5.3 this file will be created automatically for you, otherwise you need to create it. Do not mistake with local.properties !
Remember that we used a property for specifying the key store location ? That is the property we need to define in our gradle.properties file and it could look like this (replace with path to your key store) :
MyApp.signing = D:\\REPOSITORIES\\blog_articles\\SigningConfig\\.signing\\MyApp |
Notice that we don’t specify the file extension in the property value, this allows us to use the same property for both the key store and the signing configuration.
Project integration
The last step is to make use of the property defined above. In order to do that you need to add the following code to the build.gradle file of your app :
if (project.hasProperty("MyApp.signing")
&& new File(project.property("MyApp.signing") + ".gradle").exists()) {
apply from: project.property("MyApp.signing") + ".gradle";
} |
The code above :
- checks if the project has a property named “MyApp.signing” (existence of the property defined in the gradle.properties file);
- checks if there is a file at the path indicated by the property (notice the extension is added manually – same property used for key store and .gradle file);
- if the .gradle file exists, apply configuration from the file to this configuration .
You’re done ! You can generate your apk from the command line or from the Gradle tasks provided by Android Studio (which will generate tasks automatically when you add new signing configurations or product flavors).
You can find a working sample here !
Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.
Thank you for reading, I’m glad you found the article useful.
April 11, 2014 at 7:38 am