Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple targets in android studio

I am handling a project for education university ,where there are 5 seperate universities each university having there own app but in each application images ,json differs and the source code remains same . i want to create 5seperate apks with different package name using only one source code and if any change done in source code should update in all apks.

please give me structure of project ,graddle.build tool setting and if i need to make library how and where it will be, or please upload a sample to do above type in detail.

like image 848
Ajay S Avatar asked Feb 27 '15 19:02

Ajay S


1 Answers

Following Android Developers guide Configuring Gradle Builds I was able to create application sharing same code with different resources.

Firstly, after creating new project in Android Studio (Gradle: Android Module), I added to build.gradle file (residing in your module directory, like 'Project/app/build.gradle') 'blue' and 'red' flavors:

android {
// ...
    productFlavors {
        blue {
            applicationId 'com.example.app.blue'
            versionName '1.0-blue'
        }
        red {
            applicationId 'com.example.app.red'
            versionName '1.0-red'
        }
    }
}

IDE requested to sync project files with Gradle, so I did it. Then I added resource file colors.xml to 'red' flavor by right-clicking directory 'app' in project root in Project panel (New -> Android resource file, selected 'red' as my Source Set).

Next I modified new file to contain definition of color resource:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary">#ff0000</color>
</resources>

I did the same for my 'blue' flavor (but with different color value).

I changed background color of activity layout created automatically while creating new project to see if it'll work.

<RelativeLayout
    ...
    android:background="@color/primary" />

Switching Build Variant in Build Variants panel (opened by lower-left button names such) resulted in different background colors in my activity.

I assume you keep your images, json and other files in Android resurces directories, so the way you should keep different files in different flavors is similiar to what I've achieved.

See this site to better understand product flavors and Build Variants.

like image 141
Aruziell Avatar answered Oct 26 '22 13:10

Aruziell