Change BuildConfig file in Android Studio using Gradle
Asked Answered
B

3

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.

Bellis answered 17/7, 2018 at 5:0 Comment(0)
E
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
Experimentalize answered 17/7, 2018 at 5:12 Comment(14)
Should I add this field into release in order to see this field in BuildConfig file? Because I cannot access to it as you stated in answerBellis
You should rebuild your project probably in order to add filed in BuildConfigExperimentalize
You know, actually I had other field in BuildConfig and I changed its value from gradle and made rebuild of project but it still did not changed, why?Bellis
Paste your complete gradle file. There must be something that you are doing wrong.Experimentalize
Or you can do this. File -> Invalidate Cache / Restart then click on invalidae and restart. Android studio will restart and if you have written everything right then you will be able to get fields in buildconfigExperimentalize
what if I will add this fields by my own into BuildConfig file?Bellis
I see, you are writing some libaray. Please remember that you can't access buildConfigs of libaray into app module. That's impossible.Experimentalize
So what should I do?Bellis
you required fields in app or in libaray module?Experimentalize
in app module, I tried it to put in app gradle file and rebuild, but still same problemBellis
You must be using wrong buildConfig file. When you import BuildConfig make sure it match with the package name of your app module.Experimentalize
Thank you, it should be added to other gradle fileBellis
glad that it helped, no problem.Experimentalize
@ZeeshanShabbir "When you import BuildConfig make sure it match with the package name of your app module." My import was not correct. Thanks for the help.Behlke
A
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.

Auria answered 17/7, 2018 at 5:19 Comment(0)
B
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">
Birkett answered 24/10, 2020 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.