Ip Getter


Source link: https://github.com/brunodles/IpGetter

IpGetter Plugin

The IpGetter is a plugin which will help us to get the Ip of the current machine to use on a build.gradle.

You may ask, why do I need that? The answer is simple, you need that to replace your API URL for your computer address during development stage, to point to a MockApi or a under development api. That's the main idea.

How to use

BuildTypes, do you know it?

Have you ever made a if to change the api base url?
There's no need to check it with a if like that if (BuildConfig.DEBUG) or String API_URL = BuildConfig.DEBUG? "192.168.10.42:3000" : "http://my-real-api.com".

There are a lot of ways to do that, you can create a build types and change the API URL depending on the build type.

You can enable and disable the test url just by changing you gradle.properties.

Plugin setup?

You can choose the wanted network interface you want to use to get the ip from.

To do that you need to create a new file in your root project path. rootPath/ip.properties.
You can also put these properties into your gradle.properties. In your project rootPath/gradle.properties or inside homePath/.gradle/gradle.properties.

Inside this file you just can create three properties.

  • api.use_mock this is the flag to enable your local server
  • api.local_ip_interface will be used to define the network interface
  • api.local_port will be used to define the service port

Sample file

api.use_mock=true api.local_ip_interface=wlp9s0 api.local_port=3000

Using

Now, in your app level root/app/build.gradle call the method localIpOr(). Yes you can call methods inside the build.gradle files, so do it.

It will be like this

buildConfigField "String", "API_URL", "\"${
localIpOr('http://my-real.api')
}
\""

Sample

Here is a sample of how the build.gradle will end up.

buildscript {

  repositories {

maven {
 url "https://dl.bintray.com/brunodles/TempRepo" 
}

  
}

  dependencies {

classpath 'com.github.brunodles:IpGetter:1.1.0'
  
}
 
}
 apply plugin: 'com.github.brunodles.IpGetter' android {

  ...
  defaultConfig {

...

buildConfigField "String", "API_URL", "\"${
localIpOr('http://my-real.api')
}
\""
  
}
 
}

You can also change it only on debug build type

android {

  ...
  defaultConfig {

...

buildConfigField "String", "API_URL", "\"http://my-real.api\""
  
}

  buildTypes {

...

release {

 ...

 // don't change anything on release

}

debug {

 ...

 buildConfigField "String", "API_URL", "\"${
localIpOr('http://my-real.api')
}
\""

}

  
}
 
}

Contributing

Issues are welcome, create one and we will discourse about it. If you saw any error, please reports, it will be a great help.

Licence

You can use any code you found here, some of then I found on the internet too.

I'm using the MIT Licence, take a look on Licence.

If you're using this plugin, please give me some credits too.

Sources

This one have many sources, I don't remember where I found it at first, it was on 2014. Now we have some articles about it, so I'll link some of then.

Gradle

JitPack

Resources

Floating hint for ProgressBar/SeekBar.

Using Recyclerview with the new Android Data Binding framework.

A set of custom views used to navigate between the pages (or indicate the current position) of a ViewPager from the Android Support Package.

This library allows you to embed a Barcode scanner as a Fragment.

Nammu is a simple library to speed up working with new Runtime Permissions introduced in Android M.

BottomSheet is an Android component which presents a dismissible view from the bottom of the screen. BottomSheet can be a useful replacement for dialogs and menus but can hold any view so the use cases are endless.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes