androidandroid-gradle-pluginandroid-buildconfig

Change BuildConfig file in Android Studio using Gradle


I am trying to add URL into my BuildConfig file using Gradle in Android Studio. So I added this code into the gradle file

buildTypes {
    debug {
        buildConfigField "String", "MIDTRANS_API", '"https://app.sandbox.midtrans.com/snap/v1"'
    }
}

Then I tried to rebuild my project and hoped that this value will appear in BuildConfig file, but it was not so. Can someone explain how to do this correctly?

My complete gradle file code is as below

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
    debug {
        buildConfigField "String", "DAR_API", '"{some string}"'
        buildConfigField "String", "DAR_API_AUTH", '"{some string}"'
        buildConfigField "String", "MIDTRANS_API", '"https://app.sandbox.midtrans.com/snap/v1"'
    }

    rc {
        minifyEnabled true
        debuggable true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "DAR_API", '"{some string}"'
    }

    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "DAR_API", '"{some string}"'
    }
}

After that goes dependencies, but I think it is not related to problem.


Solution

  • Try this:

    buildTypes {
            debug {
                buildConfigField "String", "MIDTRANS_API", "\"https://app.sandbox.midtrans.com/snap/v1\""
            }
            release {
                buildConfigField "String", "MIDTRANS_API", "\"https://app.sandbox.midtrans.com/snap/v1\""
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    

    In Java you can access this by the below code after synchronising the Gradle files.

    String url = BuildConfig.MIDTRANS_API