gradle.properties的作用是什么?(并使用外部变量)

Far*_*deh 2 android android-gradle-plugin gradle.properties

我已经开发了一段时间的Android应用程序,但是意识到我仍然不知道该gradle.properties文件的用途。

我已经阅读了Gradle文档,其中介绍了您可以添加用于指定Java主目录或内存设置的配置。还有其他用途吗?

在这种情况下,我的主要参考通常是Google I / O开源应用程序,然后查看gradle.properties文件,我可以看到它的一种用途是存储依赖项版本变量,因此Android支持库依赖项的版本代码,例如,不需要每个都使用新版本的库进行更新,只需更新一个变量即可:

...

// Android support libraries.
compile "com.android.support:appcompat-v7:${android_support_lib_version}"
compile "com.android.support:cardview-v7:${android_support_lib_version}"
compile "com.android.support:design:${android_support_lib_version}"
compile "com.android.support:support-v13:${android_support_lib_version}"
compile "com.android.support:recyclerview-v7:${android_support_lib_version}"
compile "com.android.support:preference-v7:${android_support_lib_version}"

...
Run Code Online (Sandbox Code Playgroud)

相同的想法已用于Google Play服务。

但是,在我自己的一个Android项目中,我一直在做类似的事情-我将版本变量放在根build.gradle文件中,如下所示:

// Top-level build file where you can add configuration options
// common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.0.5-2'

    repositories {
        ...
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }

    ...
Run Code Online (Sandbox Code Playgroud)

然后,我一直在模块中使用它,build.gradle如下所示:

dependencies {

    ...

    // Kotlin standard library
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    ...
}
Run Code Online (Sandbox Code Playgroud)

所以我想我有几个问题:

  1. gradle.properties文件还有什么用?

  2. 使用外部变量作为依存关系版本中的依赖项版本gradle.properties(与在iosched中一样)与在根目录中使用外部变量build.gradle(正如我一直在做的)有什么区别?

    • 如果有的话,哪种方法更合适?
    • 以一种特定的方式进行操作是否有优点/缺点?

Mat*_*aga 5

我在(内部app/build.gradle)使用它:

signingConfigs {
    release {
        keyAlias RELEASE_KEY_ALIAS
        keyPassword RELEASE_KEY_PASSWORD
        storeFile file(RELEASE_STORE_FILE)
        storePassword RELEASE_STORE_PASSWORD
    }
}

productFlavors {
    ....
    prod {
        applicationIdSuffix ".prod"
        buildConfigField "String", "BASE_URL", BASE_URL_PROD
    }
    ....
}

buildTypes.each {
    it.buildConfigField "Double", "CONTACT_MAP_LATITUDE", CONTACT_MAP_LATITUDE
    it.buildConfigField "Double", "CONTACT_MAP_LONGITUDE", CONTACT_MAP_LONGITUDE
    it.resValue "string", "google_maps_api_key", GOOGLE_MAPS_API_KEY
}
Run Code Online (Sandbox Code Playgroud)

RELEASE_KEY_ALIASRELEASE_KEY_PASSWORDRELEASE_STORE_FILERELEASE_STORE_PASSWORDBASE_URL_PRODCONTACT_MAP_LATITUDECONTACT_MAP_LONGITUDEGOOGLE_MAPS_API_KEY所有的都在里面gradle.properties,并没有该文件推到混帐

例:

gradle.properties: BASE_URL_PROD = "http://something.com/api/"

build.gradle: buildConfigField "String", "BASE_URL", BASE_URL_PROD

Java文件: BuildConfig.BASE_URL

编辑:此外,您还可以在此处找到服务器应用程序的示例(春季):https : //melorriaga.wordpress.com/2016/08/06/gradle-dont-store-api-keys-and-db-information-in -versioned-files /