Home » Android » Create multiple apk files with one project build in Android Studio

Create multiple apk files with one project build in Android Studio

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have two flavors in my Android project, one works with test server and one with production. I store the URL inside a string resource, so I may access the correct URL based on a flavor that I choose for compilation. Usually I need to create multiple apk files during a day, each time for both servers.

Is there a way to create two apk files each time I run my project or build an apk from Build menu?

How to&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 have to run assemble in Gradle projects

enter image description here

And you can find all the different apks build/outputs/apk

enter image description here

Hope this time I’d be more helpful

Answer:

Mastering “Product Flavors” on Android

The only thing you have to do is to define it on each of your product flavors:

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

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

Send requests to multiple hosts depending on the flavor
As before, you must include some params on your product flavor config 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 with Retrofit to send request to the appropiate server without handling which server you’re pointing and based on the flavor. In this case this is an excerpt of the Zuul android app:

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 have to use the BuildConfigclass to access the variable you’ve just defined.

Answer:

You can use this command line in Gradle:

./gradlew assemble

Or you can generate saparately all flavors for debug or release respectively

./gradlew assembleDebug
./gradlew assembleRelase