Android生成 jar 和 arr ,以及 jar 和 arr 的使用方法。

          很多东西长时间不用,再用的时候又忘记了。今天工作中需要把自己开发的库做成远程依赖给客户调用。上午整理了一下,写了一篇博客,就是上篇 Android 上传自己的依赖库让别人使用(github),之后觉得干脆把生成jar 和 arr 的方法,和给别人用的方法也写一下,虽然很简单,但是最近不忙,写一下练练手。

1.首先我们要创建一个Project(这点就不演示了),然后我们创建Moudle,然后我们可以在创建的这个Android Library里面创建一个工具类。怎么创建一个Project和Model我这里就不介绍了,不会的看我上篇博客或者百度。

       先写一个工具类,然后自己测试一下工具类有没有问题。我的工具类里面写了两个方法,一个是只有java代码的,一个是用到了资源文件的。jar包里面只能是java代码,不能引用资源。arr包中既可以有java,也可以有引用资源。

      然后点击 Build -> Rebuild Project.

       (1)得到jar包。

            在lib的moudle中,build->intermediates->bundles->classes.jar,复制出来,修改名字,我这里改成myjar.jar.

        (2)得到arr包。

             在lib的moudle中,build->outputs->aar->*****.aar,复制出来,修改名字,我这里改成 tlibrary.aar.

2.使用jar包和arr包。

(1)使用jar包。

    将jar包放到自己项目中app moudel 中的lib下,在app moudel 的 build.gradle中dependencies中添加implementation files('libs/myjar.jar')

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation files('libs/myjar.jar')
}

然后就可以使用了。使用的时候两个方法,只有java代码的方法可以使用,有java代码和资源的不能使用。

(2)使用arr包。

      将arr包放到自己项目中app moudel 中的lib下,在app moudel 的 build.gradle中做如图修改。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.fyamei.app.myapplication"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compile(name:'tlibrary', ext:'aar')
}

然后就可以使用了。使用的时候工具类里的两个方法都可以使用。

 

但是如果在arr中封装的有.so,需要把.so文件放到jniLibs文件夹里面。

如果在arr有远程依赖,在使用的时候要在app moudel 的 build.gradle中把远程依赖加上。

其实觉得这样挺不方便的,不如直接把代码放在github上面,然后给一个远程依赖的连接,别人用的时候可以远程依赖。不会用的可以看我的上一篇博客 Android 上传自己的依赖库让别人使用(github)

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