Android Studio中Gradle编译配置

编译流程就如下图所示:


The build process for a typical Android app module, as shown in figure 1, follows these general steps:

  1. The compilers convert your source code into DEX (Dalvik Executable) files, which include the bytecode that runs on Android devices, and everything else into compiled resources.
  2. The APK Packager combines the DEX files and compiled resources into a single APK. Before your app can be installed and deployed onto an Android device, however, the APK must be signed.
  3. The APK Packager signs your APK using either the debug or release keystore:
    1. If you are building a debug version of your app, that is, an app you intend only for testing and profiling, the packager signs your app with the debug keystore. Android Studio automatically configures new projects with a debug keystore.
    2. If you are building a release version of your app that you intend to release externally, the packager signs your app with the release keystore. To create a release keystore, read about signing your app in Android Studio.
  4. Before generating your final APK, the packager uses the zipalign tool to optimize your app to use less memory when running on a device.
At the end of the build process, you have either a debug APK or release APK of your app that you can use to deploy, test, or release to external users.



下面这幅图就是整个Android应用(不包含NDK部分)的构建编译框架详细流程说明(来源于官方):




自定义配置:
1、自定义编译type。可以定义编译debug和release版本
    buildTypes  {
        release 
{
            minifyEnabled 
true
            proguardFiles getDefaultProguardFile
( 'proguard-android.txt' ),   'proguard-rules.pro'
       
}

        debug 
{
            applicationIdSuffix 
".debug"
       
}

       
/**
         * The 'initWith' property allows you to copy configurations from other build types,
         * so you don't have to configure one from the beginning. You can then configure
         * just the settings you want to change. The following line initializes
         * 'jnidebug' using the debug build type, and changes only the
         * applicationIdSuffix and versionNameSuffix settings.
         */

        jnidebug 
{

           
// This copies the debuggable attribute and debug signing configurations.
            initWith debug

            applicationIdSuffix 
".jnidebug"
            jniDebuggable 
true
       
}
   
}

2、自定义编译渠道。多渠道打包
android  {
   
...
    defaultConfig 
{...}
    buildTypes 
{...}
    productFlavors 
{
        demo 
{
            applicationId 
"com.example.myapp.demo"
            versionName 
"1.0-demo"
       
}
        full 
{
            applicationId 
"com.example.myapp.full"
            versionName 
"1.0-full"
       
}
   
}
}
使用:
android  {
    ...
    defaultConfig  {...}
    buildTypes  {...}
    productFlavors  {
        xiaomi {
           manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
        }
        _360 {
          manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
       }
       baidu {
          manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
       }
       wandoujia {
          manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
       }
   }

   // 或者统一命名
   productFlavors {
       xiaomi {}
       _360 {}
       baidu {}
       wandoujia {}
  }  
  productFlavors.all { 
      flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] 
  }
}

3、多个Manifest合并时的冲突:遵循 Manifest Entries合并配置   https://developer.android.com/studio/build/manifest-merge.html



4、依赖 Dependencies:
     主要有三种依赖:compile project(":mylibrary")      //模块依赖
                                         compile 'com.android.support:appcompat-v7:23.4.0'      // 远程库依赖
                                         compile fileTree(dir: 'libs', include: ['*.jar'])      // 本地依赖
     全部依赖:
              1.Compile
                    compile是对所有的build type以及favlors都会参与编译并且打包到最终的apk文件中。
              2.Provided
                    Provided是对所有的build type以及favlors只在编译时使用,类似eclipse中的external-libs,只参与编译,不打包到最终apk。
             3.APK
                    只会打包到apk文件中,而不参与编译,所以不能再代码中直接调用jar中的类或方法,否则在编译时会报错
            4.Test compile
                    Test compile 仅仅是针对单元测试代码的编译编译以及最终打包测试apk时有效,而对正常的debug或者release apk包不起作用。
            5.Debug compile
                    Debug compile 仅仅针对debug模式的编译和最终的debug apk打包。
            6.Release compile
                    Release compile 仅仅针对Release 模式的编译和最终的Release apk打包。

5、 ProGuard混淆

6、 APK Splits, 分离APK
        主要用来编译出针对不同机型的apk,比如屏幕密度,比如CPU等,可以配置如下:
                  
        Property Description
      abi
ABI settings.

The list of ABI filters used for multi-apk.

    density
Density settings.

The list of Density filters used for multi-apk.

    language
Language settings.
languageFilters

The list of language filters used for multi-apk.


       比如密度density,如何hdpi、mdpi、xhdpi和xxhdpi中图片文件很多,打一个通用包大小为20.0M,其中所有图片资源大约占15M,那么单独打个xxhdpi的大小可能只有15M / 4 + (20M - 15M),相对来说减少了很多,参考blog: http://blog.chengyunfeng.com/?p=889 。如下:
android  {
 
...
  splits 
{

   
// Configures screen ABI split settings
    abi 
{

     
// Enable ABI APK splits
      enable 
true

     
// By default all ABIs are included, so use reset() and include to specify that we only
     
// want APKs for x86, armeabi-v7a, and mips

     
// Resets the list of ABIs that Gradle should create APKs for to none
      reset
()

     
// Specifies a list of ABIs that Gradle should create APKs for
      include 
"x86" ,   "armeabi-v7a" ,   "mips"

      // Specify that we do not want to also generate a universal APK that includes all ABIs
      universalApk  false       // 是否打通用包
    }
 
}
}






### 回答1: Gradle是一种基于Apache Ant和Apache Maven概念的自动化构建工具,用于Java项目的自动化构建、依赖管理和测试等。在Android StudioGradle被用来构建和管理Android项目。 Gradle配置包括: 1. 项目级别的Gradle配置:位于项目根目录下的build.gradle文件,用于配置整个项目的构建和依赖管理。 2. 模块级别的Gradle配置:位于每个模块的build.gradle文件,用于配置该模块的构建和依赖管理。 在Gradle配置,常见的配置项包括: 1. repositories:用于指定依赖库的仓库地址,可以是本地文件系统、远程Maven仓库或者其他自定义仓库。 2. dependencies:用于指定项目的依赖库,可以是本地文件系统、远程Maven仓库或者其他自定义仓库。 3. buildTypes:用于指定项目的构建类型,例如debug、release等。 4. productFlavors:用于指定项目的产品风味,例如free、paid等。 5. compileSdkVersion、buildToolsVersion、minSdkVersion、targetSdkVersion等:用于指定项目的编译版本、构建工具版本、最低支持版本和目标版本等。 以上是Android StudioGradle配置的基础内容,具体配置还需要根据项目需求进行调整。 ### 回答2: GradleAndroid Studio的一种构建工具,用于编译、打包、发布Android应用程序。它使用Groovy语言编写,基于Apache Maven和Apache Ant构建工具的优点,支持灵活的构建脚本并提供了丰富的插件,可以有效地管理依赖关系、自动化构建任务、自动导入库等。 在Android StudioGradle配置分为两部分:项目级别的Gradle配置和模块级别的Gradle配置。 项目级别的Gradle配置包括build.gradle文件和gradle-wrapper.properties文件。build.gradle文件是Gradle构建脚本的主文件,它定义了项目的构建方式、仓库地址、插件,以及需要构建的模块等。gradle-wrapper.properties文件是Gradle包装器的配置文件,它定义了Gradle版本号并指定了Gradle下载地址。 模块级别的Gradle配置包括build.gradle文件和proguard-rules.pro文件。build.gradle文件包括了该模块的构建配置,包括依赖库、运行时权限、签名方式,以及生成的APK路径等。proguard-rules.pro文件是混淆规则文件,它包括了混淆代码时需要忽略的类和方法等信息。 要配置Gradle,我们需要根据实际需要添加或修改相应的内容。例如,添加依赖库可以在build.gradle文件的dependencies节点添加相关依赖库信息,例如: implementation 'com.android.support:appcompat-v7:28.0.0' 如果需要修改混淆规则,可以在proguard-rules.pro文件添加相关规则,例如: -keep class com.example.app.** { *;} 总之,Gradle是一款功能强大的构建工具,它可以帮助开发者自动化构建任务,管理依赖关系等。对于Android开发者而言,掌握Gradle配置是非常重要的技能,可以提高开发效率,降低开发成本。 ### 回答3: Android Studio是一种支持Android应用程序开发的集成开发环境,而Gradle则是一种基于Apache Maven和Ant的开源构建自动化工具。它可以更轻松地以面向对象的方式来管理Java工程。在Android Studio,使用Gradle配置可以使项目更加方便,同时也具备了灵活性。 首先,我们需要了解一些Android StudioGradle配置的基本知识。可以在项目的根目录下找到build.gradle文件,这是Gradle构建脚本文件的主要文件之一。它包含了项目构建所需的所有配置信息,如编译版本号、依赖项等。在Gradle构建脚本,有两个重要的部分称为buildscript和dependencies。 buildscript: buildscript的配置项是为了配置Gradle编译环境。通常包含如下的内容: - repositories: Gradle插件库和第三方库的URL地址。 - dependencies: Gradle插件列表,所有的插件都需要在这个部分声明,基本上最常见的就是com.android.tools.build:gradle。 dependencies: dependencies部分包含项目依赖的所有库文件,例如支持库和第三方库。我们可以通过在dependencies块添加依赖项来设置库和版本: 例如: ``` dependencies { compile 'com.android.support:appcompat-v7:26.0.0-beta1' compile 'com.android.support.constraint:constraint-layout:1.0.2' } ``` 此外,我们还可以更改项目编译版本。可以在build.gradle文件修改编译版本信息,例如minSdkVersion,targetSdkVersion和compileSdkVersion。以下是一个示例: ``` android { compileSdkVersion 27 defaultConfig { minSdkVersion 19 targetSdkVersion 27 ... } buildTypes { release { ... } } } ``` 在配置Gradle时,我们还可以添加自定义任务。Gradle提供了一组系统任务,例如clean,assemble,build和check等。我们也可以使用Gradle插件以及自定义任务来自己指定相关任务。 总之,配置Gradle可以使Android Studio更加灵活,能够更好地管理、构建和部署项目。通过使用Gradle构建脚本,我们可以更方便地进行依赖项管理以及其他任务。同时,它还支持自定义插件和扩展,以满足我们的特定需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值