引入依賴庫
| 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|
| retrofit | okhttp | okio | RxJava | RxAndroid |
參考文檔
- Linux命令大全
- Android Gradle Plugin 3.0 implementation、api、compile區(qū)別
- 給 Android 開發(fā)者的 RxJava 詳解
- RxJava + Retrofit完成網(wǎng)絡(luò)請求
一、創(chuàng)建項目
- 新建SilenceCoder項目,創(chuàng)建時勾選添加Kotlin支持,以便后續(xù)擴(kuò)展。
- 創(chuàng)建BaseFeature Library便于以后結(jié)構(gòu)改造成Instant App。
二、配置權(quán)限
-
網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
三、 在BaseFeature中引入Retrofit 2.x 網(wǎng)絡(luò)庫
-
添加依賴
implementation 'com.squareup.retrofit2:retrofit:2.3.0' -
Retrofit 混淆配置。
依賴關(guān)系:Retrofit ---->okhttp---->okio。所以相應(yīng)的混淆都應(yīng)該添加# Platform calls Class.forName on types which do not exist on Android to determine platform. -dontnote retrofit2.Platform # Platform used when running on Java 8 VMs. Will not be used at runtime. -dontwarn retrofit2.Platform$Java8 # Retain generic type information for use by reflection by converters and adapters. -keepattributes Signature # Retain declared checked exceptions for use by a Proxy instance. -keepattributes Exceptions -
okhttp 混淆配置
-dontwarn okhttp3.** -dontwarn okio.** -dontwarn javax.annotation.** -dontwarn org.conscrypt.** # A resource is loaded with a relative path so the package of this class must be preserved. -keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase -
Okio混淆配置
-dontwarn okio.**
四、在BaseFeature中引入Retrofit Gson Converter,參考retrofit中Converters章節(jié),支持如下Converters:
-
Gson:
com.squareup.retrofit2:converter-gson -
Jackson:
com.squareup.retrofit2:converter-jackson -
Moshi:
com.squareup.retrofit2:converter-moshi -
Protobuf:
com.squareup.retrofit2:converter-protobuf -
Wire:
com.squareup.retrofit2:converter-wire -
Simple XML:
com.squareup.retrofit2:converter-simplexml - Scalars (primitives, boxed, and String):
com.squareup.retrofit2:converter-scalars
五、在BaseFeature中引入RxJava 2.x.y庫
-
添加依賴
implementation "io.reactivex.rxjava2:rxjava:2.x.y"
六、在BaseFeature中引入RxAndroid庫
-
添加依賴
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'