Android元件化實現

範瑾瑜發表於2020-12-17

1.建立config.gradle檔案

在這裡插入圖片描述

ext{
    android = [
        compileSdkVersion: 30,
        buildToolsVersion: "30.0.2",
        applicationId: "com.wonder.moduletest",
        minSdkVersion: 16,
        targetSdkVersion: 30,
        versionCode: 1,
        versionName: "1.0"
    ]

    androidxDeps = [
            "appcompat": 'androidx.appcompat:appcompat:1.2.0',
            "material": 'com.google.android.material:material:1.2.1',
            "constraintlayout": 'androidx.constraintlayout:constraintlayout:2.0.4'
    ]

    commonDeps = [
            "arouter-api": 'com.alibaba:arouter-api:1.4.1',
            "glide": 'com.github.bumptech.glide:glide:4.11.0'
    ]

    annotationDeps = [
           "arouter-compiler": 'com.alibaba:arouter-compiler:1.5.1'
    ]

    androidxLibs = androidxDeps.values()
    commonLibs = commonDeps.values()
    annotationLibs = annotationDeps.values()
}

2.把config.gradle檔案匯入專案的build.gradle中

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: 'config.gradle'
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"

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

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

3.建立基礎層

3.1 建立library模組,模組名為baselibs

在這裡插入圖片描述

3.2 配置baselibs模組的build.gradle

plugins {
    id 'com.android.library'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 30
        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
    }
}

dependencies {
    api rootProject.ext.androidxLibs
    api rootProject.ext.commonLibs
    api rootProject.ext.annotationLibs
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

用api匯入的原因,是為了其他模組匯入該模組時,不要在單獨新增依賴

4.搭建元件層

4.1 建立login模組

在這裡插入圖片描述

4.2 在/src/main/檔案下建立一個module資料夾,並在該資料夾下建立表單檔案AndroidManifest.xml

在這裡插入圖片描述

4.3 配置login模組的build.gradle

1.因為整個專案只能有一個application,所以只能有一個applicationId和AndroidManifest.xml表單。
2.在gradle.properties中定義的變數,能在build.gradle中得到引用。因此在gradle.properties中定義了一個isModule變數

#ture時表示元件模式開發 false時表示整合模式開發
isModule = false
if(isModule.toBoolean()){
    apply plugin:'com.android.application'
}else{
    apply plugin:'com.android.library'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        javaCompileOptions{
            annotationProcessorOptions{
                arguments = [AROUTER_MODULE_NAME:project.getName()]
            }
        }
        if(isModule.toBoolean()){
            applicationId "com.wonder.login"
        }
        sourceSets{
            main{
                if(isModule.toBoolean()){
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                }else{
                    manifest.srcFile 'src/main/module/AndroidManifest.xml'
                }
            }
        }
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

dependencies {
    implementation project(":baselibs")
    annotationProcessor rootProject.ext.annotationLibs
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

5.搭建應用層

5.1 配置app模組的build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        javaCompileOptions{
            annotationProcessorOptions{
                arguments = [AROUTER_MODULE_NAME:project.getName()]
            }
        }
        applicationId "com.wonder.moduletest"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

dependencies {
    implementation project(":baselibs")
    annotationProcessor rootProject.ext.annotationLibs
    if(!isModule.toBoolean()){
        implementation project(":login")
        implementation project(":share")
        implementation project(":mine")
    }
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

相關文章