Reputation: 213
I am planning to release a paid version of my free android app using the android library project approach. http://developer.android.com/tools/projects/index.html
My app has several stand-alone (non-user interface) classes and resources that are easy to reuse between the paid and free version.
My question is about the best way to manage the user interface logic (code in Activity classes). Let's say my free app has one button and my paid app has two buttons in the same activity. Is the best way to achieve this is to have the following setup?
1)Layout with one button
2) an Ativity.java file containing logic for when the button is clicked
Use layout and source code from the library project
1) A new layout file with two buttons
2) A new Activity.java which has the exact same code for handling button1 clicks and new code for handling button2 clicks.
This does not seem right because button1's logic in paid app seems to be a wasteful copy ... Is there a better way to do this?
Upvotes: 0
Views: 375
Reputation: 1005
You can make a single project library with all the functionality
Just you need a one method that can identify that if application is paid or free
For that follow the steps
1) Create a new application suppose testFree
2) Create a new Application Class as follow in the library project
package com.example.testlib;
import android.app.Application;
public class App extends Application{
private static App mInstance;
public App() {
mInstance = this;
}
public static App getInstance() {
return mInstance;
}
public boolean isFree()
{
return true;
}
}
3) create a new application suppose testPaid
4) create a new Application class in the testPaid Application as Follow
package com.example.testpaid;
import com.example.testlib.App;
public class AppPaid extends App {
@Override
public boolean isFree() {
// TODO Auto-generated method stub
return false;
}
}
5) set Application name on testFree app to Application class that we created on the library class and also set main and launcher activity from library class
<application
android:name="com.example.testlib.App"
....
<activity
android:name="com.example.testlib.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
6) now set the application name to create application class of testPaid app and also main and launcher activity from library project as follows
<application
android:name="com.example.testpaid.AppPaid"
....
<activity
android:name="com.example.testlib.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
7) All you have set now in any class of library project you have a method that will check if you app is free or paid you can check as following way and based on that you can make visible some paid functionality to paid app and some free to free
if(App.getInstance().isFree())
{
Toast.makeText(getActivity(), "Free App", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(), "Paid App", Toast.LENGTH_SHORT).show();
}
Let me know if you still find any problem.....
Upvotes: 1