androidstudio-gradle基础篇

androidstudio中各种gradle配置解释说明

项目的Project的配置

buildscript { //设置脚本的运行环境
    repositories { //支持java 依赖库管理(maven),用于项目的依赖
        jcenter()
        mavenCentral()

        google()
    }
    dependencies { //依赖包的定义。支持maven/ivy,远程,本地库,也支持单文件,如果前面定义了repositories{}maven 库,使用maven的依赖的时候只需要按照用类似于com.android.tools.build:gradle:2.1,gradle 就会自动的往远程库下载相应的依赖,就是给studio制定gradle 版本
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'

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

allprojects {//全局设置(项目根目录的build.gradle)
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        google()
    }
}

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

ext {//设置全局参数
    sdk = 23
    buildTools = "26.0.2"
    minSdk = 15
    libraryVersion = "1.0.2"
    supportVersion = "25.1.0"
}

在MyApp/app/build.gradle里面使用参数

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
}

依赖管理

 仓库
 预设配置仓库
repositories {
    jcenter()
     google()
}
远程仓库
repositories {
    maven {
       maven { url "https://jitpack.io" }
    }
}
本地仓库

repositories {
    maven {
        url "../repo"
    }
}
   本地依赖
   项目文件依赖
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

项目的Module的配置

apply plugin: 'com.android.application' //这个代表当前modle是一个app而不是一个library
apply plugin: 'org.greenrobot.greendao'

android {
    signingConfigs { //签名配置
    
        debug { //debug版签名配置
            keyAlias 'mima' //key别名
            keyPassword 'mima' //key密码
            storeFile file('E:/workspace/xuexi/5xuexi.jks') //密钥文件路径
            storePassword 'mima' //密钥文件密码
            }
       release {//发布版签名配置
            keyAlias 'mima' 
            keyPassword 'mima' 
            storeFile file('E:/workspace/xuexi/5xuexi.jks') 
            storePassword 'mima' 
          }
        }
    }
    compileSdkVersion 23 //编译的SDK版本
    buildToolsVersion "26.0.2" //编译的Tools版本
    defaultConfig {//默认配置
        applicationId "com.sj.yinjiaoyun.xuexi" //应用程序的包名
        minSdkVersion 15 //支持的最低版本
        targetSdkVersion 23 //支持的目标版本
        versionCode 21 //版本号
        versionName "2.4" //版本名
        multiDexEnabled true //支持multidex 解决65536爆包问题
    }
     
         //java编译相关配置
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    greendao { //数据库
        schemaVersion 1
        targetGenDir 'src/main/java'
        daoPackage 'com.sj.yinjiaoyun.xuexi.greedao.gen'

    }
  
      //build编译app的时候的配置,一般这里混淆的使用
    buildTypes {
        debug {
            signingConfig signingConfigs.debugConfig
            // 显示Log
            buildConfigField "boolean", "LOG_DEBUG", "true"
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false
        }
        release {
             minifyEnabled true //混淆开启
            signingConfig signingConfigs.release//设置签名信息
            zipAlignEnabled true
            //是否混淆
            minifyEnabled true
            // 移除无用的resource文件,开启这个会报错,待解决
            //  shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }
      //编译的 lint 开关
    lintOptions {
        // set to true to turn off analysis progress reporting by lint
        quiet true
        // if true, stop the gradle build if errors are found
        abortOnError false
        // if true, only report errors
        ignoreWarnings true
        checkAllWarnings false
        checkReleaseBuilds false
        // lintConfig file("lint.xml")
    }
    //为所有的子项目设置一些通用配置
    subprojects {
       //配置一个新的gradle一样
    }
    //Eclipse 中迁移过来的代码都带这个设置,一般做指定目录
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            //so库的引用
            jniLibs.srcDirs = ['libs']
        }
        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')
        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //项目文件依赖--(本地依赖)编译lib目录下的.jar文件
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile project(':circlerefresh') //编译附加的项目
    compile files('libs/jcore-android_v1.1.0.jar') //引入对应的文件
    compile 'com.android.support:design:23.3.0' //编译来自support的开源库
   


}


project下的settings.gradle
项目所有的module需要在此声明

include ':app', ':logger', ':network-sdk'

gradle升级篇

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'


android {
    signingConfigs {
        netpower {
            keyAlias 'key0'
            keyPassword '123456'
            storeFile file('../netpower.jks')
            storePassword '123456'
        }
    }
    dexOptions {
//        声明是否预 dex 库依赖项以加快您的增量构建速度。
//        由于此功能可能减慢您的干净构建的速度,
//        您可能需要为持续性集成服务器停用此功能。
        preDexLibraries true
        //设置运行 dex-in-process 时要使用的最大线程数量
        maxProcessCount 8
        //设置 DEX 编译器的最大堆大小
        //javaMaxHeapSize "2048m"


    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
//    configurations.all {
//        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
//    }
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "com.example.fanyuanhua.netpower"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        flavorDimensions "versionCode"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }
    buildTypes {
        //1.在gradlew 配置服务器地址
        debug {
            buildConfigField "String", "hostUrl"," \"https://mindcloud.camoryapps.com/test\"" //服务器地址
            ext.enableCrashlytics = false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//            signingConfig signingConfigs.netpower
        }
        release {
            buildConfigField "String", "hostUrl", "\"https://mindcloud.camoryapps.com/OssService\"" //服务器地址
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.netpower
        }

        develop{
            //自定义包名
            applicationIdSuffix ".develop"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
    sourceSets{

    }

    //多渠道命令打包所有平台的
    productFlavors {
        demo {}
        googlePlay {}
        tencent {}
        qihu360 {}
        baidu {
            applicationIdSuffix '.dev'

        }
        huawei {}
        xiaomi {}
        oppo {}
        vivo {}
        samsung {}
        meizu {}


    }
    //过滤variant
    variantFilter{variant->
        def names = variant.flavors*.name
        if ( names.contains("demo")) {
            // Gradle ignores any variants that satisfy the conditions above.
            setIgnore(true)
        }
    }

    // 自定义输出配置
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            def formattedDate = new Date().format("yy_MM_dd", TimeZone.getTimeZone("UTC"))
            if (variant.buildType.name == 'release') {
                outputFileName = "NetPower_" + variant.productFlavors[0].name + "_${variant.versionName}.apk"
            } else if (variant.buildType.name == 'debug') {
                outputFileName = "NetPower_" + variant.productFlavors[0].name + "_${variant.buildType.name}" + "_${formattedDate}.apk"
            }
        }
    }



    productFlavors.all {
        flavor ->
            flavor.manifestPlaceholders = [
                    UMENG_CHANNEL_VALUE: name,
                    FABRIC_API_KEY     : "704c2ed990ae3edea47fffbf0aa1785dede6ab39",
                    APPFLYER_DEV_KEY   : "uWDevKNCFta9W2tPK3CMfP", //修改为当前应用的AppsFlyer开发ID
                    APPFLYER_CHANNEL   : name,]
    }
}



dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs'
    })
//    implementation('com.crashlytics.sdk.android:crashlytics:2.9.7@aar') {
//        transitive = true;
//    }

    implementation 'com.android.support:appcompat-v7:28.+'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.jakewharton:butterknife:8.5.1'

    implementation 'com.android.support:design:28.+'
    implementation 'com.android.support:support-vector-drawable:28.+'
    implementation 'com.android.support:support-v4:28.+'
    implementation 'com.android.support:recyclerview-v7:28.+'
    testImplementation 'junit:junit:4.12'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    implementation 'com.daasuu:EasingInterpolator:1.0.0'
    //dagger
    annotationProcessor "com.google.dagger:dagger-compiler:2.15"
    compileOnly 'org.glassfish:javax.annotation:10.0-b28'
    implementation 'com.google.dagger:dagger:2.15'
    implementation 'com.google.dagger:dagger-android:2.15'
    implementation 'com.google.dagger:dagger-android-support:2.15'
    annotationProcessor "com.google.dagger:dagger-android-processor:2.15"
    //retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
    //Rxjava
    implementation 'io.reactivex.rxjava2:rxjava:2.1.3'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    //logger
    implementation 'com.orhanobut:logger:2.1.1'
    //Gson
    implementation 'com.google.code.gson:gson:2.8.2'
    //leakcanary
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.2'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
//    implementation project(':appsflyer')
    implementation 'com.android.support:multidex:1.0.3'
    androidTestImplementation 'com.android.support:multidex:1.0.3'
    androidTestImplementation 'com.android.support:multidex-instrumentation:1.0.3'
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值