Building many similar Android .apk files at the sa

2020-07-11 04:32发布

问题:

I've got a single Android project which I need to build many .apk's from. The .apk's differ from each other in only a few ways:

  1. there are a few values in the Android manifest file that differ from each other
  2. some of the .apk's might exclude some files in the /res folder
  3. different package name

What is the best way to build all these different .apk's automatically?

Thanks!

回答1:

Use Android Maven plugin. It supports android library projects.

  1. Create a multi-module project.
  2. Put all common classes in one module (library).
  3. Create a module for each app you want to distribute and make it depend on lib module.

Here is an example: https://github.com/jayway/maven-android-plugin-samples/tree/master/libraryprojects

(You will need to create several 'libraryprojects-mainapp' modules)



回答2:

If you use git you can make different branches and checkout each one before building. You can also write a shell script to automate this if you are on a Mac or Linux.

Development can for example be done on an integration branch that will be merged into your three different branches.

I don't know if this makes it any easier for you, but it would be one way.



回答3:

2011 is a long time ago.. but it seems that there is no Eclipse/Netbeans solution on this. I faced the same problem recently on a cordova project on Netbeans.

On bleeding edge Android Studio, there is a convenient solution called "flavours".

It's based on mirrored directories hierarchy. A "main" directory hierarchy that contains all files of a "main" build, and "flavours" directories hierarchies, each flavour directory contains files that will overwrite or complement those "main" ones at build time.

For my use (I can't migrate to Android Studio), I wrote a simple ant script to imitate that feature. It works on Netbeans and Eclipse, and I think it's some sort of project-independent.

For using it, the full project folder have to step back one level of hierarchy, and the original one needs to be placed on "main" directory. A build directory must be created, and multiple "flavors" folders be put inside "flavors", as this:

├── build.xml                           < - ant script file
├── main                                < - original project
├── flavors
│     ├── freeVersion                   < - files related with a freeVersion app
│     └── paidVersion                   < - files related with a paidVersion app
└── build                               < - temporary build folder

Running the script ($ ant change-flavor), it will ask witch flavor directory you want to build. After user input, it checks flavor directories existence, and copy all main directory into build folder, plus eventual "flavors" files, overwriting the "main" ones.

The resultant build folder is a complete new native Android/Cordova/whatever project, that can be normally compiled through the IDE.

<?xml version="1.0" encoding="UTF-8"?>

<project name="Flavors" basedir="." >

<property name="flavors.dir" value="flavors"/>
<property name="flavors.build.dir" value="build"/>
<property name="flavors.main.dir" value="main"/>

<target name="change-flavor">
    <input message="Witch Flavor?" addproperty="flavor.dir" />

     <fail message="Empty flavor not allowed">
         <condition>
                 <equals arg1="${flavor.dir}" arg2=""/>
         </condition>
    </fail>     

    <fail message="Directory ${flavors.dir}/${flavor.dir} not exists">
        <condition>
            <not>
                <available file="${flavors.dir}/${flavor.dir}" type="dir" />
            </not>
        </condition>
    </fail>

    <echo message="Deleting build dir ${flavors.build.dir}"/>
    <delete includeemptydirs="true">
        <fileset dir="${flavors.build.dir}" includes="**/*"/>
    </delete>

    <echo message="Copying from main ${flavors.build.dir}"/>
    <copy todir="${flavors.build.dir}" includeemptydirs="true" >  
            <fileset dir="${flavors.main.dir}" includes="**"/>  
     </copy>        

    <echo message="Copying from flavor ${flavors.build.dir}"/>
    <copy todir="${flavors.build.dir}" includeemptydirs="true" overwrite="true" >
            <!-- exclude folder is here because my flavors directories are also netbeans
                 projects. If similar approach is used on eclipse, maybe put here .project 
                 and .settings folders -->
            <fileset dir="${flavors.dir}/${flavor.dir}" includes="**" excludes="nbproject/**"/>
     </copy>        

    </target>    
</project>

There is a penalty time for each flavor build, because there is no pre-compiled stuff, but in my case, I think its worth, since the only things that changes from a flavor to another are properties files and assets. Most of development process can be done on "main" project, flavors are just skins. Beside that, it avoids intromission on cordova own build system/netbeans integration.