In the development process, multi-environment configuration is often encountered, such as in the Android development process, in different environments, the URL of the request server is different, using gradle to manage, is very convenient.
First look at the project directory structure:
Use Androidstudio developed to see this familiar. Main is the current development environment. Dev is the test environment. Product,staging for other environments, and of course there are more.
1. Define the URL of the request to the constant constant class:
public class Constant {
public static final String url= "Http://XXXXX";
}
Add constant classes in environments such as dev,product,staging, and set different URLs.
The following directory structure is set up: My app package is named Com.example.XX.myapplication
Note here is not to add the constant class in the main environment, otherwise the class will be repeated, gradle compile will be reported: Dumplicate class XXX
Use the same way as normal class!
Gradle configuration:
Apply plugin:' Com.android.application 'Android {Compilesdkversion +Buildtoolsversion"19.1.0"lintoptions {Abortonerrorfalse} defaultconfig {ApplicationID"Com.example.teamlab.myapplication"Minsdkversion9Targetsdkversion +Versioncode1Versionname"1.0"} signingconfigs {Debug{StoreFile file ("Src/main/keystore/debug.keystore") Storepassword"Android"Keypassword"Android"} release {StoreFile file ("Src/main/keystore/debug.keystore") Storepassword"Android"Keypassword"Android"} Staging {StoreFile file ("Src/main/keystore/debug.keystore") Storepassword"Android"Keypassword"Android"}} buildtypes {release {minifyenabledfalseProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro '}Debug{minifyenabledfalseProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro '}} productflavors {dev {applicationid' Com.example.teamlab.myapplication.dev 'Signingconfig Signingconfigs.Debug} staging {Signingconfig signingconfigs.DebugApplicationID' com.example.teamlab.myapplication.staging '} Product {ApplicationID' Com.example.teamlab.myapplication 'Signingconfig Signingconfigs.Debug}} packagingoptions {exclude' Meta-inf/notice.txt 'Exclude' Meta-inf/license.txt '}}dependencies {Compile filetree (dir:' Libs ', include: [' *.jar ']) Compile' com.android.support:appcompat-v7:21.+ 'Compile' com.android.support:support-v4:21.+ 'Compile' cn.pedant.sweetalert:library:1.3 'Compile' com.mcxiaoke.volley:library:1.0.+ 'Androidtestcompile' junit:junit:4.10 'Androidtestcompile' org.robolectric:robolectric:2.3+ 'Androidtestcompile' com.squareup:fest-android:1.0.+ 'Compile Project (': Slidingmenu ')}
Gradle Multi-environment URL request settings