Enable `--multi-dex` option in ant for Android

后端 未结 3 934
后悔当初
后悔当初 2020-12-09 05:55

It\'s easy to enable multi-dex option for gradle build system, but I haven\'t found example how I can enable this option for ant building. How can archive this?

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 06:08

    I also added multidex to the ANT build, but in a slightly different fashion that is more flexible in the classes*.dex support.

    In any case, I did this in two phases: 1) get DX to output multidex then 2) use aapt to include the multidex classes. There is an optional step for producing the main classes list at the end of this posting but it is not necessary for the basic implementation.

    Here's the implementation in build.xml, with comments to follow:

    ... your build script ...
    
    
    
    
        
        
        
            
                
                    
                        
                            
                            
                        
                    
                
                
                    
                    
                        
                            
                                
                                    
                                
                            
                        
                        
                            
                                
                            
                        
                     
                
            
        
    
    
    
    
        
    
    
    
    
    
    
    
        
        
        
            
            
    
            
            
                
                    
                
                
                    
                        
                    
                
            
    
            
                
                    
                        
                            
                        
                    
                    
                        
                        
    
                        
                        
    
                        
                        
                        
    
                        
                    
                
                
                    
                    
                        
                        
                        
                    
                
            
        
    
    

    Modification (1) replaces the -package target to allow extra jars to be passed to ApkBuilder. It turns out ApkBuilder just copies the contents of the JAR files into the APK, so if we put our classes[1-N].dex into a JAR we can get ApkBuilder to package those extra jars into the APK.

    (2) Builds that extra JAR file with all classes*.dex except classes.dex, so supports any number of extra classes.dex files

    (3) Is a work around for that fact that the "dex" AntTask does not support any way to pass --multi-dex to "dx.bat", which knows how to use it. I looked at what dx.bat was invoking and added that directly here (Amended: michaelbrz has a better implementation in his script so I borrowed it. Thank you)

    BTW, we always run Proguard (either obfuscated or unobfuscated) to get class and method shrinkage. This is handy for generating a "main class" list. In our case, we added Google Play Services and that blew our DEX file method limit, so decided to put those classes and methods in the secondary dex. Generating that class list from the Proguard output is a simple matter of filtering dump.txt:

    
    
    
        
        
            
    
                
                
                    
                
    
                
                
                    
                
    
            
        
    
    

提交回复
热议问题