編輯:關於 Android 程式設計
最近在做一个供其他開發者使用的 SDK,因此必須輸出成 jar 包。Eclipse 有現成的「Export Java Project」功能,但 Android Studio 1.0 並未提供匯出 jar 的圖形介面,得靠 Gradle 自己寫任務。
在 module 的 build.gradle 中把 plugin 換成 library:
apply plugin: 'com.android.library'
這樣 build 之後,Android Studio 會在 module/build/intermediates/bundles/release/ 底下產生一支名為 classes.jar 的檔案(這個路徑在後續版本可能改變)。如果你的專案沒有用到 assets 等資源檔,直接把這支 jar 複製出去就能用了。
在 module 的 build.gradle 加上兩個任務:
task clearJar(type: Delete) {
delete 'build/outputs/yourname.jar'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename('classes.jar', 'yourname.jar')
}
makeJar.dependsOn(clearJar, build)
執行 gradle makeJar,就會在 build/outputs/ 下得到重新命名的 jar。先刪再複製,是為了避免舊檔殘留造成混淆。
如果你的 SDK 需要附帶 assets 資源,這部分就稍微 tricky。這裡用一個有點非主流的做法:利用檔案依賴來打包。
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
provided files('src/main/assets')
compile 'com.android.support:appcompat-v7:21.0.3'
}
關鍵是第三行。有兩個細節一定要注意:
minifyEnabled 設為 true,assets 才會被打進 jar。原因沒有深究,反正做 SDK 本來就需要混淆。
-libraryjars 'C:/Soft/android/adtsdk/platforms/android-19/android.jar'
-optimizations !code/simplification/arithmetic
-allowaccessmodification
-repackageclasses ''
-keepattributes *Annotation*
-dontpreverify
-dontwarn android.support.**
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.content.Context {
public void *(android.view.View);
public void *(android.view.MenuItem);
}
-keepclassmembers class * extends android.os.Parcelable {
static ** CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
這份設定主要在保留四大元件、自訂 View 的建構子、Parcelable 的 CREATOR、R 資源 id,以及加了 JavascriptInterface 註解的方法——這些都是被系統或反射呼叫、混淆後一定會出問題的部分。
Android Small 插件化框架剖析(下):AMS 如何啟動插件 Activity
上一篇我們追著 startActivity() 一路走到 AMS,把請求發了出去。本文接著分析 AMS 內部的處理過程,以及控制權如何回到 App 進程;然後拆解 Sma
Android Studio 完全入門指南:從安裝到多渠道打包
一、下載與安裝 先安裝 JDK 並配置好 JAVA_HOME 環境變量(這步純小白請自行搜索)。 再安裝 Android Studio,建議使用官方穩定版本。 下載時
手機酷狗與電腦酷狗不同步?用「網絡收藏」打通兩端
手機酷狗和電腦酷狗的歌單到底能不能互通?答案是肯定的。關鍵在於同一個帳號,加上「網絡收藏」這個功能。下面把操作步驟走一遍。 同步歌單怎麼做 登入酷狗後,在歌單裡選中想同
Android 為 EditText 搜尋框的 ListView 實作即時篩選顯示
「根據 EditText 搜尋框即時過濾 ListView 資料」這個需求聽起來挺有意思,牽涉到的知識點也不少,於是動手寫了一遍,整理成文,希望能給需要的朋友一點參考。先