gpt4 book ai didi

java - Android - 读取类内的 build.gradle 属性

转载 作者:行者123 更新时间:2023-12-01 14:06:41 26 4
gpt4 key购买 nike

我想访问 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
...
dependencies {
// Support libraries
compile libs.appcompat
compile libs.design
.....
}

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

最佳答案

你可以这样做,但是 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";
}

关于java - Android - 读取类内的 build.gradle 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45586839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com