Android - How do I dynamically set the package name at build time for an Open-source project?

后端 未结 3 1783
天命终不由人
天命终不由人 2020-12-24 15:30

I\'m working on an Open-source project. As it is intended that anyone can download the source and build it themselves, I do not want to hard-code the package name anywhe

3条回答
  •  情深已故
    2020-12-24 16:03

    I did something similar (but not for this reason) which required updating the manifest at build time. The way I accomplished this was by making a second AndroidManifest and putting it under a directory named config. So in config/AndroidManifest you could have something like this:

    
    
    
    
    
    
    
    

    Then you can use the regular bare bones build.xml ant script with just a few modifications (no need to copy the whole script from the android build system as they added some hooks for you to use without reinventing the wheel). The build script should be reading local.properties by default, but if not add (or uncomment) a line like this:

    
    

    In your build script you should see a task called "-pre-build", change it like this:

    
         
           
              
              
              
           
                    
    
    

    Then your local.properties file you would put the package name, version name/code like so:

    app.version=1.0
    app.versioncode=1
    app.packagename=com.mypackage.name
    

    Now you just need to make sure in your manifest that you fully qualify all of your activities/services/broadcast listeners etc.. That means you always specify the full package of your source code. If you want the package for your own source code to be dynamic you could replace out each of the prefixes to each class.. But that seems kind of silly.. It is easy enough to package your code up under your own package name and they can use it from any project by simply including the source or a jar in their project.

    -- UPDATE -- Oh and one other thing you can do to notify the user that they must define a package name is use the fail tag in your build xml like this:

    
    

    Put this after the line which reads the local.properties file

提交回复
热议问题