AS中并没有独立的Module 工程,但是可以在普通的Project中加入Module。所谓的Module就是我们通常所指的模块化的一个单元,并常常以jar包的形式存在。下面以一个获取手机信息的例子演示AS中的模块化。
## 一、项目中新建Module
File—>New Module,详细见下图。
![](https://box.kancloud.cn/2016-01-01_5685d1973a461.jpg)
![](https://box.kancloud.cn/2016-01-01_5685d197503fd.jpg)
![](https://box.kancloud.cn/2016-01-01_5685d19761842.jpg)
## 二、新建Java类
新建一个PhoneInfo类,内容如下:
~~~
package com.linc.mylibrary;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Created by linc on 15-3-19.
*/
public class PhoneInfo {
private String TAG = "PhoneInfo";
private Context mContext;
private TelephonyManager mPhoneManager;
public PhoneInfo(Context context) {
mContext = context;
mPhoneManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
}
public String getDeviceId() {
return mPhoneManager.getDeviceId();
}
public String getPhoneModule() {
return Build.MODEL;
}
public String getSerialNumber() {
return Build.SERIAL;
}
public String getPhoneNumber() {
return mPhoneManager.getLine1Number();
}
public String getMacAddress(){
String result = "";
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
result = wifiInfo.getMacAddress();
Log.i(TAG, "macAdd:" + result);
return result;
}
public String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String[] cpuInfo = {"", ""}; //1-cpu型号 //2-cpu频率
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i < arrayOfString.length; i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
}
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo[1] += arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
}
Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);
return cpuInfo;
}
public String getTotalMemory() {
String str1 = "/proc/meminfo";// 系统内存信息文件
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
arrayOfString = str2.split("\\s+");
for (String num : arrayOfString) {
Log.i(str2, num + "\t");
}
initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
localBufferedReader.close();
} catch (IOException e) {
}
return Formatter.formatFileSize(mContext, initial_memory);// Byte转换为KB或者MB,内存大小规格化
}
}
~~~
并在Module的AndroidManifest文件中加入两个权限:
~~~
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
~~~
## 三、app中引入此module
在app的build.gradle中加入此module的dependency,如下:
~~~
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':mylibrary')
}
~~~
在app的MainActivity中加入测试代码:
~~~
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PhoneInfo info = new PhoneInfo(this);
Log.d(TAG,"devices id: "+info.getDeviceId());
Log.d(TAG,"getPhoneModule: "+info.getPhoneModule());
Log.d(TAG,"getSerialNumber: "+info.getSerialNumber());
Log.d(TAG,"getPhoneNumber: "+info.getPhoneNumber());
Log.d(TAG,"getMacAddress: "+info.getMacAddress());
Log.d(TAG,"getCpuInfo: "+info.getCpuInfo());
Log.d(TAG,"getTotalMemory: "+info.getTotalMemory());
}
~~~
## 四、jar的生成
项目编译之后jar会在下面的目录找到:
~~~
./mylibrary/build/intermediates/bundles/debug/classes.jar
./mylibrary/build/intermediates/bundles/release/classes.jar
~~~
## 五、Module的移除
先要在File—>Project Structure中将此module“减“掉后才能在项目中Module右键的Delete键可用。
![](https://box.kancloud.cn/2016-01-01_5685d19776b4f.jpg)
参考:
[http://www.cnblogs.com/wuya/p/android-studio-gradle-export-jar-assets.html](http://www.cnblogs.com/wuya/p/android-studio-gradle-export-jar-assets.html)
[http://www.cnblogs.com/helloandroid/articles/2210334.html](http://www.cnblogs.com/helloandroid/articles/2210334.html)
[http://blog.csdn.net/hyr83960944/article/details/37519299](http://blog.csdn.net/hyr83960944/article/details/37519299)
- 前言
- 一:文本与布局
- 二:组合控件
- 三:性能测试类
- 四:语音识别
- 五:读取Excel
- 六:PreferenceActivity使用详解
- 七:按钮控制ViewPager的左右翻页
- 八:Ubuntu下切换JDK版本
- 九:最新Android开发环境(Eclipse+ADT+Android 5.0)
- 十:获得屏幕物理尺寸、密度及分辨率
- 十一:Android Studio和Gradle
- 十二:Android Studio导入第三方类库、jar包和so库
- 十三:APK签名
- 十四:混淆与反编译
- 十五:多分辨率适配常用目录
- 十六:getprop与dumpsys命令
- 十七:Linux下的模拟器硬件加速
- 十八:adb取出安装在手机中的apk
- 十九:android studio导出jar包(Module)并获得手机信息
- 二十:两个开源的图表/报表控件
- 二十一:Android原型设计工具探索
- 二十二:Android 5.1 SDK下载与配置
- 二十三:Android Studio的NDK开发
- 二十四:横竖屏切换
- 二十五:模拟器如何重启?试试Genymotion!
- 二十六:persistableMode与Activity的持久化
- 二十七:Maven编译开源二维码扫描项目zxing