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

时间:2019-06-24 18:53:04

标签: android gradle android-gradle

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

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

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>

	<script>
		if(true) // change to => if (document.location.pathname === '/win')
			document.write( '<meta name="mrf-advertisement" content="" />' );
	</script>
</head>
<body>
	<h1>Hello world!</h1>
</body>
</html>

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

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

错误是:

println(INTERNET_CHECK)
println(BuildConfig.INTERNET_CHECK)

另一件事是,它不会在诸如 Could not get unknown property 'INTERNET_CHECK' for DefaultConfig_Decorated{................} 之类的某些条件块中在INTERNET_CHECK中分配另一个值,并打印它给出的if/else变量,也不会在另一个变量中分配值。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

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以不同风格使用signconfig。

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'
    }

}