前言
之前在平臺發佈過一篇關於AS導入二次開發系統包的文章,得到很多開發者的回饋和討論,有興趣的可以回看AS中導入framework.jar包編譯,運行全部通過。加上最近使用團隊開發模式,樓主也用到了Mac環境做開發。一些android/framework包的路徑問題也暴露出來。小夥伴們都抱怨每次編譯前都需要去project目錄下build.gradle修改相關的路徑參數。
抱着方便你(zhuang)我(bi)他,就研究了一些獲取系統路徑參數的辦法。
準備
首先,準備一個系統包,
LinkAddress mIpAddress = new LinkAddress(mIpAddr, prefixLength);
錯誤信息如下:
錯誤: 無法將類 LinkAddress中的構造器 LinkAddress應用到給定類型;
需要: 沒有參數
找到: InetAddress,int
原因: 實際參數列表和形式參數列表長度不同
打開源碼,這段構造函數是hide的。
/**
* Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
* the specified flags and scope. Flags and scope are not checked for validity.
* @param address The IP address.
* @param prefixLength The prefix length.
* @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
* @param scope An integer defining the scope in which the address is unique (e.g.,
* {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
* @hide
*/
public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
init(address, prefixLength, flags, scope);
}
操作流程
首先,將android包放在lib目錄下,並在項目的build.gradle目錄的dependence下添加以下代碼:
- gradle 3.0以上
compileOnly files('libs/android.jar')
- gradle 3.0以下
provide files('libs/android.jar')
這裏是爲了在編譯和打包時只編譯/打包進使用過得二次編碼包。因爲使用compile或者implementation會將整個android.jar包打入apk包中,還會造成dex打包錯誤。同時註釋以下依賴
// implementation fileTree(include: ['*.jar'], dir: 'libs')
其次,這裏無需再增加.iml的操作腳本。也就是如下腳本
//@Deprecated
preBuild {
doLast {
def imlFile = file(project.name + ".iml")
println 'Change ' + project.name + '.iml order'
try {
def parsedXml = (new XmlParser()).parse(imlFile)
def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
parsedXml.component[1].remove(jdkNode)
def sdkString = "Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"
new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
} catch (FileNotFoundException e) {
// nop, iml not found
}
}
}
最後,在項目根目錄build.gradle目錄下,需要使用全局配置路徑的方式來引入jar包。
//添加以下代碼,使android.jar包編譯先於系統下android.jar
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add("-Xbootclasspath/p:$rootDir${File.separator}app${File.separator}libs${File.separator}android.jar")
}
}
- rootDir 項目的路徑
- $ {File.separator}引用當前系統的分隔符,windows與mac不相同
編譯運行通過。