├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── nairbspace │ │ │ │ └── retrofitdaggerexample │ │ │ │ ├── retrofit │ │ │ │ ├── model │ │ │ │ │ └── ExampleModel.java │ │ │ │ ├── ExampleInterface.java │ │ │ │ └── ExampleInterceptor.java │ │ │ │ ├── dagger │ │ │ │ ├── AppComponent.java │ │ │ │ └── NetworkModule.java │ │ │ │ ├── SetupApplication.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── nairbspace │ │ │ └── retrofitdaggerexample │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── nairbspace │ │ └── retrofitdaggerexample │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── vcs.xml ├── encodings.xml ├── modules.xml ├── runConfigurations.xml ├── compiler.xml ├── gradle.xml └── misc.xml ├── settings.gradle ├── after.png ├── before.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md ├── gradle.properties ├── gradlew.bat ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Retrofit Dagger Example -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /after.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/after.png -------------------------------------------------------------------------------- /before.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/before.png -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nairbspace/retrofit-dagger-example/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/java/com/nairbspace/retrofitdaggerexample/retrofit/model/ExampleModel.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample.retrofit.model; 2 | 3 | // Blank model for Retrofit Call 4 | public class ExampleModel { 5 | } 6 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Retrofit Dagger Example 3 | API Base URL 4 | Run 5 | Response Header: 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/nairbspace/retrofitdaggerexample/dagger/AppComponent.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample.dagger; 2 | 3 | import com.nairbspace.retrofitdaggerexample.MainActivity; 4 | 5 | import javax.inject.Singleton; 6 | 7 | import dagger.Component; 8 | 9 | @Singleton 10 | @Component(modules = NetworkModule.class) 11 | public interface AppComponent { 12 | void inject(MainActivity mainActivity); 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/nairbspace/retrofitdaggerexample/retrofit/ExampleInterface.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample.retrofit; 2 | 3 | import com.nairbspace.retrofitdaggerexample.retrofit.model.ExampleModel; 4 | 5 | import retrofit2.Call; 6 | import retrofit2.http.GET; 7 | 8 | /** Dummy interface used for Retrofit **/ 9 | public interface ExampleInterface { 10 | 11 | @GET("/") 12 | Call get(); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/nairbspace/retrofitdaggerexample/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/nairbspace/retrofitdaggerexample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | 34 | *.iml 35 | /.idea/workspace.xml 36 | /.idea/libraries 37 | .DS_Store 38 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\Brian\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android example using Dagger2 and Retrofit2 with Dynamic Base URL 2 | Android example showing how to change the Base URL in Retrofit2 dynamically during runtime even though Retrofit2 has already been injected in your application using Dagger2. I got the base concept from [here](https://gist.github.com/swankjesse/8571a8207a5815cca1fb). 3 | 4 | The whole purpose of this is so we don't have to create multiple instances of Retrofit2 when, for whatever reason, the Base URL changes or when we don't know the Base URL because the User has to put it in. 5 | 6 | Before User Input After User Input 7 | 8 | Notes: 9 | * See git commit log for procedural steps of how to construct this example. 10 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | 24 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | 4 | android { 5 | compileSdkVersion 23 6 | buildToolsVersion "23.0.3" 7 | 8 | defaultConfig { 9 | applicationId "com.nairbspace.retrofitdaggerexample" 10 | minSdkVersion 16 11 | targetSdkVersion 23 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | testCompile 'junit:junit:4.12' 26 | compile 'com.android.support:appcompat-v7:23.3.0' 27 | compile 'com.squareup.retrofit2:retrofit:2.0.0' 28 | compile 'com.squareup.retrofit2:converter-gson:2.0.0' 29 | compile 'com.google.dagger:dagger:2.0.2' 30 | provided 'javax.annotation:jsr250-api:1.0' 31 | apt 'com.google.dagger:dagger-compiler:2.0.2' 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/nairbspace/retrofitdaggerexample/SetupApplication.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | import com.nairbspace.retrofitdaggerexample.dagger.AppComponent; 7 | import com.nairbspace.retrofitdaggerexample.dagger.DaggerAppComponent; 8 | import com.nairbspace.retrofitdaggerexample.dagger.NetworkModule; 9 | 10 | public class SetupApplication extends Application { 11 | 12 | private AppComponent mAppComponent; 13 | 14 | // Convenience method for getting application context 15 | public static SetupApplication get(Context context) { 16 | return (SetupApplication) context.getApplicationContext(); 17 | } 18 | 19 | @Override 20 | public void onCreate() { 21 | super.onCreate(); 22 | 23 | // Create instance of AppComponent 24 | mAppComponent = DaggerAppComponent.builder() 25 | .networkModule(new NetworkModule()) 26 | .build(); 27 | } 28 | 29 | // Getter for AppComponent 30 | public AppComponent getAppComponent() { 31 | return mAppComponent; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/nairbspace/retrofitdaggerexample/retrofit/ExampleInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample.retrofit; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.inject.Inject; 6 | import javax.inject.Singleton; 7 | 8 | import okhttp3.HttpUrl; 9 | import okhttp3.Interceptor; 10 | import okhttp3.Request; 11 | import okhttp3.Response; 12 | 13 | /** 14 | * An interceptor that allows runtime changes to the API Base URL in Retrofit. 15 | * The Base URL is set by calling the {@link ExampleInterceptor#setInterceptor(String)} method. 16 | * */ 17 | @Singleton 18 | public class ExampleInterceptor implements Interceptor { 19 | private static ExampleInterceptor sInterceptor; 20 | private String mScheme; 21 | private String mHost; 22 | 23 | @Inject 24 | public static ExampleInterceptor get() { 25 | if (sInterceptor == null) { 26 | sInterceptor = new ExampleInterceptor(); 27 | } 28 | return sInterceptor; 29 | } 30 | 31 | private ExampleInterceptor() { 32 | // Intentionally blank 33 | } 34 | 35 | public void setInterceptor(String url) { 36 | HttpUrl httpUrl = HttpUrl.parse(url); 37 | mScheme = httpUrl.scheme(); 38 | mHost = httpUrl.host(); 39 | } 40 | 41 | @Override 42 | public Response intercept(Chain chain) throws IOException { 43 | Request original = chain.request(); 44 | 45 | // If new Base URL is properly formatted than replace with old one 46 | if (mScheme != null && mHost != null) { 47 | HttpUrl newUrl = original.url().newBuilder() 48 | .scheme(mScheme) 49 | .host(mHost) 50 | .build(); 51 | original = original.newBuilder() 52 | .url(newUrl) 53 | .build(); 54 | } 55 | return chain.proceed(original); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/nairbspace/retrofitdaggerexample/dagger/NetworkModule.java: -------------------------------------------------------------------------------- 1 | package com.nairbspace.retrofitdaggerexample.dagger; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.GsonBuilder; 5 | import com.nairbspace.retrofitdaggerexample.retrofit.ExampleInterceptor; 6 | import com.nairbspace.retrofitdaggerexample.retrofit.ExampleInterface; 7 | 8 | import javax.inject.Singleton; 9 | 10 | import dagger.Module; 11 | import dagger.Provides; 12 | import okhttp3.OkHttpClient; 13 | import retrofit2.Retrofit; 14 | import retrofit2.converter.gson.GsonConverterFactory; 15 | 16 | @Module 17 | public class NetworkModule { 18 | 19 | @Provides 20 | @Singleton 21 | ExampleInterceptor provideInterceptor() { // This is where the Interceptor object is constructed 22 | return ExampleInterceptor.get(); 23 | } 24 | 25 | @Provides 26 | @Singleton 27 | OkHttpClient provideOkHttpClient(ExampleInterceptor interceptor) { // The Interceptor is then added to the client 28 | return new OkHttpClient.Builder() 29 | .addInterceptor(interceptor) 30 | .build(); 31 | } 32 | 33 | @Provides 34 | @Singleton 35 | Gson provideGson() { 36 | return new GsonBuilder().create(); 37 | } 38 | 39 | @Provides 40 | @Singleton 41 | Retrofit.Builder provideRetrofitBuilder(Gson gson, OkHttpClient okHttpClient) { // The Client is then added to Retrofit 42 | return new Retrofit.Builder() 43 | .addConverterFactory(GsonConverterFactory.create(gson)) 44 | .client(okHttpClient) 45 | .baseUrl("http://localhost/"); // Dummy Base URL needed to create instance 46 | } 47 | 48 | @Provides 49 | @Singleton 50 | ExampleInterface provideExampleInterface(Retrofit.Builder builder) { // This is where Retrofit is finally created 51 | return builder.build().create(ExampleInterface.class); // with the Interface we provided 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 21 |