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)

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