简体   繁体   English

Android-阅读类内部的build.gradle属性

[英]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. 我希望可以访问build.gradle属性,以便可以自动执行我的应用程序中的某些流程。

This is the structure i have at the moment: 这是我目前的结构:

rootProject/build.gradle 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 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. 我想同时在Java和build.gradle上访问属性版本libs变量。 How can I achieve this? 我该如何实现?

Use gradle.properties file to define your values in one only place, then you can use this values in your gradle project config. 使用gradle.properties文件仅在一个位置定义值,然后可以在gradle项目配置中使用此值。

Then to be able to access thoses values in Java android code use 'buildConfigField' definition. 然后,要能够在Java android代码中访问这些值,请使用“ buildConfigField”定义。

gradle.properties gradle.properties

values=value1,value2

build.gradle build.gradle

def String[] valuesArray = values.split(',')

android {
    defaultConfig {
        buildConfigField "String[]", "VALUES_ARRAY", "{" +
                "\"" + valuesArray[0] + "\"," +
                "\"" + valuesArray[1] + "\"" +
                "}"
    }
}

in java file 在java文件中

BuildConfig.VALUES_ARRAY

You can do somenthing like this, but outside the buildscript block 您可以执行类似操作,但是在buildscript之外

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. 在Java中,您无法访问build.gradle文件,因为它们仅在构建时使用。 Hovewer you can put same values in the BuildConfig file. 但是,您可以将相同的值放在BuildConfig文件中。 In this file there are some prebuilt values like BuildConfig.VERSION_NAME or you can use some custom keys like: 在此文件中,有一些预先构建的值,例如BuildConfig.VERSION_NAME或者您可以使用一些自定义键,例如:

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: 自动生成的<package_name>.BuildConfig.java将基于上述指令包含以下字段:

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM