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

对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,它指定了当前模块的详细 编译规则。
android {
    // 指定编译用的SDK版本号。比如30表示使用Android 11.0编译
    compileSdkVersion 30
    // 指定编译工具的版本号。这里的头两位数字必须与compileSdkVersion保持一致,具体的版本号可在sdk安装目录的“sdk\build-tools”下找到
    buildToolsVersion "30.0.3"
   
    defaultConfig {
        // 指定该模块的应用编号,也就是App的包名
        applicationId "com.example.chapter02"
        // 指定App适合运行的最小SDK版本号。比如19表示至少要在Android 4.4上运行
        minSdkVersion 19
        // 指定目标设备的SDK版本号。表示App最希望在哪个版本的Android上运行
        targetSdkVersion 30
        // 指定App的应用版本号
        versionCode 1
        // 指定App的应用版本名称
        versionName "1.0"
        
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-androidoptimize.txt'), 'proguard-rules.pro'
        }
    }
}

// 指定App编译的依赖信息
dependencies {
    // 指定引用jar包的路径
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // 指定编译Android的高版本支持库。如AppCompatActivity必须指定编译appcompat库
    //appcompat库各版本见
https://mvnrepository.com/artifact/androidx.appcompat/appcompat
    implementation 'androidx.appcompat:appcompat:1.2.0'
    // 指定单元测试编译用的junit版本号
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

注意

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

运行配置文件AndroidManifest.xml

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chapter02">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main2Activity"></activity>
        <!-- activity节点指定了该App拥有的活动页面信息,其中拥有
android.intent.action.MAIN的activity说明它是入口页面 -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</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节点内部还配置了以下的过滤信息:

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

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

猜你喜欢

转载自blog.csdn.net/danielxinhj/article/details/127736694
今日推荐