简体   繁体   中英

How to generate multiple android apk with multiple set of source codes using gradle

recently I am working on an android project, where there's a set of resource as src, res, assets, etc. Now there's a requirement that I need to generate multiple apks using these resources. And I need to change a little bit for every source files for each apk. For example, to build apk one. in the original AndroidManifest.xml there's a package name field "com.hello.app", I need to replace it to "com.hello.app.one". And in the original java code like "HelloApp.java" there's something like "com.hello.app", I need to rename it as "HelloAppOne.java" and replace "com.hello.app" to "com.hello.app.one". The similar happens for apk two

The solution I come up with is to first copying all the original codes and Manifest.xml to one place for each apk using Gradle copy task. Then rename and replace the file name and tokens. This part works.

But next when I try to build the apks using different sets of code I generated, it turns out something was wrong.

Here's the gradle script I come out.

android{
    productFlavors {
        One{
            def appFolders = 'build/cache/AppOne';
            applicationId = 'com.hello.app.one'
            sourceSets{
                main{                
                    manifest.srcFile appFolders + File.separator + 'AndroidManifest.xml'
                    java.srcDirs = [appFolders+'/src']
                    res.srcDirs = [appFolders+'/res']
                    assets.srcDirs = [appFolders+'/assets']
                }
            } 
        }        
        Two{
            def appFolders = 'build/cache/AppTwo';
            applicationId = 'com.hello.app.two'
            sourceSets{
                main{                
                    manifest.srcFile appFolders + File.separator + 'AndroidManifest.xml'
                    java.srcDirs = [appFolders+'/src']
                    res.srcDirs = [appFolders+'/res']
                    assets.srcDirs = [appFolders+'/assets']
                }
            } 
        }  

    }
}

The directory layout is like

build.gradle
AndroidManifest.xml
src/
   com.hello.app/
res/
assets/
build/cache/
   AppOne/
      src/
         codes for apk one
      res/
      assets/
      AndroidManifest.xml (modified for apk one) 
   AppTwo/
      src/
         codes for apk two
      res/
      assets/
      AndroidManifest.xml (modified for apk two)    

The files in folders AppOne and AppTwo are generated by Gradle copy task.

When I build, I can get APKs for each product flavor. When I install apk one and apk two on a device I found that these two are are exactly the same. APK one has the exactly the same name, provider with APK two.

Can somebody help me? What's wrong in my gradle scripts? or Is there other way to achieve my requirements? Thank you!

I have no experience with this feature, but it looks like your 'manifest.srcfile' line is missing an =. Should it be: manifest.srcFile = appFolders + File.separator + 'AndroidManifest.xml'

HTH, Jim

Make your structure like this:

build.gradle
src/
   main/
       java/com.hello.app
       assets
       res
       AndroidManifest.xml
   One/
       java/com.hello.app
       assets
       res
   Two/
       java/com.hello.app
       assets
       res

so, the idea is to name your source sets like the product flavors , that way when you build your app with assembleOne (for example) you'll get code from main and One source sets. Use main for code that needs to be shared, and One , Two for flavor-unique code. You can probably also omit the manifest in main and place a unique manifest in each flavor source set, if that's what necessary.

Furthermore, you can use the buildConfigField and manifestPlaceholders directly from your build.gradle to inject custom values into the manifest or BuildConfig.java class at build time.

Hint : if using Android Studio, find a build variants option (left-bottom corner of the window), when you switch them you can see which source set is currently selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM