android 中如何将多个相互关联的APK打包成一个APK?

在项目中经常遇到这种问题,为了提高效率,实现资源利用最大化,往往把一个项目不同功能模块化,每个模块由相应的人员各自构建工程,最后进行整合。比如一个工程项目有多个模块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,在FeatherAppmainfest.xml中添加:

<activity

android:name=".com.example.sunapp"android:label="@string/app_name"/>

这样就可以了,最后奉上代码demo连接:点击打开链接 免费下载,共勉!


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trent1985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值