1

所以,我从 Github 克隆了这个项目,在浏览它的时候build.gradle,我发现了这个奇怪的配置,特别是对于targetSdkVersion. 现在,在我详细介绍它是什么之前,让我提一下该项目有两个模块 - app(主要的)和callrecord(封装通话录音功能)

这是build.gradle相同的文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion project.sdk

    defaultConfig {
        applicationId "com.aykuttasil.callrecorder"
        minSdkVersion project.minSdk
        targetSdkVersion project.sdk
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:appcompat-v7:$supportVersion"
    testCompile 'junit:junit:4.12'
    compile project(':callrecord')
}

你能看见它吗?

我不明白这条线compileSdkVersion project.sdk。这个项目“对象”也被其他几个地方引用过。

首先,为什么有人会使用这个属性?其次,我如何找出它是什么版本?

4

1 回答 1

4

为什么有人会使用这个属性?

  • 使用此属性的主要目的是在一个地方为 Android 项目模块配置所有变量
  • 所以在一个地方改变它会影响整个项目

我如何找出它是什么版本?

  • 它将在文件中gradle.propertiesbuild.gradle文件中可用

看看 Build.Gradle file of CallRecorder那个项目

在此处输入图像描述

样本

这是关于它的好文章Configure variables for all Android project modules in one place

gradle.properties

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX = true
android.enableJetifier = false

#gradle.properties
myTargetSdkVersion=27
myCompileSdkVersion=27
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

在此处输入图像描述

比用在你的Build.Gradle

android {
    compileSdkVersion project.myTargetSdkVersion.toInteger()
    defaultConfig {
        applicationId "com.example.nilesh.myapplication"
        minSdkVersion project.myMinSdkVersion.toInteger()
        targetSdkVersion project.myTargetSdkVersion.toInteger()
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}
于 2018-08-31T04:25:48.497 回答