dxzmpk

endless hard working

0%

Android Studio中3个和构建相关的组件

android studio中3个和构建相关的组件

  1. Gradle程序,这是和安卓独立的gradle系统,现在的版本号为6.5;

    在项目中, gradle存储在gradle\wrapper\gradle-wrapper.properties文件中,以下载链接的方式存在。例如:image-20201126135147962

    在构建中往往出现gradle下载不完全的问题,这时就会报错。打开C:\Users\Administrator\.gradle\wrapper\dists文件夹,就能看到不同版本的gradle文件。

    image-20201126135349366

    我们可以直接到gradle-wrapper.properties指定的url下载压缩包并放到C:\Users\Administrator\.gradle\wrapper\dists\gradle-5.6.4-bin\bxirm19lnfz6nurbatndyydux类似的位置中。在启动构建时,android studio会自动解压并运行。

  2. android sdk build tools.这是安卓sdk中自带的构建工具。我们可以打开image-20201126140235517这个图标,选择SDK tools栏看到image-20201126140333350

    即已经安卓了相应的SDK build tools工具。在项目中的image-20201126140458210文件指定了需要的版本。image-20201126140536176如这里是29.0.2。打开安卓sdk build-tools目录,如C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools就可以看到工具的所有版本。当出现下载问题时,可以到相应网站下载需要的工具版本,解压后重命名放到sdk build-tools目录中,android studio会自动检测并使用。

  3. gradle plugin. 除了以上两种,安卓还提供了一个构建插件。其版本号规定在项目文件image-20201126141054278中。即对于全局项目,使用的gradle插件是相同的。在打开android studio的时候,往往会提示我们更新gradle插件版本,其实就是指的它。一般情况下,gradle插件并不会导致太大的问题。

gradle plugin 和 Gradle程序的兼容问题

下表列出了每个版本的Android Gradle插件所需的Gradle版本。 为了获得最佳性能,建议使用Gradle和插件的最新版本。

Plugin version Required Gradle version
1.0.0 - 1.1.3 2.2.1 - 2.3
1.2.0 - 1.3.1 2.2.1 - 2.9
1.5.0 2.2.1 - 2.13
2.0.0 - 2.1.2 2.10 - 2.13
2.1.3 - 2.2.3 2.14.1+
2.3.0+ 3.3+
3.0.0+ 4.1+
3.1.0+ 4.4+
3.2.0 - 3.2.1 4.6+
3.3.0 - 3.3.3 4.10.1+
3.4.0 - 3.4.3 5.1.1+
3.5.0 - 3.5.4 5.4.1+
3.6.0 - 3.6.4 5.6.4+
4.0.0+ 6.1.1+
4.1.0+ 6.5+

安卓程序的构建

文件结构

img

安卓项目主要分为三个级别:

  1. Project级别:项目包含的所有文件
  2. Module级别:例如app等模块,一个项目可以有多个模块。
  3. 代码模块,main文件夹下的所有文件。

build.gradle文件结构

主要包含两个文件:

  1. 全局构建配置文件build.gradle(Project)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    buildscript {
    repositories {
    jcenter()
    maven {
    url 'https://maven.google.com/'
    name 'Google'
    }
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:3.6.1'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
    }

    allprojects {
    repositories {
    jcenter()
    maven {
    url 'https://maven.google.com/'
    name 'Google'
    }
    }
    }

    task clean(type: Delete) {
    delete rootProject.buildDir
    }

    位于根项目目录中的全局build.gradle文件定义了适用于项目中所有模块的构建配置。

    默认情况下,buildscript块定义项目中所有模块通用的Gradle存储库和依赖项。

    allprojects块用来配置存储库和所有模块共用的依赖,例如第三方插件和库,默认情况下,这里只包括jcenter和maven存储库。

    dependencies配置了gradle构建项目需要的依赖,关于版本的问题前文已经提到过了。

  2. 模块配置文件 build.gradle(Module)

    位于模块的根目录中,主要用于配置其所在的特定模块。我们可以通过配置这些构建设置来提供自定义打包选项,例如其他构建类型和产品类型,并覆盖manifest或全局build.gradle文件中的设置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    /**
    * The first line in the build configuration applies the Android plugin for
    * Gradle to this build and makes the android block available to specify
    * Android-specific build options.
    */

    apply plugin: 'com.android.application'

    /**
    * The android block is where you configure all your Android-specific
    * build options.
    */

    android {

    /**
    * compileSdkVersion specifies the Android API level Gradle should use to
    * compile your app. This means your app can use the API features included in
    * this API level and lower.
    */

    compileSdkVersion 28

    /**
    * buildToolsVersion specifies the version of the SDK build tools, command-line
    * utilities, and compiler that Gradle should use to build your app. You need to
    * download the build tools using the SDK Manager.
    *
    * This property is optional because the plugin uses a recommended version of
    * the build tools by default.
    */

    buildToolsVersion "29.0.2"

    /**
    * The defaultConfig block encapsulates default settings and entries for all
    * build variants, and can override some attributes in main/AndroidManifest.xml
    * dynamically from the build system. You can configure product flavors to override
    * these values for different versions of your app.
    */

    defaultConfig {

    /**
    * applicationId uniquely identifies the package for publishing.
    * However, your source code should still reference the package name
    * defined by the package attribute in the main/AndroidManifest.xml file.
    */

    applicationId 'com.example.myapp'

    // Defines the minimum API level required to run the app.
    minSdkVersion 15

    // Specifies the API level used to test the app.
    targetSdkVersion 28

    // Defines the version number of your app.
    versionCode 1

    // Defines a user-friendly version name for your app.
    versionName "1.0"
    }

    /**
    * The buildTypes block is where you can configure multiple build types.
    * By default, the build system defines two build types: debug and release. The
    * debug build type is not explicitly shown in the default build configuration,
    * but it includes debugging tools and is signed with the debug key. The release
    * build type applies Proguard settings and is not signed by default.
    */

    buildTypes {

    /**
    * By default, Android Studio configures the release build type to enable code
    * shrinking, using minifyEnabled, and specifies the default Proguard rules file.
    */

    release {
    minifyEnabled true // Enables code shrinking for the release build type.
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }

    /**
    * The productFlavors block is where you can configure multiple product flavors.
    * This allows you to create different versions of your app that can
    * override the defaultConfig block with their own settings. Product flavors
    * are optional, and the build system does not create them by default.
    *
    * This example creates a free and paid product flavor. Each product flavor
    * then specifies its own application ID, so that they can exist on the Google
    * Play Store, or an Android device, simultaneously.
    *
    * If you declare product flavors, you must also declare flavor dimensions
    * and assign each flavor to a flavor dimension.
    */

    flavorDimensions "tier"
    productFlavors {
    free {
    dimension "tier"
    applicationId 'com.example.myapp.free'
    }

    paid {
    dimension "tier"
    applicationId 'com.example.myapp.paid'
    }
    }

    /**
    * The splits block is where you can configure different APK builds that
    * each contain only code and resources for a supported screen density or
    * ABI. You'll also need to configure your build so that each APK has a
    * different versionCode.
    */

    splits {
    // Settings to build multiple APKs based on screen density.
    density {

    // Enable or disable building multiple APKs.
    enable false

    // Exclude these densities when building multiple APKs.
    exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
    }
    }
    }

    /**
    * The dependencies block in the module-level build configuration file
    * specifies dependencies required to build only the module itself.
    * To learn more, go to Add build dependencies.
    */

    dependencies {
    implementation project(":lib")
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    }

参考文献

https://developer.android.com/studio/build

https://developer.android.com/codelabs/android-training-hello-world?index=..%2F..%2Fandroid-training#0