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       // 是否打通用包
    }
 
}
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值