Grade常見配置

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);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章