Gradle Multi-environment URL request settings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.