Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build.gradle优化汇总 #38

Open
soapgu opened this issue May 11, 2021 · 0 comments
Open

build.gradle优化汇总 #38

soapgu opened this issue May 11, 2021 · 0 comments
Labels
安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented May 11, 2021

这期的内容相对零碎,正好凑一波合集来说明
更新的契机正好是Android Studio的版本升级到的4.2

  • 更新AGP到4.2

一开始以为AGP和AS的版本是同步的那,原来是巧合,下一个AGP版本将会是7.0,好吧真是任性
如果是用IDE升级, Gradle 版本升级到6.7.1,在gradle-wrapper.properties里面修改

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip

在project下的build.gradle修改如下

dependencies {
        classpath 'com.android.tools.build:gradle:4.2.0'

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

具体的版本差异可以看资料
Android Gradle 插件 4.2

  • 删库跑路的JCenter,远程仓库大搬家

升级完成后repositories->jcenter()出现了警告
图片
怎么肥事,跑到谷歌开发者上看到也是这样。
图片
马上跑路还不至于,JCenter 在 2022 年 2 月 1 日前仍会提供现有工件供用户下载。
但是新写的项目就顶一个警告,让我有代码强迫症怎么办。
房租没到期,我们也提前搬家吧
官方的建议是搬家到 Maven Central ,说干就干

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.0'

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

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

这里把jcenter()换成了mavenCentral()。
点击同步编译,en...失败了。
原来implementation 'com.licheedev:android-serialport:2.1.2'这句这个在远程仓库里面找不到了,发布在JCenter确没发布到Maven Central。还有不肯搬钉子户

  • 强迁android-serialport钉子户

虽然房租没到期,但是不能留后患,对钉子户我们不能手软

首先到mvnrepository.com去查户口
图片
找到的发布二进制文件,下载android-serialport-2.1.2.aar文件
有华虹jar二进制文件应用的经验,用aar也是差不多,用IDE比较简单
建一个libs的文件夹,把aar文件扔进去
Project Structure -> Dependencies -> ADD JAR/AAR Denpendency

dependencies {
    //implementation 'com.licheedev:android-serialport:2.1.2'
    implementation fileTree(dir: '../libs', include: ['*.aar', '*.jar'], exclude: [])
}

总算送走了钉子户。
通过这件事情总结下面经验

  1. 第三方库的使用特别是个人提供的一定要慎重,尽量使用机构组织发布的有名气的库
  2. 如果使用的第三方的库,一定要拿到源码,以备不时之需
  3. 对于这种基本不更新的小众第三方库,本地库要比远程仓库要好
  • 配置项目全局属性

需求,对于多模块项目。引用依赖的一致性和易维护性很重要
在root的build.gradle的ext区块定义全局属性

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        // Sdk and tools
        compileSdkVersion = 30
        minSdkVersion = 25
        targetSdkVersion = 30

        gradleVersion = '4.2.0'

        // App dependencies
        appCompatVersion = '1.1.0'
        materialVersion = '1.3.0'
        constraintLayoutVersion = '2.0.4'
        fragmentVersion = '1.3.3'
        hiltVersion = '2.35.1'
        rxAndroidVersion = '3.0.0'
        rxJavaVersion = '3.0.2'
        loggerVersion = '2.2.0'

        junitVersion = '4.13.2'
        testExtJunit = '1.1.2'
        espressoVersion = '3.3.0'
    }

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradleVersion"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hiltVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

把公共变量提取出来

plugins {
    id 'com.android.library'
    id 'dagger.hilt.android.plugin'
}

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion "30.0.3"

    defaultConfig {
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {

        dataBinding true

        // for view binding:
        // viewBinding true
    }
}

dependencies {

    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"
    implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
    annotationProcessor "com.google.dagger:hilt-android-compiler:$rootProject.hiltVersion"
    implementation "io.reactivex.rxjava3:rxandroid:$rootProject.rxAndroidVersion"
    implementation "io.reactivex.rxjava3:rxjava:$rootProject.rxJavaVersion"
    implementation project(path: ':core')
    testImplementation "junit:junit:$rootProject.junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$rootProject.testExtJunit"
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"
}

这是login模块的build.gradle。看起来清爽多了。只是编辑器的IDE还不够聪明,rootProject会自动补全,ext的变量认不出来,已经很好了,将就用着吧

参考资料

@soapgu soapgu added the 安卓 安卓 label May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant