Time wasted, lost years.
sequence
This is the second day of my participation in the August Text Challenge.More challenges in August
- ‘s note | MobPush access
- Gradle Notes the basic build configurations I have used.
To tell you the truth, I want to mix a cup, the cup was broken by the cat.
A small factory ape ape, forgive me for not seeing the appearance of the world, 😂
Let’s start with a structure diagram from the current Demo:
Always want bit by bit accumulation, follow chicken elder brother to learn slowly, in case one day outstanding?
I really want to talk about Gradle in detail. I’m afraid I still have limited ability at present. I only have a superficial understanding of Gradle
Gradle basic configuration
In fact, this should be the basic build configuration that encapsulates the project, so that subsequent modules can reduce a lot of repetitive content, on the one hand, redundancy, and on the other hand, higher maintenance costs.
1, the basic gradle
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android {// Specify the API level compileSdkVersion Versions.compileSDK // specify the SDK tool version to be used when building the project, Manual configuration is not required after Android Studio 3.0. BuildToolsVersion versions. buildTools // Specifies the default value of the build attribute for the Android plug-in defaultConfig {// minSdkVersion, the least compatible version Versions.minSDK // Highest supported version targetSdkVersion versions. targetSDK // Internal version identifier versionCode 1 // Apk version information - versionName is visible to users "1.0" // Only Chinese resources are reserved. Set this parameter based on the product content. If only Chinese characters are supported, you are advised to set this parameter. ResConfigs "en" // enable multiDexEnabled true NDK {// set the supported SO library architecture abiFilters "armeabi", "X86} testInstrumentationRunner androidx. Test. Runner. AndroidJUnitRunner"} / / configure Java compilation (level of encoding format, compile and produce bytecode version) compileOptions { encoding = 'utf-8' sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility VERSION_1_8} kotlinOptions {jvmTarget = JavIstan.version_1_8.toString ()} // Enable view binding compatible with Gradle 4.x and later BuildFeatures {dataBinding = true X +} lintOptions {// Continue abortOnError false} // Disable AaptOptions {cruncherEnabled = false}} /** * implementation: implementation does not pass down, only in the current module; Dependencies {implementation fileTree(dir: 'implementation fileTree') ['*.jar']) implementation Deps.kotlinStdlibJdk7 implementation Deps.appcompat implementation Deps.coreKtx testImplementation Deps.junit androidTestImplementation Deps.extJunit androidTestImplementation Deps.espressoCore }Copy the code
2. Build. Gradle in app
apply plugin: 'com.android.application' apply from: ".. /basic.gradle" android {// Obtain local private information configuration local.properties see section 3 properties = new properties () properties.load(project.rootProject.file("local.properties").newDataInputStream()) def jksDir = properties.getProperty('jksDir') def jksAlias = properties.getProperty('jksAlias') def jksPassword = Properties.getproperty ('jksPassword') // Specifies the default value defaultConfig {applicationId for the version property of the Android plug-in for all builds "Com.pwccn. fAdvisor"} // Signature information configuration (depending on the project) // It is more important to configure config directly and write release information, SigningConfigs {config {v1SigningEnabled true v2SigningEnabled true storeFile File (jksDir) StorePassword jksPassword keyAlias jksAlias keyPassword jksPassword}} // encapsulates all buildTypes of the project configuration buildTypes {debug {// secondary package name ". Debug "// Disable picture compression crunchPngs false // Enable debuggable true // Disable debug build Crashlytics ext.enablecrashlytics = false // Disable automatic build ID ext.alwaySupDateBuildid = false // Signature configuration signingConfig Signingconfigs. config // Turn off shrinkResources false // Turn off minifyEnabled false // turn off zipAlign to optimize zipAlignEnabled ProguardFiles getDefaultProguardFile(' proguard-Android-optimize.txt '), 'ProGuard-rules.pro'} release {// Disable debuggable false // signingConfig signingConfigs.config // Enable resource reduction ShrinkResources true // Enable code reduction minifyEnabled true // Enable zipAlign optimization zipAlignEnabled true // obfuscate proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}} // multiDex related configuration dexOptions {// set the running memory javaMaxHeapSize '4096m' // increase each dex string index limit 2^16 -> 2 ^ 32 jumboMode whether true / / save the runtime annotation class to keep to the main dex keepRuntimeAnnotatedClasses false / / maximum number of processes to the default value is 4 maxProcessCount 4 / / the number of threads FlavorDimensions "default" threadCount 4 // enable precompiled dex lib preDexLibraries true} BuildConfigField "int", "hostType", "1" buildConfigField "Boolean ", "LOG_DEBUG", "true" } release { buildConfigField "int", "hostType", "3" buildConfigField "boolean", "LOG_DEBUG", Variants -> if (variants. BuildType. Name! = "debug") { variant.getPackageApplicationProvider().get().outputDirectory = new File(project.rootDir.absolutePath + "/apk") } variant.outputs.all { apkData -> def fileName if (variant.buildType.name == 'debug') { fileName = "A_D_v" } else if (variant.buildType.name == 'release') { fileName = "A_R_v" } def timeStamp = new Date().format("yyyyMMdd_HHmmss") def apkName = fileName + variant.versionName + "_" + variant.flavorName + "_${timeStamp}" + ".apk" apkdata. outputFileName = apkName}}} /** * implementation: implementation is not passed down, only in the current module; */ dependencies {//... }Copy the code
buildConfigField
At build time, Gradle generates the BuildConfig class so that the application code can check for information related to the current build. We can add the custom property fields we need with buildConfigField.
For example, the basic log switch, which used to be true/false, is manually changed in the release version. In some cases, the required change is forgotten before the package is sent.
To make a small upgrade of my method, add the following to the existing gradle file:
BuildTypes {debug {// Log controller - output Log buildConfigField "Boolean ", "LOG_DEBUG", "true" // ... BuildConfigField "Boolean ", "LOG_DEBUG", "false" //... }}}Copy the code
Subsequent Build changes will insert this variable in BuildConfig based on the current Build type:
public final class BuildConfig {
// ...
// Fields from build type: debug
public static final boolean LOG_DEBUG = true;
}
Copy the code
Use it directly with buildconfig.log_debug.
Local. properties stores the certificate key
We could have written it directly into the build, but it’s not safe, so we put it in the local.properties file.
JksDir =.. /jks/HLQ_Test.jks jksAlias = HLQ_Test jksPassword = 12345678Copy the code
One day
Use README wisely
I don’t know if you have ever encountered this situation. When you are new to a company, after the project is clone, you don’t know much about many things. If you ask your colleagues, they are also busy. I once saw a big man in Zhihu say:
- Write more than one line of notes, and people convenient, convenient with yourself.
Personally, I still recommend README skillfully, record some commonly used things in the project, convenient for later friends to quickly start ~
Attached here is an example of my previous project, which is also trying, welcome to provide better suggestions ~
Here I’ll take a snapshot of the README for the project records I was in charge of
THK
- Gradle tips and tricks
- Android Plugin DSL Reference