Create multiple apk files with one project build in Android Studio

I have two options in my Android project, one works with a test server and one works with products. I store the url inside the string resource, so I can access the correct url based on the flavor that I choose to compile. Usually I need to create several apk files during the day, each time for both servers.

Is there a way to create two apk files every time I run my project or create apk from the Build menu?

+6
source share
3 answers

If you have something like this:

android { productFlavors { dev { applicationId "your.com.android.devel" buildConfigField 'String', 'HOST', '"http://192.168.1.78"' } prod { applicationId "your.com.android" buildConfigField 'String', 'HOST', '"http://yourserver.com"' } } } 

You only need to build in Gradle projects

enter image description here

And you can find all the various apks / apk builds / outputs

enter image description here

I hope that this time I will be more useful

+7
source

You can use this command line in Gradle:

 ./gradlew assemble 

Or you can independently generate all options for debugging or release accordingly

 ./gradlew assembleDebug ./gradlew assembleRelase 
+1
source

Mastering Product Flavors on Android

The only thing you need to do is define it on each of your products:

 android { productFlavors { devel { applicationId "zuul.com.android.devel" } prod { applicationId "zuul.com.android" } } } 

Send requests to multiple hosts depending on taste As before, you must include some parameters in the product taste configuration field.

 android { productFlavors { devel { applicationId "zuul.com.android.devel" buildConfigField 'String', 'HOST', '"http://192.168.1.34:3000"' } prod { applicationId "zuul.com.android" buildConfigField 'String', 'HOST', '"http://api.zuul.com"' } } } 

As an example, we will try to show you how you can integrate this using Retrofit to send a request to the appropriate server without processing the server you are pointing to and based on its taste. In this case, this is an excerpt from the Zuul android application:

 public class RetrofitModule { public ZuulService getRestAdapter() { RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(BuildConfig.HOST) .setLogLevel(RestAdapter.LogLevel.FULL) .build(); return restAdapter.create(ZuulService.class); } } 

As you can see, you just need to use the BuildConfigclass to access the just defined variable.

+1
source

All Articles