Ip Getter

General

Category
Free
Tag
Gradle Plugins
License
MIT License
Registered
Oct 13, 2016
Favorites
0
Link
https://github.com/brunodles/IpGetter
See also
gradle-properties-plugin
gnag
AspectJ Gradle
Crate
Easylauncher

Additional

Language
Kotlin
Version
N/A
Created
Sep 27, 2016
Updated
Jun 1, 2018
Owner
Bruno de Lima e Silva (brunodles)
Contributor
Bruno de Lima e Silva (brunodles)
1
Activity
Badge
Generate
Download
Source code

Announcement

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 {
    dependencies {
        classpath 'com.brunodles:ipgetter:1.1.0'
    }
}
apply plugin: '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