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'
}

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