Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Read build.gradle properties inside class

I would like to have access to build.gradle properties so I can automatize some processes in my app.

This is the structure i have at the moment:

rootProject/build.gradle

buildscript {
    ext {
        buildTools = "25.0.2"
        minSdk = 16
        compileSdk = 23
        targetSdk = 23

        versions = [supportLib         : '25.0.0',
                    multidex           : '1.0.1',
                    playServices       : '10.0.1']
        libs = [appcompat            : "com.android.support:appcompat-v7:$versions.supportLib",
               design               : "com.android.support:design:$versions.supportLib"]

        .... Other libraries and setup....

    }
}

rootProject/app/build.gradle

...
dependencies {
    // Support libraries
    compile libs.appcompat
    compile libs.design
    .....
}

I would like to access both on Java and build.gradle side the properties version and libs variables. How can I achieve this?

like image 565
Bugdr0id Avatar asked Aug 09 '17 09:08

Bugdr0id


1 Answers

You can do somenthing like this, but outside the buildscript block

ext {
    // Version
    supportVersion = '26.0.0'

    // Support Libraries dependencies
    supportDependencies = [
            design:            "com.android.support:design:${supportVersion}",
            recyclerView:      "com.android.support:recyclerview-v7:${supportVersion}",
            cardView:          "com.android.support:cardview-v7:${supportVersion}",
            appCompat:         "com.android.support:appcompat-v7:${supportVersion}",
            supportAnnotation: "com.android.support:support-annotations:${supportVersion}",
    ]

    firebaseVersion = '11.0.2';

    firebaseDependencies = [
            core:         "com.google.firebase:firebase-core:${firebaseVersion}",
            database:     "com.google.firebase:firebase-database:${firebaseVersion}",
            storage:      "com.google.firebase:firebase-storage:${firebaseVersion}",
            crash:        "com.google.firebase:firebase-crash:${firebaseVersion}",
            auth:         "com.google.firebase:firebase-auth:${firebaseVersion}",
            messaging:    "com.google.firebase:firebase-messaging:${firebaseVersion}",
            remoteConfig: "com.google.firebase:firebase-config:${firebaseVersion}",
            invites:      "com.google.firebase:firebase-invites:${firebaseVersion}",
            adMod:        "com.google.firebase:firebase-ads:${firebaseVersion}",
            appIndexing:  "com.google.android.gms:play-services-appindexing:${firebaseVersion}",
    ];
}


// Module build file
dependencies {
    // ...
    compile supportDependencies.appCompat
    compile supportDependencies.design
    compile firebaseDependencies.crash
}

In java you can't access the build.gradle files since they are used only at buildtime. Hovewer you can put same values in the BuildConfig file. In this file there are some prebuilt values like BuildConfig.VERSION_NAME or you can use some custom keys like:

android {
    ...
    defaultConfig {
        ...
        // defining the build date
        buildConfigField "long", "BUILD_DATE", System.currentTimeMillis() + "L"
        // define whether this build is a production build
        buildConfigField "boolean", "IS_PRODUCTION", "false"
        // note that to define a string you need to escape it
        buildConfigField "String", "API_KEY", "\"my_api_key\""
    }
}

The automatically-generated <package_name>.BuildConfig.java will contain the following fields based on the directive above:

public class BuildConfig {
    // ... other generated fields ...
    public static final long BUILD_DATE = 1469504547000L;
    public static final boolean IS_PRODUCTION = false;
    public static final String API_KEY = "my_api_key";
}
like image 108
Gabriele Mariotti Avatar answered Sep 27 '22 17:09

Gabriele Mariotti