Updated 1 March 2017
Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program. ~ (We know who have said that other wise no point of telling it. If you still don’t know follow this link)
It’s two months now that no blog come from my side. But sit back and relax, blog updates will now come every week to keep you updated and you can enjoy reading the blog while learning some handy tricks and tech.
Build.gradle file allows you to configure your project or module as per the project need. Following are some handy options that will help you to take the benefits of it.
defining signing configuration allows creating different signing configuration used in the project. It can be used in running built variant in release mode or whenever there are different project flavors in the application.
1 2 3 4 5 6 7 8 9 10 11 12 |
signingConfigs { config { try { keyAlias project.MOBIKUL_HYERLOCAL_KEY storeFile file(project.MOBIKUL_HYERLOCAL_STORE_FILE_PATH) keyPassword project.MOBIKUL_HYERLOCAL_KEY_PASSWORD storePassword project.MOBIKUL_HYERLOCAL_STORE_PASSWORD } catch (ex) { throw new InvalidUserDataException("You should define signingConfigs properly in gradle.properties. \n" + ex.getMessage()); } } } |
For defining an external variable like MOBIKUL_HYERLOCAL_KEY use gradle.properties and access via project.MOBIKUL_HYERLOCAL_KEY. Follow EXTERNALIZING THE DEPENDENCY AND KEYWORD section for more
You can access the signing configuration inside buildTypes
1 2 3 4 5 6 7 8 9 10 11 |
buildTypes { debug { debuggable true minifyEnabled false } release { <strong> signingConfig signingConfigs.config</strong> shrinkResources true minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } |
Externalizing the keywords and dependency allows you to build the project on different dependency without having the pain to download and maintain it everywhere.
Thus, no conflict dependency issue that occurs in support libraries and play service libraries. Keywords can also be defined in the same manner.
1 2 3 4 |
compile "com.squareup.picasso:picasso:$project.PICASSO_VERSION" compile "com.android.support:design:$project.SUPPORT_LIBRARY_VERSION" compile "com.android.support:cardview-v7:$project.SUPPORT_LIBRARY_VERSION" compile "de.hdodenhof:circleimageview:$project.CIRCLULAR_IMAGE_VIEW_VERSION" |
gradle.properties
This is how this file will look:
1 2 3 4 5 |
PICASSO_VERSION=2.5.2 SUPPORT_LIBRARY_VERSION=25.1.0 CIRCLULAR_IMAGE_VIEW_VERSION=2.1.0 MOBIKUL_HYERLOCAL_KEY=mobikul_hyperlocal_live_key |
You can create all the constant (if you want) in the build file. Moreover, You have control to change the constant depending upon the buildType and like that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
buildTypes { debug { debuggable true minifyEnabled false // ANDROID STRING CONSTATNT resValue "string", "app_name", "Test Hyperlocal" // JAVA BUILD CONSTANT buildConfigField "String", "BASE_URL", '\"http://192.168.10.114/hyperlocal/index.php/\"' buildConfigField "boolean", "IS_SHOW_SPLASHSCREEN", "false" } release { // Android string constant resValue "string", "app_name", "Mobikul Hyperlocal" // JAVA CONSTANTS buildConfigField "String", "base_url", '"http://mobikul.webkul.com/hyperlocal/index.php/"' buildConfigField "boolean", "isShowSplashScreen", "true" signingConfig signingConfigs.config shrinkResources true minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } |
Please note while creating String resource you need to add extra quotes.
All the JAVA CONSTANT are defined in BuildConfig.java file generated at build time
1 2 3 4 5 6 7 8 9 10 11 |
public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true"); public static final String APPLICATION_ID = "com.webkul.mobikulhyperlocal"; public static final String BUILD_TYPE = "debug"; public static final String FLAVOR = ""; public static final int VERSION_CODE = 7; public static final String VERSION_NAME = "1.07"; // Fields from build type: debug public static final String BASE_URL = "http://192.168.10.114/hyperlocal/index.php/"; public static final boolean IS_SHOW_SPLASHSCREEN = false; } |
You can access it easily from anywhere:
1 2 3 4 |
if (!BuildConfig.IS_SHOW_SPLASHSCREEN) { displayHomeWithoutDelay(); return; } |
You can use setProperty method to change the base name.
Here you application name will contain version name along with predefined string instead of app
1 2 3 4 5 |
defaultConfig { setProperty("archivesBaseName", "mobikul-hyperlocal-$versionName") // o/p: mobikul-hyperlocal-VERSION_NAME-Build_type // sample: mobikul-hyperlocal-1.07-debug } |
That all folks.
STAY UPDATED…
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.