Ant构建并管理Android项目

使用Ant构建并管理Android项目

一.背景:

最近刚刚完成将公司的主项目从Eclipse迁移到Android Studio的工作,但还是有部分老项目需要在Eclipse下进行开发、维护,本文记录一下使用Ant对老项目进行管理的经验。
接到一个开发sdk的任务,要求提供demo测试该sdk,该任务已由他人在eclipse上完成了一部分,且尚未建立基线、只能从本地导出jar包和apk.为了便于管理,使用ant脚本对该项目进行构建管理。

二.效果:

最终效果如下图:执行ant脚本自动生成jar或apk到指定release目录

最终效果

由于暂时还不能走发布系统打版,且需要不断测试,而每次手动导jar和apk耗时且不便对提测的jar和apk进行有效的管理。使用ant脚本自动化构建则能有效地解决这一问题。

三.demo源码(文末附github链接):

1. 库工程

AssetManager.java

package com.tobenull.antlibrary;

import android.content.Context;

public class AssetManager {
   

    private static AssetManager mInstance = new AssetManager();

    private AssetManager() {

    }

    public static AssetManager getInstance() {
        return mInstance;
    }

    public void init(Context context) {
        Config.getInstance().init(context);
    }

    public String getName() {
        return Config.getInstance().getName();
    }

    public String getTime() {
        return Config.getInstance().getTime();
    }
}

Config.java

package com.tobenull.antlibrary;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import android.content.Context;

public class Config {
   

    private static Config mInstance = new Config();

    private Config() {

    }

    public static Config getInstance() {
        return mInstance;
    }

    public void init(Context context) {
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open("antlibrary.properties");
            properties.load(inputStream);
            setName(properties.getProperty("name"));
            setTime(properties.getProperty("time"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String name;
    private String time;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

antlibrary.properties

name=tobenull
time=20170316

2.主工程

MainActivity.java

package com.tobenull.antmain;

...

public class MainActivity extends Activity {
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
               AssetManager.getInstance().init(getApplicationContext());
        ((TextView) findViewById(R.id.test))
                .setText(AssetManager.getInstance().getName() + "\n" + AssetManager.getInstance().getTime());
    }
    ...
}

activity_main.xml

<RelativeLayout 
    ... >

    <TextView
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值