欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析Android中build.gradle的實(shí)用技巧

 更新時(shí)間:2019年03月05日 15:19:38   作者:謝長意  
這篇文章主要介紹了淺析Android中build.gradle的實(shí)用技巧,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

1.替換符的使用

(1)在 app-android-defaultConfig (或者多渠道打包)下面可以這樣使用

android {
  defaultConfig {
  manifestPlaceholders = [
        //高德地圖key
        GDKEY: "123456789",
    ]
   }
}

(2)在 AndroidManifest.xml 文件的 application 標(biāo)簽下面這樣引用

<!-- 高德地圖 -->
    <meta-data
      android:name="com.amap.api.v2.apikey"
      android:value="${GDKEY}" />

2.打包設(shè)置appname(啟動(dòng)圖標(biāo)類似,res下面的都可以這樣使用)

android {
  defaultConfig {
    //在string.xml中不能出現(xiàn)app_name這個(gè)字段,否則生成報(bào)錯(cuò)
    resValue "string", "app_name", "app名稱"   
   }
}

3.生成BuildConfig.java字段

在build.gradle中

android {
  defaultConfig {
      //生成一個(gè)boolea類型的變量
      buildConfigField "boolean", "IS_TEST_URL", "true"
      //生成一個(gè)字符串變量
      buildConfigField "String", "APP_UPDATE_TIME", "\"${System.currentTimeMillis().toString()}\""
   }
}

在java代碼

public final class BuildConfig {
 // Fields from product flavor: 渠道名
 public static final String APP_UPDATE_TIME = "1551754973086";
 public static final boolean IS_TEST_URL = false;
}

4.多渠道打包(注意在defaultConfig下面添加flavorDimensions "versionCode")

android {
  compileSdkVersion 28
  defaultConfig {
    minSdkVersion 19
    targetSdkVersion 28
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    flavorDimensions "versionCode"
 
  productFlavors {
    name1 {
      applicationId "com.test.name"
      versionName "0.1.4"
      versionCode 5
      resValue "string", "app_name", "app名字"   
      buildConfigField "boolean", "IS_TEST_URL", "false"
      buildConfigField "String", "APP_UPDATE_TIME", "\"${System.currentTimeMillis().toString()}\""
    }  
  }

5.設(shè)置簽名

android{
 signingConfigs {
    release {
      keyAlias ''
      keyPassword ''
      storeFile file('./sign.jks')
      storePassword ''
      v2SigningEnabled false
    }
  }
  buildTypes {
    release {
      debuggable false
      minifyEnabled true
      shrinkResources true
      useProguard true
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      signingConfig signingConfigs.release
    }
    debug {
      debuggable true
      minifyEnabled false
      shrinkResources false
      useProguard false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      signingConfig signingConfigs.release
    }
  }
}

6.更改打包的apk名

android{
 android.applicationVariants.all { variant ->
    variant.outputs.all {
      Date nowTime = new Date()
      SimpleDateFormat time = new SimpleDateFormat("MM月dd日HH時(shí)mm分")
      outputFileName = "${variant.flavorName}(${variant.versionCode}_${variant.versionName})(${time.format(nowTime)}).apk"
    }
  }
}

7.引入第三方庫的時(shí)候,剔除某些不需要的包或者重復(fù)的包

1.直接在configuration中排除

configurations {
  compile.exclude module: 'commons'
  all*.exclude group: 'org.gradle.test.excludes', module: 'reports'
}

2.在具體的某個(gè)dependency中排除

dependencies {
  implementation("com.github.bumptech.glide:glide:4.8.0") {
    exclude module: 'appcompat-v7'
  }
}

PS:Android Studio開發(fā)Build.gradle小技巧

引用版本統(tǒng)一規(guī)范

Android開發(fā)存在著眾多版本的不同,比如compileSdkVersion、minSdkVersion、targetSdkVersion以及項(xiàng)目中依賴第三方庫的版本,不同的module及不同的開發(fā)人員都有不同的版本,所以需要一個(gè)統(tǒng)一版本規(guī)范的文件,現(xiàn)在我就來介紹一種方式。

在項(xiàng)目根目錄,也就是跟app同一目錄下的build.gradle文件,如下圖所示


在其最后添加如下groovy代碼。

ext {
  compileSdkVersion = 25
  buildToolsVersion = "25.0.0"
  minSdkVersion = 19
  targetSdkVersion = 19
 
  supportVersion = '25.3.1'
  rxjavaVersion = '1.1.8'
  rxandroidVersion = '1.2.1'
  glideVersion = '3.6.1'
  junitVersion = '4.12'
 
  deps = [
      appcompatv7  : "com.android.support:appcompat-v7:$supportVersion",
      supportv4   : "com.android.support:support-v4:$supportVersion",
      recyclerviewv7: "com.android.support:recyclerview-v7:$supportVersion",
      rxjava    : "io.reactivex:rxjava:$rxjavaVersion",
      rxandroid   : "io.reactivex:rxandroid:$rxandroidVersion",
      glide     : "com.github.bumptech.glide:glide:$glideVersion",
      junit     : "junit:junit:$junitVersion"
  ]
}

有了這個(gè)規(guī)范,那么我們在app下的build.gradle文件就可以這樣來引用了

android {
  compileSdkVersion rootProject.compileSdkVersion
  buildToolsVersion rootProject.buildToolsVersion
  defaultConfig {
    applicationId "com.ecarx.thememanager"
    minSdkVersion rootProject.minSdkVersion
    targetSdkVersion rootProject.targetSdkVersion
    versionCode 1
    versionName "1.0"
  }
  ...
}
 
dependencies {
  compile fileTree(include: ['*.jar'], dir: 'libs')
  compile deps.supportv4
  compile deps.appcompatv7
  compile deps.recyclerviewv7
  compile deps.rxjava
  compile deps.rxandroid
  compile deps.glide
 
  testCompile deps.junit
}

是不是一勞永逸了,今后修改版本只需要修根目錄下的build.gradle文件即可把所有依賴版本都修改

對資源進(jìn)行分包

我們可以對每個(gè)頁面的資源都進(jìn)行具體分類,不只是layout,還有drawable及value,是不是心動(dòng)了,趕緊照著如下配置試一試吧,別再讓資源文件們“混為一潭”了。

方法很簡單,配置我們的app文件夾下的build.gradle文件,比如我的

android {
  ...
  sourceSets {
    main {
      res.srcDirs =
          [
              'src/main/res',
              'src/main/res/layouts',
              'src/main/res/layouts/home',
              'src/main/res/layouts/hot_sale',
              'src/main/res/layouts/amuse',
              'src/main/res/layouts/delicacy',
              'src/main/res/layouts/food_management',
              'src/main/res/layouts/settings',
          ]
    }
  }
}

新建相關(guān)文件夾,配置完之后,sync project一下就成功了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論