Auto increment version code in Android app

后端 未结 15 1010
忘掉有多难
忘掉有多难 2020-12-07 08:22

is there a way to auto-increment the version code each time you build an Android application in Eclipse?

According to http://developer.android.com/guide/publishing/v

15条回答
  •  Happy的楠姐
    2020-12-07 08:36

    Here is the Java version for what it's worth. Also handling multiple manifests.

    String directory = "d:\\Android\\workspace\\";
    
    String[] manifests = new String[] 
    {
            "app-free\\AndroidManifest.xml",
            "app-donate\\AndroidManifest.xml",
    };
    
    public static void main(String[] args)
    {
        new version_code().run();
    }
    
    public void run()
    {
        int I = manifests.length;
        for(int i = 0; i < I; i++)
        {
            String path = directory + manifests[i];
    
            String content = readFile(path);
            Pattern         versionPattern = Pattern.compile( "(.*android:versionCode=\")([0-9]+)(\".*)", Pattern.DOTALL );
            Matcher m = versionPattern.matcher(content);
    
            if (m.matches())
            {
                int code = Integer.parseInt( m.group(2) ) + 1;
    
                System.out.println("Updating manifest " + path + " with versionCode=" + code);
    
                String newContent = m.replaceFirst("$1" + code + "$3");
    
                writeFile(path + ".original.txt", content);
                writeFile(path, newContent);
            }
            else
            {
                System.out.println("No match to update manifest " + path);
            }
        }
    }
    

提交回复
热议问题