Search code examples
androidiosandroid-studiomoduletargets

Creating Targets in Android Studio like in iOS after mistakenly creating a application rather than library


I was trying to make my already existing app as a base code for my other targets (applications) that i wanted to release. It took me some time in learning the process. I did that and i want to help people by answering it myself. Its straight forward in iOS and seemed rough in android studio , but eventually turned to be insanely simple.

Problem Statement:

  1. Application is created and you want to use it as a library so that code repetition is minimal. OR
  2. You want to add two or more apps with small changes and basically the same code.i.e you want to add modules that use same basic functionality code(layouts , string files, themes, assets etc)

Solution

  • Go to the Project toolbar in android studio.

    Select Androidview

    You will see Gradle Script in the tree

    Their you will find two build script files, one with Project name and another with module name.

    Say for example module name is app (which by default is) double click on it

    You will see apply plugin: 'com.android.application' change it to apply plugin: 'com.android.library'

    Gradle Sync your project.

    Also remove the ` applicationId "core.example.something" tag from it , Sync again.

    Now right click on the project and add new module, You will see a window with different modules select Phone AND tablet (in my case) and follow the on screen instructions.

    A new module will be added to your project structure.

    Copy paste the android manifest from the app module into this new module.

    Check the Gradle Script tree, you will find a new entry in it, open it and add compile project(":lib") in the dependency (replace lib by app in my case or the module name that you changed into library)

    Now go back to the android manifest file of newly added module. see for errors (more of them are gone by now)and add the new package name that you want to release this code as. keep adding modules :D