在项目中经常遇到这种问题,为了提高效率,实现资源利用最大化,往往把一个项目不同功能模块化,每个模块由相应的人员各自构建工程,最后进行整合。比如一个工程项目有多个模块A,B,C,每个模块各自有自己的APK生成,其中A的APK需要调用B,C的APK,那么最后整合的时候我们的问题就来了,如何才能把这多个APK打包成一个APK呢?
我们举例说明:
假如有两个APK:FatherApp,SonApp,其中,FatherApp主页面只有一个按钮,用来调用SonApp的一个页面,最后,两个APP要合并为一个APK文件递交给客户。
FatherApp 代码如下:
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/sonBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="My son APP" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fatherproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.fatherproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.fatherproject;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.sonBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
intentToSunActivity();
}
});
}
private void intentToSunActivity()
{
String action = "com.mytest"; //用于调用SonApp
Uri uri = Uri.parse("file:"+"//123456");
Intent intent = new Intent(action,uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
SonApp 代码如下:
添加一个新的Activity:SonActivity.java,其他均为默认
package com.example.sunapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class SonActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
修改AndroidMainfest.xml,实现SonActivity可以被FatherApp调用:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sunapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sunapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.sunapp.SonActivity" android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.mytest" />
<data android:scheme="file"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
以上代码就是两个相互调用的APK,接下来我们给出合并的过程:
1,点击SunApp—右键—Export—Java—JAR file—next—按下图勾选,同时填写路径,到最后finish会生成一个SunApp.jar
2,点击SunApp—右键—Properties—Android,选中Is Library---Apply
3,点击FatherApp—右键—Properties—Android,取消选中Is Library---Add—SonApp:
这里有个笔误,我就不该了,看出来的别介意呵呵,这样就成功把SonApp引用到FatherApp中了
4,在FeatherApp的mainfest.xml中添加:
<activity
android:name=".com.example.sunapp"android:label="@string/app_name"/>
这样就可以了,最后奉上代码demo连接:点击打开链接 免费下载,共勉!