Skip to content

Commit

Permalink
+ Enable R8
Browse files Browse the repository at this point in the history
+ Destroy binding instance in onDestroy()
+ fix "javax.net.ssl.SSLHandshakeException" issue
  • Loading branch information
Mina Mikhail committed Aug 18, 2021
1 parent 000721f commit 2e8110e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
implementation "com.google.code.gson:gson:$gson"
implementation "com.squareup.okhttp3:logging-interceptor:$interceptor"
implementation "com.mocklets:pluto:$pluto"
implementation "com.google.android.gms:play-services-auth:$play_services"

// UI
implementation "com.google.android.material:material:$material_design"
Expand Down
26 changes: 25 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,28 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile

## okhttp
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

## Retrofit
# 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

## Bottom Navigation
-keep public class com.google.android.material.bottomnavigation.* { *; }

# To prevent obfusticating model classes
# TODO : Make sure you do this for each feature module in your app
-keep class com.mina_mikhail.newsapp.features.news.domain.entity.* { *; }
27 changes: 27 additions & 0 deletions app/src/main/java/com/mina_mikhail/newsapp/core/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.mina_mikhail.newsapp.core

import android.app.Application
import com.google.android.gms.common.GooglePlayServicesNotAvailableException
import com.google.android.gms.common.GooglePlayServicesRepairableException
import com.google.android.gms.security.ProviderInstaller
import com.mina_mikhail.newsapp.BuildConfig
import com.mocklets.pluto.Pluto
import com.mocklets.pluto.PlutoLog
import com.mocklets.pluto.modules.exceptions.ANRException
import com.mocklets.pluto.modules.exceptions.ANRListener
import dagger.hilt.android.HiltAndroidApp
import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException
import javax.net.ssl.SSLContext

@HiltAndroidApp
class MyApplication : Application() {
Expand All @@ -15,11 +21,32 @@ class MyApplication : Application() {
fun onCreate() {
super.onCreate()

updateAndroidSecurityProvider()

initPlutoNetworkInspection()

initPlutoANRInspection()
}

private fun updateAndroidSecurityProvider() {
// To fix the following issue, when run app in cellular data, Apis not working
// javax.net.ssl.SSLHandshakeException: SSL handshake aborted: ssl=0x7edfc49e08: I/O error during system call, Connection reset by peer
try {
ProviderInstaller.installIfNeeded(applicationContext)
val sslContext: SSLContext = SSLContext.getInstance("TLSv1.2")
sslContext.init(null, null, null)
sslContext.createSSLEngine()
} catch (e: GooglePlayServicesRepairableException) {
e.printStackTrace()
} catch (e: GooglePlayServicesNotAvailableException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: KeyManagementException) {
e.printStackTrace()
}
}

private fun initPlutoNetworkInspection() {
if (BuildConfig.DEBUG) {
Pluto.initialize(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import androidx.viewbinding.ViewBinding

abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {

protected lateinit var binding: VB
private var _binding: VB? = null
open val binding get() = _binding!!
protected lateinit var navController: LiveData<NavController>

override
Expand All @@ -32,7 +33,7 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
}

private fun initViewBinding() {
binding = getViewBinding()
_binding = getViewBinding()
}

abstract fun getViewBinding(): VB
Expand All @@ -45,4 +46,11 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
fun onSupportNavigateUp(): Boolean {
return navController.value?.navigateUp()!! || super.onSupportNavigateUp()
}

override
fun onDestroy() {
super.onDestroy()

_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import com.mina_mikhail.newsapp.core.utils.showLoadingDialog

abstract class BaseFragment<VB : ViewBinding> : Fragment() {

protected lateinit var binding: VB
private var _binding: VB? = null
open val binding get() = _binding!!
private var mRootView: View? = null
private var hasInitializedRootView = false
private var progressDialog: Dialog? = null
Expand All @@ -27,7 +28,7 @@ abstract class BaseFragment<VB : ViewBinding> : Fragment() {
}

private fun initViewBinding(inflater: LayoutInflater, container: ViewGroup?) {
binding = getViewBinding(inflater, container)
_binding = getViewBinding(inflater, container)
mRootView = binding.root
}

Expand Down Expand Up @@ -81,4 +82,11 @@ abstract class BaseFragment<VB : ViewBinding> : Fragment() {
}

fun hideLoading() = hideLoadingDialog(progressDialog, requireActivity())

override
fun onDestroyView() {
super.onDestroyView()

_binding = null
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ buildscript {
gson = '2.8.6'
interceptor = '4.8.1'
pluto = '1.0.2-beta'
play_services = '19.2.0'

// UI
material_design = '1.4.0'
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.code.style=official
android.enableR8=true

0 comments on commit 2e8110e

Please sign in to comment.