'Cannot connect to internet after release build in Android Studio

I created an app using Android Studio. It connects to the internet for various reasons (eg. facebook SDK, Firebase database).

It's connecting OK when the app is running in a real device with build variant debug mode but it's not connecting to the internet when the app is running in a real device with build variant release mode.

Here is my build gradle and android manifest file

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />

<application
    android:name=".GenarateHashKey"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity
        android:name=".StartScreen"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_start_screen"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".QuestionList" />
    <activity android:name=".SignInScreen" />
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    <activity android:name=".ReportThisQuestionActivity" />
    <activity android:name=".UpdateQuestionlistActivity" />
</application>

here is my gradle file

apply plugin: 'com.android.application'android {
signingConfigs {
    KarateRefereeQuiz {
    }
}
compileSdkVersion 23
buildToolsVersion "23.0.2"
repositories {
    mavenCentral()
}
defaultConfig {
    applicationId "com.example.prasad.karaterefereequiz"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

}
packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
     }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile files('libs/commons-io-2.4.jar')
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:recyclerview-v7:23.2.0'
    compile 'com.facebook.android:facebook-android-sdk:4.+'
    compile files('libs/firebase-client-android-2.5.2.jar')
}


Solution 1:[1]

I found the solution.facebook sdk needs realese hashkey for release builds

Solution 2:[2]

From my gradle build:

signingConfigs {
    release {

    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
    }
}

followed by dependencies, and then:

allprojects {
afterEvaluate { project ->
    def propsFile = rootProject.file('keystore.properties')
    def configName = 'release'

    if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
        def props = new Properties()
        props.load(new FileInputStream(propsFile))
        android.signingConfigs[configName].storeFile = file(props['storeFile'])
        android.signingConfigs[configName].storePassword = props['storePassword']
        android.signingConfigs[configName].keyAlias = props['keyAlias']
        android.signingConfigs[configName].keyPassword = props['keyPassword']
    }
}

also dependencies is:

compile fileTree(dir: 'libs', include: ['*.jar'])

Solution 3:[3]

this worked for me. first of all in res package create xml package and in xml package create new xml resource file with network_security_config name

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
 </network-security-config>

then in manifest in

android:networkSecurityConfig="@xml/network_security_config"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prasad Manjula Hewavitharana
Solution 2
Solution 3 Trilochan