Skip to content

Commit

Permalink
添加编译Libary
Browse files Browse the repository at this point in the history
  • Loading branch information
xunhu.gui committed May 17, 2018
1 parent 5070c80 commit 4c17627
Show file tree
Hide file tree
Showing 26 changed files with 571 additions and 6 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 4 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

javaCompileOptions {
annotationProcessorOptions {
arguments = [testParam: 'I am diaobaole']
includeCompileClasspath true
classNames = ["com.example.ioc_compiler.IocProcessor"]
}
}
}
buildTypes {
release {
Expand Down Expand Up @@ -63,5 +71,7 @@ dependencies {
compile 'com.android.support:design:25.3.1'

compile 'com.luffykou:android-common-utils:1.1.3'
compile project(':ioc-api')
annotationProcessor project(':ioc-compiler')

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import android.view.View;
import android.widget.TextView;

import com.example.ico_annotation.BindView;
import com.example.ico_annotation.ContentView;
import com.example.ioc_apu.ViewInjector;

import tiger.com.lp.dailydemo.R;
import tiger.com.lp.dailydemo.designpatterns.observer.ActivityController;
import tiger.com.lp.dailydemo.designpatterns.observer.BaseModel;
Expand All @@ -23,19 +27,19 @@
* @description
*/

@ContentView(R.layout.activity_observer)
public class ObserverActivity extends Activity{
private ActivityController activityController;
private DemoObserver demoObserver;

private ObserverTextView info;
@BindView(R.id.info_ob)
public ObserverTextView info;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_observer);
info = findViewById(R.id.info);

ViewInjector.injectView(this);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_observer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


<tiger.com.lp.dailydemo.designpatterns.observer.concrete.ObserverTextView
android:id="@+id/info"
android:id="@+id/info_ob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
Expand All @@ -18,6 +19,7 @@ allprojects {
repositories {
jcenter()
mavenCentral()
google()
}
}

Expand Down
1 change: 1 addition & 0 deletions ico-annotation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
8 changes: 8 additions & 0 deletions ico-annotation/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apply plugin: 'java-library'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.ico_annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.FIELD)
public @interface BindView {
int value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.ico_annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*
* 绑定ContentView的注解
*/

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface ContentView {
int value();
}
1 change: 1 addition & 0 deletions ioc-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions ioc-api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"


defaultConfig {
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:27.1.1'
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile project(':ico-annotation')

}
21 changes: 21 additions & 0 deletions ioc-api/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# 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 *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.ioc_apu;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.example.ioc_apu.test", appContext.getPackageName());
}
}
2 changes: 2 additions & 0 deletions ioc-api/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ioc_apu" />
10 changes: 10 additions & 0 deletions ioc-api/src/main/java/com/example/ioc_apu/ViewInject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.ioc_apu;

/**
*
* 注入接口
*/

public interface ViewInject<T> {
void inject(T target, Object source);
}
43 changes: 43 additions & 0 deletions ioc-api/src/main/java/com/example/ioc_apu/ViewInjector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.ioc_apu;

import android.app.Activity;
import android.view.View;

/**
* Created by li.zhipeng on 2017/3/17.
*
* 提供注入的静态方法,间接调用了io-complier的编译生成的类方法
*/

public class ViewInjector {

private static final String SUFFIX = "$$ViewInject";

public static void injectView(Activity activity) {
ViewInject proxyActivity = findProxyActivity(activity);
proxyActivity.inject(activity, activity);
}

public static void injectView(Object object, View view) {
ViewInject proxyActivity = findProxyActivity(object);
proxyActivity.inject(object, view);
}

/**
* 通过反射创建要使用的类的对象
* */
private static ViewInject findProxyActivity(Object activity) {
try {
Class<?> clazz = activity.getClass();
Class<?> injectorClazz = Class.forName(clazz.getName() + SUFFIX);
return (ViewInject) injectorClazz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
throw new RuntimeException(String.format("can not find %s , something when compiler.", activity.getClass().getSimpleName() + SUFFIX));
}
}
3 changes: 3 additions & 0 deletions ioc-api/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">ioc-apu</string>
</resources>
17 changes: 17 additions & 0 deletions ioc-api/src/test/java/com/example/ioc_apu/ExampleUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.ioc_apu;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}
1 change: 1 addition & 0 deletions ioc-compiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
14 changes: 14 additions & 0 deletions ioc-compiler/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apply plugin: 'java-library'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// auto-service库可以帮我们去生成META-INF等信息。
compile 'com.google.auto.service:auto-service:1.0-rc2'
implementation project(':ico-annotation')
// 如果找不到javax包,可以直接引入本地jdk的jar包
compile files ('/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/rt.jar')

targetCompatibility = '1.7'
sourceCompatibility = '1.7'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.ioc_compiler;

import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;

/**
*
* 类的工具类
*/

public class ClassValidator {

/**
* 判断是否是private修饰
* */
static boolean isPrivate(Element annotatedClass){
return annotatedClass.getModifiers().contains(Modifier.PRIVATE);

}

/**
* 获取类的完整路径
* */
static String getClassName(TypeElement type, String packageName){
int packageLen = packageName.length() + 1;
return type.getQualifiedName().toString().substring(packageLen).replace(".", "$");
}

}
Loading

0 comments on commit 4c17627

Please sign in to comment.