Creating template app for multiple android apps

不问归期 提交于 2019-12-23 04:49:10

问题


So here is the thing. I want to create few apps, which they all should look exactly the same, but only one variable should change. I'm looking for a way to create one library and to import it in all the other apps, and instead of updating a lot of apps, I would be able to update just the library.

Any ideas how such thing can be accomplished? Maybe using meta-data?

Thanks


回答1:


In addition to CommonsWear's answer, which is correct if you are using Android Studio, what you require can also be achieved in Eclipse - albeit with a little more work.

You'd need to create a 'library application' (see image below) which should be your base application - i.e the app you want to be the common codebase of all the children-apps.

After that is completed and you should create a new Android project (a child app) that uses the base application library (see example image below)


(source: vogella.com)

Any values in a child app (i.e. strings, styles, etc) are inherited from the parent library app /res folder unless you specify new values in the child app /res folder.

e.g. if libraryapp/res/strings.xml contains

<string name="override_me">Hello</string>

and childapp/res/strings.xml contains <string name="override_me">Bonjour</string>

then running the child app will return "Bonjour" rather than what was defined in the library.

As the child app is basically an empty project without activities, your child app's manifest should basically be a carbon copy of that of the library apart from the <manifest> tag which should contain the child app's package, android:versionCode and android:versionName. It can launch your library with these modified values like this:

 <activity
        android:name="com.example.library.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Being Eclipse, this is slightly more convoluted but if you manage to set up correctly, it makes creating child apps quick and easy (but just not as easy as with gradle!).




回答2:


Create one app using Android Studio and Gradle for Android. Create multiple product flavors for that app. Use buildConfigField to define the "variable" that changes based upon the flavor. When you build for your flavor, you get the app with the variable definition that you seek, along with anything else that you overrode in that product flavor (e.g., icon, app_name).



来源:https://stackoverflow.com/questions/27706775/creating-template-app-for-multiple-android-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!