1. 模块之间共享变量的定义
在根目录创建common_config.gradle,内容如下
project.ext {
javaVersion = 8
javaMaxHeapSize = '4G'
compileSdkVersion = 29
buildTooksVersion = "29.0.1"
minSdkVersion = 19
targetSdkVersion = 29
//混淆相关
minifyEnable = false
shrinkResEnable = minifyEnable
//jdk版本
sourceCompatibility = this.&getJavaVersion()
targetCompatibility = this.&getJavaVersion()
}
def getJavaVersion() {
switch(project.ext.javaVersion) {
case "6":
return JavaVersion.VERSION_1_6
case "7":
return JavaVersion.VERSION_1_7
case "8":
return JavaVersion.VERSION_1_8
case "9":
return JavaVersion.VERSION_1_9
}
}
在模块的gradle引入,使用
apply from: "${project.rootDir}/common_config.gradle"
project.ext.compileSdkVersion
如果嫌麻烦每一个模块都需要这么导入那就可以在项目的gradle 中导入,则全都生效
subprojects {
apply from: "${project.rootDir}/common_config.gradle"
}
2. 如何引入arr
implementation (name: 'react-native-0.18.0', ext: 'arr')
//为了能够在工程的libs目录中找到其中的arr文件
repositories {
flatDir {
dirs 'libs'
}
}
问题如果你引入arr的模块被别人依赖,别人是找不到你的arr的,需要配置根目录的build.grade
allprojects {
repositories {
google()
jcenter()
flatDir {
dirs '../module_name/libs'
}
}
}
3. 如何自动签名
创建keystore.properties
storePassword=741963sc
keyPassword=741963sc
keyAlias=dsy
storeFile=scy.jks
引入gradle
// Remove private signing information from your project
// 创建一个名为keystorePropertiesFile的变量,并将其初始化为rootProject文件夹中的keystore.properties文件。
def keystorePropertiesFile = rootProject.file("keystore.properties")
// 初始化一个名为keystoreProperties的新Properties()对象
def keystoreProperties = new Properties()
// 将keystore.properties文件加载到keystoreProperties对象中
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
signingConfigs {
myConfig {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
debug {
signingConfig signingConfigs.myConfig
minifyEnabled project.ext.minifyEnable
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
signingConfig signingConfigs.myConfig
minifyEnabled project.ext.minifyEnable
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
命令行输入 gradlew assembleRelease 就可以生成带签名的apk了
4. 混淆
把minifyEnable = true,一般的release版本的设置就可以了,然后按照你的要求配置proguard-rules.pro
注意 四大组件 Fragment 和自定义控件是不需要添加混淆规则的,因为这些默认都是不会被混淆的
使用了Gson之类的工具那么javabean不需要混淆,保证枚举不被混淆,对第三方中的库不进行混淆等
例如
-keep class com.scy.android.entity.** { *; } #实体类不参与混淆
-keep class com.scy.android.view.** { *; } #自定义控件不参与混淆
#不混淆R类
-keep public class com.scy.android.R$*{
public static final int *;
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}