how to specify in case of an Android local development? - android

Is there a way to tell Android we are developing and testing the application locally ? so I can write something like that in the code to execute specific code that has to be executed in case of a local development vs when the application is intended to be deployed and fetch the right remote data :
if (LOCALLY) {
MyLocalTools.xdebugIntegration();
...
}
...
And when the application is to be shared, i just have to turn a global option to mute all the local code ?

If by "local development", you mean debug builds, and by "intended to be deployed", you mean release builds, you can use BuildConfig.BUILD_TYPE to distinguish those build types, or use BuildConfig.DEBUG to distinguish between builds that are debuggable versus those that are not. Or, if you need a particular constant, use buildConfigField to add that custom field to BuildConfig.
Product flavors, cited in another answer, are designed for cases where you need two separate release builds (e.g., one with Google's in-app purchasing APIs, one with Amazon's in-app purchasing APIs).

You can use build flavor and build variants.
In your build.gradle you can define some flavors like follows:
productFlavors {
dev {
buildConfigField "String", "SERVICE_URL_BASE", "\"dev.example/rest\""
applicationId "development.example.app"
}
prod {
buildConfigField "String", "SERVICE_URL_BASE", "\"prod.example/rest\""
applicationId "com.example.app"
}
}
As you can see we have defined 2 flavor with different variables to use. In my case I have defined two different endpoints for rest services and different application id.
Then on the bottom left of Android Studio you can select which build variant use to launch your app. Build variants are the union of build flavors and build types (defaults build types are debug and release).
In your code you can access variables defined in build.gradle file like follows:
BuildConfig.SERVICE_URL_BASE
And you can access your build type with of flavor with something like this:
BuildConfig.FLAVOR
BuildConfig.BUILD_TYPE
Using these variables you can implement all the switch you desire

Related

Package naming convention Android

I have an Android App, which has two flavors: Basic and Advanced.
The Basic is already on the AppStore, with a package name of form com.domain.something. I would like to publish the second as com.domain.something.advanced. Is it possible? Or is the fact that the second is a sub package of the first would cause trouble?
Yes, this is possible and will not cause any problems in the Play Store.
You will want to read the Configure Build Variants guide for more information on setting up a product flavor for your "advanced" version. You can use the applicationIdSuffix in your build types or product flavors to set a suffix on your application ID for that particular variant.
Your build.gradle will end up looking something like this:
android {
defaultConfig {
applicationId "com.domain.something"
}
buildTypes {...}
productFlavors {
basic {...}
advanced {
applicationIdSuffix ".advanced"
}
}
}
You can then either programmatically check your product flavor with the generated BuildConfig class, or put your code for the advanced version in the advanced product flavor's source folder (/src/advanced/java, /src/advanced/res, etc.).

Android buildTypes for Alpha/Beta/Production apk

I want to release my apk on play store. Initially I will release it on alpha, then beta and if everything goes well I will release it on production. Can we define different api endpoint for each in buildType inside gradle. if yes then how? As I just want to change the end point of API I am calling throughout my application. Like if I release my apk on alpha the api that it points will be http://test.alpha.bla.bla
for beta: http://test.beta.bla.bla
for production: http://test.production.bla.bla.
so in this my all version of app (alpha/beta/production) will be having same version code without any need to upload new apk.
Thanks.
I assume that you mean you want 3 different build targets (and thus 3 different uploads to google play):
in your build.gradle you have the android part, in there you can define productFlavors like the following:
productFlavors{
alpha{
buildConfigField 'string', 'server','http://test.alpha.bla.bla'
}
}
But you could also make enums and refere those (instead of the type "string" you would have to specifiy the full package name + enum type , and in the last part (the value), the full package name + enum )
you can then reference the server by using (in java)
BuildConfig.server; //this would be http://test.alpha.bla.bla
I would put it in strings.xml. Each build variant can have its own copy with a different value.

Building two different android apps on the same codebase that only differ by const

I am building two android applications, which relay on the same codebase but differ by the server address which they grab their files from.
Till now i've created two line of my server's address constant, and builded the application once with the first const, and second with the second const.
Is there any way to make my app compile twice, once with the first constant, and second with the second one?
I am using Android studio with Gradle build.
Thank you!
You can use product flavors to teach Gradle to build two separate copies of your app, where your server address is defined in BuildConfig:
android {
// other stuff here
productFlavors {
flavorOne {
buildConfigField "String", "URL", '"https://..."'
}
flavorTwo {
buildConfigField "String", "URL", '"https://..."'
}
}
}
In your Java code, refer to BuildConfig.URL to get the URL to use.
Yeah, you can use Build Variants. You can move those strings into resources under the variants directory and the build will pull in the right one.
Here's the link to the full documentation for how to set them up: https://developer.android.com/studio/build/build-variants.html

Android: Managing different server URL for development and release

I am developing an Android application that interacts with server via REST APIs. Obviously I need to use different URL for development and release builds. Commenting and un-commenting code is very tedious and error pron.
Which is the best way to handle this situation? Using different build types in gradle file is one which could automate the process, but I am not sure if this is the right way to go.
There is also a possibility of increase in number of build types viz. test, internal-release etc.
If you are using Android Studio, use buildConfigField to add custom fields to your BuildConfig class.
buildTypes {
debug {
buildConfigField "String", "SERVER_URL", '"http://test.this-is-so-fake.com"'
}
release {
buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"'
}
mezzanine.initWith(buildTypes.release)
mezzanine {
buildConfigField "String", "SERVER_URL", '"http://stage.this-is-so-fake.com"'
}
}
Here, I have three build types: the standard debug and release, plus a custom mezzanine one. Each defines a SERVER_URL field on BuildConfig.
Then, in Java code, you just refer to BuildConfig.SERVER_URL. That field will have a value based on what build type you used to build that particular edition of the app.
It can be managed by using ProductFlavours in app build.gradle. ProductFlavours will manage different URL ie. development and release.
Please have a look it on medium.
It involves detailed explanation.
I had a similar issue and I solved it using
if (BuildConfig.DEBUG) { }
You will need to import
import com.commandsoftware.androidbookingapp.BuildConfig;
I had a similar problem with writing to logcat. I wanted to write all the messages if the app was signed with the debug key, otherwise write almost none of them. I solved the problem with this line of code:
boolean showAllMessages = ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
then using that boolean in my log writer. You should be able to do something similar when you initialize the URIs.
I am using Eclipse. I can't say with certainty that this will work in other IDE environments. This answer implies that it might be an Eclipse-only feature

How to define static String values in Android that change for test configurations with Gradle?

Hey I am trying to statically define String values that change according to the configuration I am running. So if I run a test configuration, it uses the test API url, but if I run a regular build, it statically sets the real API URL.
I am using two strings files right now, one in the main folder and one in the androidTest folder in Android Studio. This works well for getting different Strings per configuration, but I'de like to do it statically rather than dealing with Resource fetches.
Is this possible?
I have seen this answer for ANT, but I am not sure how to do it with Gradle.
You can generate gradle constants like this:
build.gradle
android {
buildTypes {
debug {
buildConfigField "String", "FOO", "\"foo\""
}
release {
buildConfigField "String", "FOO", "\"bar\""
}
}
}
And access them in your code through BuildConfig.FOO
Note you may need to clean and/or restart your IDE for the to come in to effect.

Categories

Resources