gpt4 book ai didi

android - 如何在Gradle中声明变量并将其打印在另一个Gradle文件中

转载 作者:行者123 更新时间:2023-12-03 03:56:48 25 4
gpt4 key购买 nike

我是新手。我对如何在gradle中声明变量进行了一些研究,并在build.gradle中找到了一些东西。我的问题是如何声明在某些情况下其值已更改的变量,并访问该变量是我自己的custom.gradle的另一个gradle文件。

我不确定这是声明变量的正确方法。

defaultConfig {
buildConfigField "boolean", "INTERNET_CHECK", "true"
}

之后,我尝试在另一个custom.gradle中打印变量,它给出了一个错误。以下是我如何在custom.gradle中打印变量
println(INTERNET_CHECK)
println(BuildConfig.INTERNET_CHECK)

错误是:
 Could not get unknown property 'INTERNET_CHECK' for DefaultConfig_Decorated{................}

另一件事是,在某些条件块(例如 if/else)中,它不会在INTERNET_CHECK中分配其他值,并且不会打印出它给出 null的变量,也不会在另一个变量中分配值。

最佳答案

Vivek,我不明白您的错误。
我的观察

1.  buildConfigField "boolean", "SUPPORTS_THIRD_PARTY_SYSTEMS", "" + SUPPORTS_THIRD_PARTY_SYSTEMS

You need to define this variable SUPPORTS_THIRD_PARTY_SYSTEMS.

2. logger.error(BuildConfig.BUILD_TIME + " ")
It could be logger.error("BuildConfig.BUILD_TIME=" + BUILD_TIME)

3. One example on how to print data defined in defaultConfig {} block.
println defaultConfig.targetSdkVersion.mApiLevel

app / build.gradle以不同的风格使用signconfigs。
apply plugin: 'com.android.application'

android {
compileSdkVersion rootProject.compileSdkVersion
flavorDimensions "default"
lintOptions {
// ignore lint errors related with following issue ID
disable 'MissingTranslation', 'NotSibling', 'ExtraTranslation'
}
ext{
APP_LABEL = "@string/app_label"
APP_ICON = "@drawable/app_icon"

}
defaultConfig {
applicationId "com.ranjan.example"
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
buildToolsVersion = rootProject.buildToolsVersion
versionCode 1
versionName "1.0"

}
buildTypes {
debug {
signingConfig null
// this make the debug build take the flavor certificate instead of google debug one
}
release {
// Don't do code optimization, it is creating issue with NDK
shrinkResources false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
signingConfigs {
XYZ {
keyAlias 'asXYZ'
keyPassword 'ASXYZ02018#033'
storeFile file('keystore/xyz.keystore')
storePassword 'AXY02018#033'
}

//add another block for new sign config
}

productFlavors {
ABCD {
// application ID change is not working properly in middleware
applicationId "com.ranjan.abcd"

def ApplicationName = "com.ranjan.newApp"

//Use signconfig defined earlier
signingConfig signingConfigs.XYZ

manifestPlaceholders = [applicationLabel: APP_LABEL, applicationIcon: APP_ICON, applicationBanner: APP_BANNER]

.

buildConfigField("String", "APP_NAME", "\"${ApplicationName}\"")

}

//add other block for new flavor
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/aidl']
renderscript.srcDirs = ['src/main/rs']
jni.srcDirs = []
jniLibs.srcDirs = []
res.srcDirs = ['src/main/res']
assets.srcDirs = []
}
test{
java.srcDirs = ['test']
}
productFlavors.all {
flavor ->
if (flavor.name.startsWith("ABCD")) {

getProperty(flavor.name + "Debug").assets.srcDirs = ["src/main/assets"]
getProperty(flavor.name + "Release").assets.srcDirs = ["src/main/assets"]
getProperty(flavor.name + "Debug").jniLibs.srcDirs = ["src/main/jniLibs"]
getProperty(flavor.name + "Release").jniLibs.srcDirs = ["src/main/jniLibs"]
}
}
}
packagingOptions {
exclude 'error_prone/Annotations.gwt.xml'
exclude 'third_party/java_src/error_prone/project/annotations/Annotations.gwt.xml'
exclude 'third_party/java_src/error_prone/project/annotations/Google_internal.gwt.xml'
exclude 'jsr305_annotations/Jsr305_annotations.gwt.xml'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'junit:junit:4.12'
implementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation ('com.android.support.test:runner:1.0.2') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.2') {
exclude group: 'com.android.support', module: 'support-annotations'
}

}

关于android - 如何在Gradle中声明变量并将其打印在另一个Gradle文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56742354/

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