[AS3.0.1] Gradle multi-version package learning details, as3.0.1gradle
Let's talk about the requirements first!
Because there are often beta and official versions during development. At this time, there may be differences between server access addresses and two applications that need to be compatible at the same time. However, one is a test version and the other is an official version. Although we can use a stupid method, we just copy two projects with code. The package name can be changed. However, management between two projects is a big problem. If you change the version in the beta version but go to the official version, you forgot to modify the version. This will cause a lot of redundant management consumption. At this time, dynamic management of Gradle is a good management.
Create a project
Go to the build. gradle file under the default app project.
Open to modify buildTypes
Only one release is set by default. The initial code is as follows:
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
Here, minifyEnabled indicates whether to enable obfuscation, and the following proguardFiles indicates the path of the obfuscation file called after obfuscation is enabled.
Next, we will modify this buildTypes and add the debug settings.
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug{ minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
You can copy the release file and modify the name.
In this way, the formal and tested versions are differentiated.
Test the official version differentiation settings
Server address differentiation Modification
Here we need to use buildConfigField.
The effect we want is that the address called after debug and release compilation is different.
The stupid way is to add the following content to the Java code:
Public static final String Host = "official version"; // public static final String Host = "test version ";
Let's take a look at how Gradle is managed.
Add both debug and release to buildConfigField
Modify the Code as follows:
BuildTypes {release {minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' buildConfigField ("String", "Host", "\" official version \ "")} debug {minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' buildConfigField ("String", "Host", "\" Beta \"")}}
Three Parameters
The first type is String Boolean int.
The second is the name.
The third is the content value.(Then mention that the content value here must be added ", otherwise the obtained result will not exist)
After modifying the code, click Sync Now to generate a BuildConfig file. Click here to see
!
We can see that the values obtained by modifying debug and release are all set.
You can obtain the parameters by calling the following command:
String host = BuildConfig.Host
The Beta and official versions can coexist.
You only need to add rhetoric to debug.
Debug {applicationIdSuffix ". debug" minifyEnabled false proguardFiles comment '), 'proguard-rules. Pro' buildConfigField ("String", "Host", "\" test \"")}
In this way, the generated app adds. debug to the original package name.
The package names of the two installed apps are as follows:
-Modify APP name
You can use resValue to generate a new string.
Modify the Code as follows:
BuildTypes {release {minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' buildConfigField ("String", "Host", "\" official version \ "") resValue ("string", "app_name1", "myMode")} debug {applicationIdSuffix ". debug "minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' buildConfigField ("String", "Host", "\" Beta \ "") resValue ("string", "app_name1", "myModecopy ")}}
Three Parameters
The first type is string int and so on.
The second is the name.
The third is the content value.
Calls are the same as normal writes to the res file.
getResources().getString(R.string.app_name1)
Now we want to modify the app name and modify the value in android: label in the configuration file of the project.
android:label="@string/app_name1"
The name of the project we installed is different.
Before entering multi-version settings. We first set the signature, because I am going to directly use Build Variants to select build to directly install the project, so I added a signature setting here.
The signature code is as follows:
Apply plugin: 'com. android. application 'android {..... other code signingConfigs {config {keyAlias 'gjn' keyPassword '000000' storeFile file ('d:/project/MyDemo/gjnKey. jks ') storePassword '000000'} buildTypes {release {minifyEnabled false proguardFiles getdefaproproguardfile('proguard-android.txt'), 'proguard-rules. pro' buildConfigField ("String", "Host", "\" official version \ "") resValue ("string", "app_name1", "myMode") signingConfig signingConfigs. config} debug {applicationIdSuffix ". debug "minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' buildConfigField ("String", "Host", "\" Beta \ "") resValue ("string", "app_name1", "myModecopy ")}}.... other code}
The signature is
signingConfig signingConfigs.config
Select the place where build is installed
Multi-Version Control
Use productFlavors for multi-Version Control
Here we want to add a free version.
Add the following code:
productFlavors{ dev{ flavorDimensions("versionCode") } free{ applicationIdSuffix ".free" flavorDimensions("versionCode") } }
We need to save the default version, so we have added two.
In fact, the free and dev {} attributes can be set in the same way as the defaultConfig settings of the project.
Like this default
defaultConfig { applicationId "com.gjn.mydemo" minSdkVersion 19 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }
You can also set multiple attributes for free.
I have added the following attributes to free and want to try to replace them.
ProductFlavors {dev {flavorDimensions ("versionCode")} free {applicationIdSuffix ". free "buildConfigField (" String "," Host "," \ "Free version \" ") resValue (" string "," app_name1 "," myModefree ") flavorDimensions ("versionCode ")}}
Now we have installed four versions.
Then let's take a look at the package name and open content for each version.
We will install four versions in sequence
-DevDebug
DevRelease
FreeDebug
FreeRelease
We also found that
BuildConfigField ("String", "Host", "\" Free version \ "") resValue ("string", "app_name1", "myModefree ")
Is ineffective.
There are other in-depth use cases, which can only be supplemented later.