Skip to content

Commit

Permalink
动态加载框架Project提交
Browse files Browse the repository at this point in the history
  • Loading branch information
yb.wang authored and yb.wang committed Nov 4, 2015
1 parent 81bb85a commit 9ef50d7
Show file tree
Hide file tree
Showing 84 changed files with 4,583 additions and 0 deletions.
Binary file added aapt_linux
Binary file not shown.
Binary file added aapt_mac
Binary file not shown.
Binary file added aapt_win.exe
Binary file not shown.
10 changes: 10 additions & 0 deletions apk_module_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<ApkModules>

<Module packageName="ctrip.android.demo1" resourceId="0x31"/>

<Module packageName="ctrip.android.demo2" resourceId="0x36"/>



</ApkModules>
52 changes: 52 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
//jcenter()
maven { url "http://mirrors.ibiblio.org/maven2"}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

ext{

This comment has been minimized.

Copy link
@dhanju

dhanju Nov 5, 2015

nice


//这是工程根目录
ctripRoot = project(":").projectDir

// local.properties 来自于AS自动创建
// 可以保存所有本地工程配置
// 不允许上传git库
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

// 从系统环境变量中或者local.properties配置文件中读取SDK位置
if(System.getenv("ANDROID_HOME")!=null){
sdkDir = System.getenv("ANDROID_HOME")
}
else{ // 在local.properties中定义
sdkDir = properties.getProperty('sdk.dir')
}

//Debug代码:开发人员可以手动改为false,这样工程就是标准Android工程,可供开发调试。
//改为true,请使用 gradle assembleRelease bundleRelease repackAll 命令打出多apk的release包。
solidMode = true
//可以在local.properties里修改这个值。添加一行 solidMode=false 即可。
solidModeConfigValue = properties.getProperty('solidMode')
if('true'.equalsIgnoreCase(solidModeConfigValue)){
solidMode = true
}
else if ('false'.equalsIgnoreCase(solidModeConfigValue)){
solidMode = false
}
}

allprojects {
repositories {
jcenter()
}
}
1 change: 1 addition & 0 deletions bundle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
7 changes: 7 additions & 0 deletions bundle/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ctrip.android.bundle">

<application android:allowBackup="true" android:label="@string/app_name">

</application>

</manifest>
5 changes: 5 additions & 0 deletions bundle/assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

13 changes: 13 additions & 0 deletions bundle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apply plugin: 'com.android.library'
apply from: '../global_config.gradle'
version "1.0"
android {
defaultConfig {
versionCode 1
versionName project.version
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
17 changes: 17 additions & 0 deletions bundle/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/yb.wang/Downloads/android_SDK/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
3 changes: 3 additions & 0 deletions bundle/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">bundle</string>
</resources>
18 changes: 18 additions & 0 deletions bundle/src/ctrip/android/bundle/framework/Bundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ctrip.android.bundle.framework;

import java.io.InputStream;

public interface Bundle {


long getBundleId();


String getLocation();


int getState();


void update(InputStream inputStream) throws BundleException;
}
190 changes: 190 additions & 0 deletions bundle/src/ctrip/android/bundle/framework/BundleCore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package ctrip.android.bundle.framework;

import android.app.Application;
import android.content.res.Resources;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import ctrip.android.bundle.hack.AndroidHack;
import ctrip.android.bundle.hack.SysHacks;
import ctrip.android.bundle.log.Logger;
import ctrip.android.bundle.log.LoggerFactory;
import ctrip.android.bundle.runtime.BundleInstalledListener;
import ctrip.android.bundle.runtime.DelegateResources;
import ctrip.android.bundle.runtime.InstrumentationHook;
import ctrip.android.bundle.runtime.RuntimeArgs;

/**
* Created by yb.wang on 15/1/5.
* Bundle机制外部核心类
* 采用单例模式封装了外部调用方法
*/
public class BundleCore {

public static final String LIB_PATH = "assets/baseres/";
protected static BundleCore instance;
static final Logger log;
private List<BundleInstalledListener> bundleDelayListeners;
private List<BundleInstalledListener> bundleSyncListeners;

static {
log = LoggerFactory.getLogcatLogger("BundleCore");

}

private BundleCore() {
bundleDelayListeners = new ArrayList<BundleInstalledListener>();
bundleSyncListeners = new ArrayList<BundleInstalledListener>();
}

public static synchronized BundleCore getInstance() {
if (instance == null)
instance = new BundleCore();
return instance;
}


public void ConfigLogger(boolean isOpenLog, int level) {
LoggerFactory.isNeedLog = isOpenLog;
LoggerFactory.minLevel = Logger.LogLevel.getValue(level);
}

public void init(Application application) throws Exception {
SysHacks.defineAndVerify();
RuntimeArgs.androidApplication = application;
RuntimeArgs.delegateResources = application.getResources();
AndroidHack.injectInstrumentationHook(new InstrumentationHook(AndroidHack.getInstrumentation(), application.getBaseContext()));
}

public void startup(Properties properties) {
try {
Framework.startup(properties);
} catch (Exception e) {
log.log("Bundle Dex installation failure", Logger.LogLevel.ERROR, e);
throw new RuntimeException("Bundle dex installation failed (" + e.getMessage() + ").");
}
}

public void run() {
try {

log.log("run", Logger.LogLevel.ERROR);
for (Bundle bundle : BundleCore.getInstance().getBundles()) {

BundleImpl bundleImpl = (BundleImpl) bundle;
try {
bundleImpl.optDexFile();
} catch (Exception e) {
e.printStackTrace();
log.log("Error while dexopt >>>", Logger.LogLevel.ERROR, e);
}
}
notifySyncBundleListers();
DelegateResources.newDelegateResources(RuntimeArgs.androidApplication, RuntimeArgs.delegateResources);

} catch (Exception e) {
Log.e("Bundleinstall", "Bundle Dex installation failure", e);
throw new RuntimeException("Bundle dex installation failed (" + e.getMessage() + ").");
}
System.setProperty("BUNDLES_INSTALLED", "true");
}


private void notifyDelayBundleListers() {
if (!bundleDelayListeners.isEmpty()) {
for (BundleInstalledListener bundleInstalledListener : bundleDelayListeners) {
bundleInstalledListener.onBundleInstalled();
}
}

}

private void notifySyncBundleListers() {
if (!bundleSyncListeners.isEmpty()) {
for (BundleInstalledListener bundleInstalledListener : bundleSyncListeners) {
bundleInstalledListener.onBundleInstalled();
}
}
}

public Bundle getBundle(String bundleName) {
return Framework.getBundle(bundleName);
}

public Bundle installBundle(String location, InputStream inputStream) throws BundleException {
return Framework.installNewBundle(location, inputStream);
}


public void updateBundle(String location, InputStream inputStream) throws BundleException {
Bundle bundle = Framework.getBundle(location);
if (bundle != null) {
bundle.update(inputStream);
return;
}
throw new BundleException("Could not update bundle " + location + ", because could not find it");
}


public void uninstallBundle(String location) throws BundleException {
Bundle bundle = Framework.getBundle(location);
if (bundle != null) {
BundleImpl bundleImpl = (BundleImpl) bundle;
try {
bundleImpl.getArchive().purge();

} catch (Exception e) {
log.log("uninstall bundle error: " + location + e.getMessage(), Logger.LogLevel.ERROR);
}
}
}

public List<Bundle> getBundles() {
return Framework.getBundles();
}

public Resources getDelegateResources() {
return RuntimeArgs.delegateResources;
}



public File getBundleFile(String location) {
Bundle bundle = Framework.getBundle(location);
return bundle != null ? ((BundleImpl) bundle).archive.getArchiveFile() : null;
}

public InputStream openAssetInputStream(String location, String fileName) throws IOException {
Bundle bundle = Framework.getBundle(location);
return bundle != null ? ((BundleImpl) bundle).archive.openAssetInputStream(fileName) : null;
}

public InputStream openNonAssetInputStream(String location, String str2) throws IOException {
Bundle bundle = Framework.getBundle(location);
return bundle != null ? ((BundleImpl) bundle).archive.openNonAssetInputStream(str2) : null;
}

public void addBundleDelayListener(BundleInstalledListener bundleListener) {
bundleDelayListeners.add(bundleListener);
}

public void removeBundleDelayListener(BundleInstalledListener bundleListener) {
bundleDelayListeners.remove(bundleListener);
}

public void addBundleSyncListener(BundleInstalledListener bundleListener) {
bundleSyncListeners.add(bundleListener);
}

public void removeBundleSyncListener(BundleInstalledListener bundleListener) {
bundleSyncListeners.remove(bundleListener);
}


}
19 changes: 19 additions & 0 deletions bundle/src/ctrip/android/bundle/framework/BundleException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ctrip.android.bundle.framework;

public class BundleException extends Exception {
private transient Throwable throwable;

public BundleException(String str, Throwable th) {
super(str);
this.throwable = th;
}

public BundleException(String str) {
super(str);
this.throwable = null;
}

public Throwable getNestedException() {
return this.throwable;
}
}
Loading

0 comments on commit 9ef50d7

Please sign in to comment.