以前就一直有使用Android Studio去开发系统应用的想法,后来由于各种原因,一直未果,网上也没有比较详细的教程示例,由此写下这篇文章,觉得可以名正言顺的使用了。至此正式抛弃干儿子eclipse,使用Google亲儿子Android Studio开发系统级应用。
一、导入工程
导入后结构如图
二、统一依赖管理
打开最外层的build.gradle
配置统一依赖,加入
ext{
compileSdkVersion = 25
buildToolsVersion = "25.0.1"
minSdkVersion = 24
targetSdkVersion = 25
versionCode = 1
// compile support libs
libs_support_v4="com.android.support:support-v4:25.0.1"
libs_support_v13="com.android.support:support-v13:25.0.1"
libs_preference_v14="com.android.support:preference-v14:25.0.1"
libs_appcompat_v7="com.android.support:appcompat-v7:25.0.1"
libs_recyclerview_v7="com.android.support:recyclerview-v7:25.0.1"
libs_preference_v7="com.android.support:preference-v7:25.0.1"
// provided support lib
lib_jar_bouncycastle="../lib/bouncycastle.jar"
lib_jar_core="../lib/core.jar"
lib_jar_core_oj="../lib/core-oj.jar"
lib_jar_framework="../lib/framework.jar"
lib_jar_ims_common="../lib/ims-common.jar"
lib_jar_telephony_common="../lib/telephony-common.jar"
}
由于调用自己的framework,所以加载自己的framework.jar
在最外层build.gradle中引入
allprojects {
repositories {
jcenter()
}
gradle.projectsEvaluated {
tasks.withType(JavaCompile){
options.compilerArgs.add('-Xbootclasspath/p:lib/framework.jar')
}
}
}
framework.jar路径视具体情况配置。
开发时,不希望有引用到framework.jar的方法报红,引入以下方法暂时将自己的framework.jar优先级提高
def pushDownSDK(iml){
def imlFile = file(iml)
try {
def parsedXml = (new XmlParser()).parse(imlFile)
def jdkIndexOf =
parsedXml.component[1].orderEntry.findIndexOf {it.'@type' == 'jdk'}
if ( jdkIndexOf <= 1) {
def jdkNode =
parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
parsedXml.component[1].remove(jdkNode)
def API = "Android API " + ext.targetSdkVersion + " Platform"
new Node(parsedXml.component[1],
'orderEntry',
['type': 'jdk', 'jdkName': API, 'jdkType': 'Android SDK'])
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)
imlFile.text = writer.toString()
println "Push File: $iml jdk priority ok"
}
} catch (Exception e) {
// do nothing
}
}
以上方法的调用是在Gradle构建完成之后调用的,于是加入
gradle.buildFinished {
pushDownSDK("settings/settings.iml");
}
子工程就是普通的配置了,这里仅仅贴一下,如下,moudle settings下的build.gradle
import java.text.SimpleDateFormat
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
def apkName = "Settings.apk"
def baseVersion = "2.001"
def buildTime(){
def df = new SimpleDateFormat("yyMMddHHmm")
return df.format(new Date())
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.android.settings"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName "${baseVersion}.${buildTime()}"
}
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
output.outputFile = new File(outputFile.parent, apkName)
}
}
}
}
repositories {
flatDir {
dirs 'aar'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'settingslib', ext: 'aar')
compile(name: 'setupwizardlib', ext: 'aar')
provided files(rootProject.ext.lib_jar_bouncycastle)
provided files(rootProject.ext.lib_jar_core)
provided files(rootProject.ext.lib_jar_core_oj)
provided files(rootProject.ext.lib_jar_framework)
provided files(rootProject.ext.lib_jar_ims_common)
provided files(rootProject.ext.lib_jar_telephony_common)
compile rootProject.ext.libs_support_v4
compile rootProject.ext.libs_support_v13
compile rootProject.ext.libs_preference_v14
compile rootProject.ext.libs_appcompat_v7
compile rootProject.ext.libs_recyclerview_v7
compile rootProject.ext.libs_preference_v7
}
三、编写简单的lint.xml
由于是系统级应用,某些lint检查是可以忽略或者降低检查的严格性,自己根据需求定义lint规则,在主moudle下新建lint.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<lint>
<issue id="MissingTranslation" severity="ignore"/>
<issue id="LongLogTag" severity="warning"/>
<issue id="ProtectedPermissions" severity="warning"/>
</lint>
以上改变了原有的规则检查,其中
MissingTranslation是对语言翻译的检查,多国语言对于我来说实在不是必须的,反正到时候有需求的时候再导入即可,于是直接降级为忽略ignore了;
LongLogTag是在打印log的时候log长度不宜过长,目前处于调试阶段,对于这种规则检查也是可以忽略的,但还是暂且降级为警告吧warning,以便后续处理;
ProtectedPermissions是指在AndroidManifest.xml定义权限时,定义了系统级别的权限,不过既然是开发系统级应用,好吧,为了好看,那就降级为警告warning就行。
好,至此,已经可以使用Android Studio来进行Android系统级应用开发了,快来愉快的学习开发系统应用吧。
女神镇楼