為什么獨立
- Android N 使用了 Ninja 編譯系統(tǒng),旨在加快編譯速度,但是對內(nèi)存要求也變高,編譯過程中會占用大量內(nèi)存,這就導致在編譯服務(wù)器上,會出現(xiàn)多人同時編譯搶占內(nèi)存資源的情況,這種情況下單模塊的編譯時間可能從幾分鐘增至半小時以上。
- UI 任務(wù)增加,UI 的微調(diào)需要多次運行到真機上看效果。
- 優(yōu)點:加快編譯速度;獨立于項目,便于管理;最重要的是可以在 AS 上編譯后,我們就可以運用 AS 的各種強大的功能(如 Lint、xml預覽)及插件(如 FindBugs)。
Problems & Solutions
-
P:原生 SDK 與平臺 frameworks 代碼差異
S:制作含平臺代碼的 SDK, 方法:
分別用解壓工具打開
out\target\common\obj\JAVA_LIBRARIES\framework_intermediates\classes.jar(左側(cè))
和
sdk\platforms\android-23\android.jar(右側(cè))
然后將 左側(cè) 所有內(nèi)容 復制黏貼 到 右側(cè),然后保存即可
-
P:資源(Resources )重復
S:根據(jù) AS 的 Build 面板中提示的錯誤信息,逐條刪除重復資源(目前沒有找到更快捷的方式)
-
P:依賴的 .so 文件缺失
S:將 Android.mk 中 LOCAL_REQUIRED_MODULES 依賴的 .so 放到應用 lib 對應的 ABI 目錄下
-
P:遠程依賴的版本選擇
S:根據(jù) AS 的 Build 面板中提示的錯誤信息,找到缺失類的包名,然后在 https://search.maven.org/ 上搜索,找到對應版本,在 dependencies 中添加即可,例:
dependencies {
compile 'com.adobe.xmp:xmpcore:5.1.2'
}注意:compile(api)、provided(compileOnly)、implementation 的使用選擇 !依賴命令解釋
-
P:無法引用 /system/lib/ 下的 .so
S:將 /system/lib/ 下無法引用的 .so 通過 adb pull 出來,然后放到應用 lib 對應的 ABI 目錄下(目前沒有找到其他解決方案)
-
P:為什么將 frameworks\ex\camera2\ 目錄作為應用 srcFolder 參與編譯
S: Android.mk 中 LOCAL_STATIC_JAVA_LIBRARIES += android-ex-camera2-portability 對此目錄代碼有依賴,且便于應用獨立后代碼修改
-
P:獨立后應用體積增加
S: build.gradle 配置
defaultConfig { resConfigs 'hdpi'// 只打包 hdpi 資源 ndk{ abiFilters 'armeabi-v7a'// 通過 “減少其他 CPU 類型支持的 SO 庫” 來減少 APK 的體積 } } buildTypes { release { minifyEnabled true// 是否進行混淆 shrinkResources true// 去除無用資源 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'// 混淆文件的位置 } }
如何獨立
主要通過修改 build.gradle 配置
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
allprojects {
repositories {
jcenter()
}
}
// app config
apply plugin: 'com.android.application'
android {
def compileSdkVersionInt = 26
compileSdkVersion = compileSdkVersionInt
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.android.camera2"
minSdkVersion 22
targetSdkVersion 26
versionCode getCurrentDate() as int
versionName "2.0.0" + "." + getCurrentDate()
multiDexEnabled true
resConfigs 'hdpi'
ndk{
abiFilters 'armeabi-v7a'
}
}
//簽名
signingConfigs {
main {
storeFile file("./platform.jks") //簽名文件路徑
storePassword "multimedia"
keyAlias "multimedia"
keyPassword "multimedia"
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
signingConfig signingConfigs.main//簽名
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true// 是否進行混淆
shrinkResources true
signingConfig signingConfigs.main//簽名
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'// 混淆文件的位置
}
}
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
if (compileSdkVersionInt >= 26) {
useLibrary 'org.apache.http.legacy'
}
sourceSets {
main {
def manifestFile = file('AndroidManifest.xml')
def srcFolder = [
'src',
'src_pd',
'src_pd_gcam',
"frameworks/ex/camera2/portability/src",
"frameworks/ex/camera2/public/src",
"frameworks/ex/camera2/utils/src",
"frameworks/ex/vendor/camera2/portability/src"
]
def resFolder = [
'res',
'res_filter',
'res_filter_arc',
'res_filter_sprd',
'res_p',
'res_modify',
'res_transsion'
]
def assetsFolder = [
]
def jniFolder = [
'libs',
]
manifest.srcFile manifestFile
java.srcDirs = srcFolder
resources.srcDirs = srcFolder
aidl.srcDirs = srcFolder
renderscript.srcDirs = srcFolder
res.srcDirs = resFolder
assets.srcDirs = assetsFolder
//jni.srcDirs = jniFolder
jniLibs.srcDirs = jniFolder
}
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
//add this for Execution failed for task ':mockableAndroidJar'
tasks.whenTaskAdded { task ->
if (task.name.contains("mockableAndroidJar")) {
task.enabled = false
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:25.0.0'
compile 'com.android.support:support-v13:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.adobe.xmp:xmpcore:5.1.2'
compile 'com.github.bumptech.glide:glide:3.5.2'
//compile 'com.google.zxing:core:3.3.0'
compile 'com.google.guava:guava:19.0'
compile 'com.google.code.findbugs:jsr305:3.0.2'
compile 'com.android.support:multidex:1.0.1'
}
def getCurrentDate() {
def now = new Date().format('yyyyMMddHH');
return now;
}