Android - 在类

时间:2017-08-09 09:33:07

标签: java android android-gradle build.gradle

我想访问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 /应用/的build.gradle

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

我想在Java和build.gradle端访问属性 版本 libs 变量。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

使用gradle.properties文件在一个地方定义您的值,然后您可以在gradle项目配置中使用此值。

然后为了能够在Java android代码中访问thoses值,使用' buildConfigField'定义

gradle.properties

values=value1,value2

的build.gradle

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

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

在java文件中

BuildConfig.VALUES_ARRAY

答案 1 :(得分:3)

你可以像这样做,但之外 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
}

在java中,您无法访问build.gradle文件,因为它们仅在构建时使用。您可以在BuildConfig文件中添加相同的值。在此文件中有一些预建值,如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\""
    }
}

自动生成的<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";
}