androidstudio中各種gradle配置解釋說明
我們知道每一個app就是as裏面的一個modle,最進由於工作接觸了各種gradle配置眼花繚亂,現在此記錄下來,以防止以後忘記,筆記如下:
- 關於一個app的gradle的配置
- 關於一個sdk的gradle的配置
- 如何設置一個只有服務沒有mainactivity的應用讓其運行並安裝無桌面圖標
關於一個app的gradle的配置
下面是一個app,它支持編譯不同版本的app,配置如下:
import java.text.SimpleDateFormat
apply plugin: 'com.android.application' //這個代表當前modle是一個app而不是一個library
android {
compileSdkVersion 24 //源碼編譯使用的api的版本
buildToolsVersion "24.0.1" //構建工具的版本
defaultConfig {
applicationId "com.zhangsan.lisi.services" //app的唯一標示,上架到市場上就是通過這個區分不同的app,相當於身份證
minSdkVersion 19 //19對應的系統是4.4,也就是說4.4以下版本的系統裝不了這個app,此爲最小兼容
targetSdkVersion 24 //目標sdk的版本,當遇到7.0系統的手機,這時的app運行效率最好
versionCode 1 //版本code,一般用於升級的時候使用,根據對比code來比大小判斷是否該升級
versionName "1.2." + getBuildTime() //版本名稱,起一個顯示的作用,常常看到設置裏面顯示的版本的介紹裏面的數字就是這個
// signingConfig signingConfigs.platformKey 配置默認的簽名,指向platformKey ,必須把這句話註釋掉的時候纔可以打一個沒有簽名的包
}
//簽名的配置,platformKey ,userKey 這個可以隨便取名字,只要對上號就行了singningConfigs.userkey,然後就是那幾個簽名的密碼,在新建簽名的時候有幾個配置,對照一下就知道填啥了
signingConfigs {
platformKey {
keyPassword 'zhangsan12345678'
storeFile file(getProjectDir().getParent() + '/key_store/platform.keystore')//簽名文件放在你的電腦哪裏
storePassword 'zhangsan12345678'
keyAlias 'zhansan'
}
userKey {
keyPassword 'lisi12345678'
storeFile file("C:/Users/zhangxiaoya/Desktop/demo.jks")
storePassword 'lisi12345678'
keyAlias '李四'
}
}
//這裏是編譯不同的版本,有debug ,release ,home ,business ,honour 幾個版本,由於在業務上需要區分不同版本的區別,所以這裏單獨配置
buildTypes {
debug {
buildConfigField "int", "Type", "1"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// signingConfig signingConfigs.userKey 可以單獨爲不同的版本配置不同的簽名,圖標之類的,具體百度
}
release {
buildConfigField "int", "Type", "1" //自定義type字段,編譯運行後在app->build->buildconfig->對應的版本下有個BuildConfig文件裏面會有type這個字段,在java文件中可以導入應用
minifyEnabled false //是否打開混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //混淆的配置文件規則
}
home {
buildConfigField "int", "Type", "1"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
business {
buildConfigField "int", "Type", "2"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
honour {
buildConfigField "int", "Type", "3"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk') && !outputFile.name.contains("debug")) {
//這裏修改apk文件名
def fileName = getProject().name + "-" + variant.baseName + "-${defaultConfig.versionName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
def getBuildTime() {
//設置時間格式
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHH");
//獲取當前時間
Date curDate = new Date(System.currentTimeMillis());
return formatter.format(curDate);
}
dependencies {
//引入對應的文件
compile files('libs/microlog4android-1.0.0.jar')
compile files('libs/xUtils-2.6.14.jar')
// compile project(':sdk-framework')
compile 'com.bot.library.urlconfig:urlconfig:0.0.2'
}
關於一個sdk的gradle的配置
下面是一個library的gridle,將來會打包成jar供其他人使用,它的配置如下:
//由於這裏是提供給別人使用的一個依賴庫,所以這個需要應用com.android.library這個插件,同時需要吧代碼提交到maven倉庫,因爲也申明需要應用一下maven插件
apply plugin: 'com.android.library'
apply plugin: 'maven'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0.1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//這個你利用gradle上傳到maven中央庫的基本配置,需要提供javadoc.jar,sources.jar,aar或者jar,pom,這個是生成sources.jar的配置
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
}
//項目中使用到了其他的庫,所以這裏是添加對這個庫的依賴
dependencies {
//吧lib下面的jar加載進來,纔可以使用
compile fileTree(include: ['*.jar'], dir: 'libs')
//在ecplise裏面有jar和項目的引用,這句話就類似項目引用,意思是指把名字叫cos的library引用到項目中
// compile project(':library-cos')
}
//下面是要提交到maven的配置
uploadArchives {
configuration = configurations.archives
repositories.mavenDeployer {
//遠程倉庫的地址,貌似有具體ip的基本上都是建在局域網的倉庫
repository(url: 'http://101.154.73.251:8080/nexus/content/repositories/releases/') {//倉庫地址
authentication(userName: "admin",//用戶名
password: "@xx321!@#")//密碼
}
pom.project {
name 'juude-library' //項目的名稱
packaging 'aar' //要提交文件的類型
description 'none' //描述
url 'http://101.123.25.110:8080/nexus/content/repositories/releases/'//倉庫地址
groupId "com.bot.library.sdk" //註冊sonatype賬號時有填,這裏是域名反轉+項目名
artifactId "sdk"
version android.defaultConfig.versionName
}
}
}
如何設置一個只有服務沒有mainactivity的應用讓其運行並安裝無桌面圖標
只有服務的應用本來就沒有圖標,服務本來就是工作在後臺的,一般系統內置的應用都是沒得圖標在桌面上的,在ecplise裏面你只需要吧這2句話去掉就好了或者根本不配置mainactivity,可是不知爲何在as裏面去掉了這兩句話,app編譯就會出錯,報找不到mainactivity的錯誤
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
如果你是用as開發,那麼只需要在mainactivty配置中加上這麼一句話就好了
<data android:host="AuthActivity" android:scheme="com.android.example" />