Reputation: 1167
In my android application, I'm trying to run two sets of code so I can automatically change an advertising ID when I'm using Android Studio.
One for when I'm debugging in Android Studio, and one for when I build the .apk to upload to the play store.
I've been using if (BuildConfig.DEBUG)
, but how can I be sure that that will only be true when I'm actually debugging in Android Studio, and when I build the signed .apk it will be set to false? Where should I be configuring Gradle to know what build type to use, or is this automatically done when clicking Run?
Manifest.xml
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
debuggable true
}
}
Upvotes: 4
Views: 1989
Reputation: 1944
You can set the variant (debug or release) that you want to build by selecting it in the build variants
tab. Once the build is completed, you can confirm the variant by going to app/build/generated/source/buildConfig/debug/your/package/name/BuildConfig
Here you will see this line:
public static final boolean DEBUG = Boolean.parseBoolean("true");
When you create a release variant, you will have
public static final boolean DEBUG = Boolean.parseBoolean("false");
in app/build/generated/source/buildConfig/release/your/package/name/BuildConfig
Upvotes: 2
Reputation: 17854
When you generate an APK, the last screen will have a dropdown that lets you choose your build type. It defaults to "Release" but you can change it to "Debug."
That's how the APK is made a debug or release APK.
Basically, if you're running the app with the play button in the top right or with Shift+F10, it'll be built as a debug APK. If you generate a signed APK, it'll be a release APK.
Upvotes: 2