Create multiple apk files with one project build in Android Studio

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?

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

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

Hope this time I'd be more helpful


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.


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

Need Your Help

Error reading assemblies: No assembly descriptors found

assemblies maven-assembly-plugin

I get Error reading assemblies: No assembly descriptors found when building my project. I'm trying to set permissions for my .sh files and exclude a nasty .jar file that makes my application crash...