as的项目中的gradle配置常用了解及其与eclipse的AndroidManifest.xml对比

eclipse的开发中,配置文件,全部在AndroidManifest.xml文件中,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tvdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

几乎所有的配置,都在这个文件里:包名(package),版本号(versionCode),版本名称(versionName),要求编译的最低版本号(minSDKVersion),目标版本,也就是当前的编译运行版本(targetSDKVersion)在这个target版本上,项目是运行最好的,好多兼容性检查在这个版本都不会进行。
启动项,也在这个配置文件中。

在as的项目中,出现了两个build.gradle文件,当然,AndroidManifest.xml依然存在。与eclipse不同的是,有关项目的配置全部独立归在了这个build.gradle文件中,

build.gradle(app)配置

1,看看app主项目的gradle配置文件中的内容。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.freedev.demo1"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

可以看到包括项目的,当前版本编号(versionCode)、版本名称(versionName);
applicationId,就是包名。如果要修改包名,配置文件中要修改,AndroidManifest中也要修改包名。
编译期的:最低编译版本号(minSDKVersion)、编译版本号(targetVersion),打包版本号(buildToolsVersion),依赖(dependencies)等等。
代码混淆也是在这里进行配置的。
混淆

buildTypes {
        release {
            minifyEnabled false     //true表示混淆,false表示不混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

关于混淆,新写一篇文章。as中的代码混淆设置

项目依赖dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}
这句代码的意思是,导入libs下面所有的,以jar结尾的文件进行依赖。

2,再看看工作空间的build.gradle中的内容

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

这最重要的就是这个,com.android.tools.build:gradle:3.0.0表示的是当前项目所使用的构建的gradle的版本号。很多时候,版本号不一致,也会出现很多奇葩的报错。
3,最后看看,as的AndroidManifest.xml中的内容

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

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

可以看到,清爽了很多,只有启动activity,icon配置。

值得一提的是,把eclipse的AndroidManifest.xml直接放进as中,也是兼容的,不会报错,

比如eclipse项目转as,这个配置文件几乎不会发生什么变化,但是,

问题是,只有build.gradle(app)这个文件没有,或者,这个文件里没有对项目进行配置,

as才会去AndroidManifest.xml中去寻找那些配置。如果build.gradle(app)中已经配置了,

那么:AndroidManifest.xml中的配置就不会起作用。

独立有一个(build.gradle(app))文件,专门负责这些配置,当然优先从这个配置文件里找了,如果实在找不到,才从以往的AndroidManifest.xml中去寻找。

最后总结一下,在build.gradle(app)中经常进行的配置:
1,
1
2,
2
3,
3
4,
4
5,
5
6,
6

工作空间的build.gradle,一般来说,几乎是不用修改的,里面基本是两个内容:1,jcenter()就是maven中央仓库;2,当前项目使用的gradle的jar包的版本。

猜你喜欢

转载自blog.csdn.net/jakezhang1990/article/details/79334384