ディレクトリ
- Gradleの文法
- 詳細な展開プロジェクト
Gradleの文法
開発環境アンドロイドスタジオ3.4.1
Gradleのバージョン5.1.1
Gradleのは何ですか
AndroidStudioを書面でのGradle
メインモジュールのGradleプロジェクトに次のファイルを書き込みます。
あなたは、コンソールログに以下の情報を入力します。
Gradleの統一マルチモジュール構成コード
運動のGradle
準備:プロジェクトがアプリモジュールとライブラリモジュールが含まれていながら、アプリのプロジェクトを作成します。
要件:様々なモジュールを統一するためには、新しいGradleのファイルを作成することにより、一般的な構成です。
ステップ:
プロジェクトのルートディレクトリ、config.gradleという名前の新しいコンフィギュレーション・ファイルで、プロジェクトのルートディレクトリに、プロファイルの導入。
テスト:
config.gradleユーザ名で宣言プロパティ。そしてGradleのモジュールlirary用途は、プロジェクトをコンパイルし、属性をユーザ名出力ログ情報を表示します。
config.gradle文件内容如下:
//添加多个自定义属性,可以使用ext代码块
ext{
username = "zfc"
}
lirarymodule下的build.gradle内容如下:
...
println "${username}"
rootProject.ext.username = 1234
println "${rootProject.ext.username}"
...
次のようにプロジェクトがコンパイルしました:
抽出統一設定
//声明变量
isRelease = true
//打印变量的值
print isRelease
//定义字典
androidId=[
compileSdkVersion:29,
buildToolsVersion:"29.0.2",
applicationId:"com.canjun.components",
minSdkVersion:22,
targetSdkVersion:29,
versionCode:1,
versionName:"1.0"
]
appId = [
"app":"com.canjun.components",
"order":"com.canjun.components.order",
"personal":"com.canjun.components.personal"
]
url = [
debug: "https://10.1.1.1/debug/rest",
release: "https://10.1.1.1/release/rest",
]
supportLirary = "28.0.1"
dependencies = [
appcompat:'androidx.appcompat:appcompat:1.1.0',
constraintlayout:'androidx.constraintlayout:constraintlayout:1.1.3'
]
統一されたコンフィギュレーション・config.gradleの使用
apply plugin: 'com.android.application'
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies
android {
compileSdkVersion androidId.compileSdkVersion
buildToolsVersion androidId.buildToolsVersion
defaultConfig {
applicationId appId.app
minSdkVersion androidId.minSdkVersion
targetSdkVersion androidId.targetSdkVersion
versionCode androidId.versionCode
versionName androidId.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation 'androidx.appcompat:appcompat:1.1.0'
// implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// HashMap<String,String> map = new HashMap<>()
support.each{ k,v-> implementation v}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
クラスBuildConfigに一定の属性セット
buildConfigFiled("String","debug","\"${url.debug}\"")
apply plugin: 'com.android.application'
...
def url = rootProject.ext.url
...
android {
...
buildTypes {
debug{ buildConfigField("String","debug","\"${url.debug}\"")
}
release {
buildConfigField("String","debug","\"${url.release}\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
結果:メインモジュールBuildConfig(APP)のディレクトリに生成されたデバッグフィールド
アンドリュース共通プロジェクト構成のGradle
android{
defaultConfig{
...
...
//开启分包
multiDexEnable true
//设置分包配置
multiDexKeepFile file('multidex-config.txt')
//使用svg生成指定纬度的png图片
vectorDrawbles.generatedDensities('xhdpi','xxhdpi')
//使用v7包兼容(5.0以上版本)
vectorDrawbles.useSupportLibrary = true
//只保留指定和默认资源
resConfig("zh-rCN")
//ndk 默认保留架构
ndk{
abiFilters("armeabi","armeabi-v7a")
}
//设置源集属性
sourceSets{
main{
if(!isRelease){
manifest.srcFile ['src/main/AndroidManifest.xml']
java.srcDirs ['src/main/java']
res.srcDirs ['src/main/res']
resources.srcDirs ['src/main/resources']
aidl.srcDirs ['src/main/aidl']
assets.srcDirs ['src/main/assets']
}else{
mainfest.srcFile 'src/main/AndroidManifest.xml'
}
}
}
}
//signingConfigs 的定义一定要放在buildTypes的前面
signingConfigs{
debug{
storeFile file('xxx/xxxx/.android/debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release{
// 签名证书文件
storeFile file('D:/NetEase/netease.jks')
// 签名证书的类型
storeType "netease"
// 签名证书文件的密码
storePassword "net163"
// 签名证书中密钥别名
keyAlias "netease"
// 签名证书中该密钥的密码
keyPassword "net163"
// 是否开启V2打包
v2SigningEnabled true
}
}
buildTypes{
debug{
signingConfig.debug
}
release{
signingConfig.release
...
}
}
}
//对adb配置
adbOptions{
//操作超时时间为5秒
timeOutInMs = 5 * 1000_0
//adb install选项配置i
installOptions '-r','-s'
}
// 对 dx 操作的配置,接受一个 DexOptions 类型的闭包,配置由 DexOptions 提供
dexOptions {
// 配置执行 dx 命令是为其分配的最大堆内存
javaMaxHeapSize "4g"
// 配置是否预执行 dex Libraries 工程,开启后会提高增量构建速度,不过会影响 clean 构建的速度,默认 true
preDexLibraries = false
// 配置是否开启 jumbo 模式,代码方法是超过 65535 需要强制开启才能构建成功
jumboMode true
// 配置 Gradle 运行 dx 命令时使用的线程数量
threadCount 8
// 配置multidex参数
additionalParameters = [
'--multi-dex', // 多dex分包
'--set-max-idx-number=50000', // 每个包内方法数上限
// '--main-dex-list=' + '/multidex-config.txt', // 打包到主classes.dex的文件列表
'--minimal-main-dex'
]
}
// 执行 gradle lint 命令即可运行 lint 检查,默认生成的报告在 outputs/lint-results.html 中
lintOptions {
// 遇到 lint 检查错误会终止构建,一般设置为 false
abortOnError false
// 将警告当作错误来处理(老版本:warningAsErros)
warningsAsErrors false
// 检查新 API
check 'NewApi'
}
詳細な展開プロジェクト
重要なコンポーネント
PhoneModuleとAndroid Libray差
PhoneModule存在的配置:
apply plugin: 'com.android.application'
applicationId "com.canjun.components"
AndroidLibrary存在的配置:
apply plugin: 'com.android.library'
applicationId "com.canjun.components"(x)
統合とコンポーネント
統合は、唯一のメインモジュール、Liraryための他のモジュールは、これらのライブラリだけでは実行しないプロジェクトです。彼らは一緒に実行し、マスタモジュールのアプリになる必要があります
プロジェクトでは、それぞれのモジュールの一つの構成要素を個別に実行することができることを意味します。
生産のための統合された、組立及び多段の開発のためのテスト、高度に開発とテストを支持してデカップリングの特性。
プロジェクト構成のコンポーネント
-
メインモジュールのGradleの構成を変更します
apply plugin: 'com.android.application' def androidId = rootProject.ext.androidId def appId = rootProject.ext.appId def url = rootProject.ext.url def support = rootProject.ext.dependencies def isRelease = rootProject.ext.isRelease android { compileSdkVersion androidId.compileSdkVersion buildToolsVersion androidId.buildToolsVersion defaultConfig { applicationId appId.app minSdkVersion androidId.minSdkVersion targetSdkVersion androidId.targetSdkVersion versionCode androidId.versionCode versionName androidId.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildConfigField("boolean","isRelease",String.valueOf(isRelease)) // multiDexEnable true } buildTypes { debug{ buildConfigField("String","debug","\"${url.debug}\"") } release { buildConfigField("String","debug","\"${url.release}\"") minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) ... support.each{ k,v-> implementation v} implementation project(":common") //主模块不能依赖主模块 if(isRelease){ implementation project(":order") implementation project(":personal") } ... }
-
ライブラリモジュール構成変更のGradle
// 要点一 if(isRelease){ apply plugin: 'com.android.library' }else { apply plugin: 'com.android.application' } def androidId = rootProject.ext.androidId def appId = rootProject.ext.appId def support = rootProject.ext.dependencies android { compileSdkVersion androidId.compileSdkVersion buildToolsVersion androidId.buildToolsVersion defaultConfig { minSdkVersion androidId.minSdkVersion // 要点二 if(!isRelease){ applicationId appId.order } targetSdkVersion androidId.targetSdkVersion versionCode androidId.versionCode versionName androidId.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) support.each{ k,v -> implementation v} implementation project(":common") testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
3.エフェクトショー
集中の効果は示しています。
コンポーネントの効果は示しています。
集中型のパッケージが動的分離コード開発の仮組、
コンポーネントの開発以来、応答の一部のローカライズ後ろのモジュールのコードが、コードを書くが、処理されない場合、デフォルトではメインモジュールにパックされます。下図のように:
そのために特別な処理を必要とします:
- コンフィギュレーションのGradle、コードの分離
- 局在コードは、構成に応じて格納されているのGradle
確認します3.