Android-2-manifest和build.gradle两个关键文件

发布时间 2023-09-14 11:43:10作者: Coder-Wang

Androidmanifest.xml本质上就是用res中的文件配置项目的情况,像使用到的权限,app的名字,icon等等
build.gradle.kts本质上和maven没什么区别,主要是做两个事情,一是添加插件,二是添加库(检查有无这个库,没有的话就从网上下载)

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <!-- 这里的android是设置app的名字,就是在手机桌面上的名字-->
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication1"
        tools:targetApi="31">
        <!-- 这里的label设置的是活动的标题-->
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.gradle.kts

// 首先第一行应用了一个插件,
// 一般有两种值可选:com.android.application 表示这是一个应用程序模块,
// com.android.library表示这是一个库模块
// 应用程序模块和库模块的最大区别在于,一个是可以直接运行的,一个只能作为代码库依附于别的应用程序模块来运行
plugins {
    id("com.android.application")
}

android {
    namespace = "com.kcy.myapplication1"

    // 指定项目的编译版本
    compileSdk = 33

    defaultConfig {
        // 指定项目的包名
        applicationId = "com.kcy.myapplication1"
        // 指定项目最低兼容的Android系统版本
        minSdk = 28
        // targetSdkVersion指定的值表示你在该目标版本上已经做过了充分的测试,系统将会为你的应用程序启用一些最新的功能和特性
        targetSdk = 33
        // versionCode用于指定项目的版本号
        versionCode = 1
        // versionName用于指定项目的版本名
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    // 指定生成安装文件的相关配置
    buildTypes {
        release {
            // 是否对项目的代码进行混淆
            isMinifyEnabled = false
            // proguardFiles用于指定混淆时使用的规则文件,这里指定了两个文件,
            // 第一个proguard-android.txt是在Android SDK目录下的,里面是所有项目通用的混淆规则,
            // 第二个proguard-rules.pro是在当前项目的根目录下的,里面可以编写当前项目特有的混淆规则
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

dependencies {

    // Android Studio项目一共有3种依赖方式:本地依赖、库依赖和远程依赖
    // 添加一个库依赖  implementation project(':helper')
    // implementation fileTree是一个本地依赖声明,
    // 它表示将libs目录下所有.jar后缀的文件都添加到项目的构建路径当中
    //  implementation fileTree(dir: 'libs', include: ['*.jar'])

    // 远程依赖声明,androidx.appcompat:appcompat:1.2.0就是一个标准的远程依赖库格式,
    // 其中androidx.appcompat是域名部分,用于和其他公司的库做区分;
    // appcompat是组名称,用于和同一个公司中不同的库做区分;
    // 1.2.0是版本号,用于和同一个库不同的版本做区分
    // 加上这句声明后,Gradle在构建项目时会首先检查一下本地是否已经有这个库的缓存,
    // 如果没有的话则会去自动联网下载,然后再添加到项目的构建路径当中
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.8.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    // testImplementation 是用于声明测试用例库
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}