• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

(Android开发)编译配置文件build.gradle和运行配置文件AndroidManifest.xml的解释

武飞扬头像
丹牛Daniel
帮助1

Android开发中项目工程里的编译配置文件build.gradle和运行配置文件AndroidManifest.xml的解释进行一个详细的解释。

目录

编译配置文件build.gradle

注意

运行配置文件AndroidManifest.xml


编译配置文件build.gradle

新创建的App项目默认有两个build.gradle,一个是Project项目级别的build.gradle;另一个是Module模块级别的build.gradle。

  • 项目级别的build.gradle指定了当前项目的总体编译规则,打开该文件在buildscript下面找到repositories和dependencies两个节点,其中repositories节点用于设置Android Studio插件的网络仓库地址,而dependencies节点用于设置gradle插件的版本号。由于官方的谷歌仓库位于国外,下载速度相对较慢,因此可在repositories节点添加阿里云的仓库地址,方便国内开发者下载相关插件。修改之后 的buildscript节点内容如下所示:
  • 学新通
  • 模块级别的build.gradle对应于具体模块,每个模块都有自己的build.gradle,它指定了当前模块的详细 编译规则。
  1.  
    android {
  2.  
    // 指定编译用的SDK版本号。比如30表示使用Android 11.0编译
  3.  
    compileSdkVersion 30
  4.  
    // 指定编译工具的版本号。这里的头两位数字必须与compileSdkVersion保持一致,具体的版本号可在sdk安装目录的“sdk\build-tools”下找到
  5.  
    buildToolsVersion "30.0.3"
  6.  
     
  7.  
    defaultConfig {
  8.  
    // 指定该模块的应用编号,也就是App的包名
  9.  
    applicationId "com.example.chapter02"
  10.  
    // 指定App适合运行的最小SDK版本号。比如19表示至少要在Android 4.4上运行
  11.  
    minSdkVersion 19
  12.  
    // 指定目标设备的SDK版本号。表示App最希望在哪个版本的Android上运行
  13.  
    targetSdkVersion 30
  14.  
    // 指定App的应用版本号
  15.  
    versionCode 1
  16.  
    // 指定App的应用版本名称
  17.  
    versionName "1.0"
  18.  
     
  19.  
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  20.  
    }
  21.  
    buildTypes {
  22.  
    release {
  23.  
    minifyEnabled false
  24.  
    proguardFiles getDefaultProguardFile('proguard-androidoptimize.txt'), 'proguard-rules.pro'
  25.  
    }
  26.  
    }
  27.  
    }
  28.  
     
  29.  
    // 指定App编译的依赖信息
  30.  
    dependencies {
  31.  
    // 指定引用jar包的路径
  32.  
    implementation fileTree(dir: 'libs', include: ['*.jar'])
  33.  
    // 指定编译Android的高版本支持库。如AppCompatActivity必须指定编译appcompat库
  34.  
    //appcompat库各版本见
  35.  
    https://mvnrepository.com/artifact/androidx.appcompat/appcompat
  36.  
    implementation 'androidx.appcompat:appcompat:1.2.0'
  37.  
    // 指定单元测试编译用的junit版本号
  38.  
    testImplementation 'junit:junit:4.13'
  39.  
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
  40.  
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
  41.  
    }
学新通

注意

为啥这两种编译配置文件的扩展名都是Gradle呢?这是因为它们采用了Gradle工具完成编译构建操作。 Gradle工具的版本配置在gradle\wrapper\gradle-wrapper.properties,也可以依次选择菜单 File→Project Structure→Project,在弹出的设置页面中修改Gradle Version。注意每个版本的Android Studio都有对应的Gradle版本,只有二者的版本正确对应,App工程才能成功编译。

运行配置文件AndroidManifest.xml

AndroidManifest.xml指定了App的运行配置信息,它是一个XML描述文件,初始内容如下所示:

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    package="com.example.chapter02">
  4.  
    <application
  5.  
    android:allowBackup="true"
  6.  
    android:icon="@mipmap/ic_launcher"
  7.  
    android:label="@string/app_name"
  8.  
    android:roundIcon="@mipmap/ic_launcher_round"
  9.  
    android:supportsRtl="true"
  10.  
    android:theme="@style/AppTheme">
  11.  
    <activity android:name=".Main2Activity"></activity>
  12.  
    <!-- activity节点指定了该App拥有的活动页面信息,其中拥有
  13.  
    android.intent.action.MAIN的activity说明它是入口页面 -->
  14.  
    <activity android:name=".MainActivity">
  15.  
    <intent-filter>
  16.  
    <action android:name="android.intent.action.MAIN" />
  17.  
    <category android:name="android.intent.category.LAUNCHER" />
  18.  
    </intent-filter>
  19.  
    </activity>
  20.  
    </application>
  21.  
    </manifest>
学新通

可见AndroidManifest.xml的根节点为manifest,它的package属性指定了该App的包名。manifest下 面有个application节点,它的各属性说明如下:

android:allowBackup,是否允许应用备份。允许用户备份系统应用和第三方应用的apk安装包和 应用数据,以便在刷机或者数据丢失后恢复应用,用户即可通过adb backup和adb restore来进行 对应用数据的备份和恢复。为true表示允许,为false则表示不允许。

  • android:icon,指定App在手机屏幕上显示的图标。
  • android:label,指定App在手机屏幕上显示的名称。
  • android:roundIcon,指定App的圆角图标。
  • android:supportsRtl,是否支持阿拉伯语/波斯语这种从右往左的文字排列顺序。为true表示支 持,为false则表示不支持。
  • android:theme,指定App的显示风格。

注意到application下面还有个activity节点,它是活动页面的注册声明,只有在AndroidManifest.xml中 正确配置了activity节点,才能在运行时访问对应的活动页面。初始配置的MainActivity正是App的默认 主页,之所以说该页面是App主页,是因为它的activity节点内部还配置了以下的过滤信息:

  1.  
    <intent-filter>
  2.  
    <action android:name="android.intent.action.MAIN" />
  3.  
    <category android:name="android.intent.category.LAUNCHER" />
  4.  
    </intent-filter>

其中action节点设置的android.intent.action.MAIN表示该页面是App的入口页面,启动App时会最先打 开该页面。而category节点设置的android.intent.category.LAUNCHER决定了是否在手机屏幕上显示 App图标,如果同时有两个activity节点内部都设置了android.intent.category.LAUNCHER,那么桌面就 会显示两个App图标。以上的两种节点规则可能一开始不太好理解,读者只需记住默认主页必须同时配 置这两种过滤规则即可。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgagccf
系列文章
更多 icon
同类精品
更多 icon
继续加载