Multiple .pk application files for Android applications from one source code

I would like the Android build system, command line or Eclipse to create multiple .apk files from the same source code base. Some common reasons for this are the availability of specific versions for markets with different requirements or the free and paid versions.

This question is NOT ABOUT:

Google says, β€œYou probably need to create separate Android projects for each APK that you intend to publish so that you can develop them separately accordingly. You can do this by simply duplicating your existing project and giving it a new name.” Then they kindly suggest using libraries that I understand. Then they mention exactly what I want: "an assembly system that can output different resources based on the assembly configuration"

  • I know that to execute conditional compilation in JAVA, you can disable the variable "public static final". There is an example of setting this value in the build.xml file. Any more complete example of Android Ant build configuration for this, or a link to an OSS project running it now, please? BTW, build.xml is automatically generated, but I saw people crack it, so how does it work?

  • With the package name declared in Manifest.xml as package = "com.example.appname", if you need to emit several .apks that change this name, stuck with a separate project for each?

+48
android ant apk
Sep 21 2018-11-11T00:
source share
10 answers

I generate 2 different APKs (demo and production) from the same source tree with three small modifications:

1) I have public static final DEMO=true; //false; public static final DEMO=true; //false; in my Application class and depending on this value I used to switch code between demo / production functions

2) There are two main activities:

 package mypackage; public class MyProduction extends Activity { //blah-blah } package mypackage.demo; public class MyDemoActivity extends mypackage.MyProductionActivity { //blah-blah } 

3) And at the end of 2 separate AndroidManifest.xml files that indicate different launch actions depending on the demo / production switch

I switch between the two APKs manually, but I see nothing difficult in writing a small ANT task to automatically switch between them.

+11
05 Oct '11 at 16:24
source share

One way to do this is to maintain two separate AndroidManifest.xml, one for each configuration. You can switch between them either manually (copying) or automatically (build script).

[edit] This person here has a system to do such things: http://blog.elsdoerfer.name/2010/04/29/android-build-multiple-versions-of-a-project/

+10
Oct 03 2018-11-11T00:
source share

The answer to this shout is Gradle , as described on this website . It is officially built into Android Studio and is recommended.

It's amazing; I created 3 separate applications using the same source code, with custom text and graphics without special coding. Only some directory and Gradle need to be installed, and other messages can be found with answers to both.

It seems that all the basics are very well explained. To answer your specific question, find the Product Flavors section in the Build Variants section for a description of the various flavors.

As the site explains, part of the goal behind this design was to make it more dynamic and more easily allow multiple APKs to be created using essentially the same code that sounds exactly like you.

I probably didn’t explain it best, but this website does a pretty good job.

+5
Apr 21 '14 at 16:02
source share

Despite your insistence that this is not about packing common code into Android libraries, it's kind of like. You have stated that markets may have different requirements or have a free and paid version. In each of these examples, your two final output APKs have different behavior and / or resources. You can put the vast majority of your code into the Android shared library, and then save the differences in your actual projects.

For example, I worked on applications where they need to be released both on the Android Market and in the Amazon AppStore. The Amazon AppStore requires that if you link to the market page for the application, it must be Amazon (as opposed to the Android Market page). You can save the URL in a resource in the library and use it in your code, but then redefine that resource in your Amazon project to point to the corresponding Amazon URL.

If you structure it correctly, you can do similar things in code, because your starting point is your application object, which you can subclass and do different things.

However, if you want to add an Ant step that changes the package name in the manifest, it is just XML. This should not be difficult to change as a precompilation step.

+3
Sep 21 '11 at 23:25
source share

This article has a good idea on how to modify configuration files during build; see, in particular, the sections Setting up the layout and using the Java configuration file . Please note that some information about build.xml and ant is a bit outdated now.

+2
04 Oct 2018-11-15T00:
source share

I had the same problem, but packing in just one project with flags is not for me. I wrote an example how to do this with Maven:

How to create several apk files for Android from one code base organized by the Maven multi module project.

+2
Apr 14 '13 at 13:33
source share

My team will build 2 different assemblies using a single code base + additional code. Since Android build is based on ant script, I use ant script to do this work.

I used xmltask to manipulate the manifest XML file and many ant tasks (regexp, copy ..) to edit the source code.

I prepared a project template template (including build.xml, default.properties, local.properties) and copied the new source code into these project templates. when the copy is complete, run build.xml in parallel to reduce build time. when the build is complete, I get several apk files.

+1
Oct 06 2018-11-11T00:
source share

Here is our situation: we have one code base from which we release for several clients. Each of them has different requirements regarding names, background and other resources in the application (not to mention package names).

The assembly is handled by a Ruby script that modifies AndroidManifest , copies / replaces certain resources from client folders, and then switches to the standard Android build procedure. After the build is completed, the script resets the modified files back to their original default state.

Well ... Maybe this is not optimal and definitely not dependent on Android, but how do we do it.

0
04 Oct 2018-11-11T00:
source share

I think that it is best to use libray for shared sources and two different Android projects for a demo and production package. This is because in Java it is very simple to do reverse processing from apk to sources. If you use the same sources for demonstration and production, someone can crack your apk by downloading the demo package, extracting java sources from it and unblocking the sources that modify the variable to use as a production version. Using the library, you can save some of the sources in the production package, so there is no way to use the demo package as a production package.

0
Apr 16 '13 at 21:11
source share

It's easy to achieve your goal using Android Studio build options that use graddle as the build system.

Read more about here for more details.

0
Dec 20 '14 at 16:25
source share



All Articles