6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Asset Bekbossynov
  • 1,633
  • 3
  • 13
  • 25

3 Answers3

4

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
0

I tried using the same buildConfigField property in my project and I was successfully able to see it under BuildConfig file for debug variant.

Probably your project didn't rebuild properly. Try cleaning the project and then build it again.

To fetch the property value in your Java code, you'll have to do this:

String API_URL = BuildConfig.MIDTRANS_API;

Note: In order to access this property in release build you would have to copy it under release type as well. Then change the Build Variant to release.

In your gradle file do 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"
    }
}

This should fix your problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
0

I solved my problem by changing the package name in the manifest.xml file which still hadn't been updated:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="com.example.android">
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
narcis dpr
  • 939
  • 2
  • 12
  • 32