aar 與 jar
jar 是 Java的打包檔, 但無法包含 res 裏的資源檔, 這真的會讓人很想死. 因為如果不能包含 res裏的檔案, 那麼 layout裏的設定全都要用動態程式碼的方式重新撰寫, 是這非常痛苦的.
aar就是解救這問題的良方, 設定及作法如同上一章的Export to jar, 唯一不同的, 就是如下專案的gradle的幾個設定而以.
輸出路徑
aar的輸出路徑如下
build/outputs/aar/app-release.aar
aar gradle 設定
//刪除舊有的jar檔 task deleteOldJar(type: Delete) { delete 'release/mahalsdk-1.3.0.jar' } task exportRoot(type: Copy){ from('build/intermediates/intermediate-jars/release/') into ('release/') include('classes.jar') rename('classes.jar', 'mahalsdk-1.3.0.jar') from('build/outputs/aar') into ('release/') include('app-release.aar') rename('app-release.aar', 'mahalsdk-1.3.0.aar') } exportRoot.dependsOn(deleteOldJar, build)
專案應用設定
需於專案gradle 新增如下藍色部份
repositories { flatDir { dirs 'libs' } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0' 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/mahalsdk-1.3.0.jar') implementation (name:'mahalsdk-1.3.0', ext:'aar') }