Android Studio 自定義Module

自定義library Module
1.File ->New-> New Module,選擇 Android Library,自定義module名稱,eg:myview
2.在當前 app 的 build.gradle 中 dependencies{} 中添加 implementation project(':myview')

Tips: :myview 中的 : 代表的與 app 同級目錄下的 Module

導入已經存在的library Module
1.File ->New-> Import Module,導入對應的module即可
在這裏插入圖片描述

卸載或者加載module
選中已經加載的module,右鍵->Load or Unload Modules
在這裏插入圖片描述
如果新建或者引入modules成功之後我們可以在工程最頂層目錄發現該modules
在這裏插入圖片描述
編譯生成*.aar文件(aar與jar的區別
在這裏插入圖片描述

刪除module
也許你覺得可以直接進入到工程目錄,刪除對應的module即可,但是這樣的話並沒有刪除當前工程裏面關於該module配置,編譯會報錯。正確方法如下:
File->Project Struture,選中Modules標籤,選中需要刪除的Module
在這裏插入圖片描述
此時關於該module的文件還是沒有刪除的,此時我們在通過右鍵->Delete即可(未卸載是沒有Delete選項的)。

也可以通過右鍵操作,1.卸載 2.Remove Module 3.Delete

引入並使用Module
如果當前Module工程已經導入到當前工程

在當前 app 的 build.gradle 中 dependencies{} 中添加 implementation project(':myview')

直接引用*.aar文件
方法一:

1.把當前文件拷貝到 libs目錄
2.在當前 app 的 build.gradle 中 dependencies{} 中添加 implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])

方法二:

1.把當前文件拷貝到 libs目錄
2.在當前 app 的 build.gradle 中增加repositories { flatDir{ dirs'libs'} }
3.在當前 app 的 build.gradle 中 dependencies{} 中添加implementation (name:"mylib-debug",ext:'aar')

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.qwe"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

}

//repositories {
//    flatDir{
//        dirs'libs'}
//}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    implementation 'org.greenrobot:eventbus:3.1.1'
    //implementation (name:"mylib-debug",ext:'aar')
    //implementation project(":mylib")
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章