android lib module 생성 후 application에서 참조하기
이미지 출처: https://medium.com/@101/speed-up-gradle-build-in-android-studio-80a5f74ac9ed#.p3j4dbawk
01. 새로운 프로젝트 생성
- 01.1. 프로젝트 생성
Android Studio: File > New > New Project...
- 01.2. Configure your new project 정보입력
ex>
Application name: Recipe
Company name: korn123.blog.me
- 01.3. Select the form factors your app will run on 정보입력
ex>
Minimum SDK: API 19: Android 4.4 (KitKat)
- 01.4. Add an Activity to Mobile
ex>
Empty Activity
- 01.5. Customize the Activity 정보입력
ex>
Activity Name: MainActivity
Layout Name: activity_main
02. 참조대상 lib module 생성
- 02.1. 새로운 모듈 생성
Android Studio: File > New > New Module...
- 02.2. New Module 선택
Android Library
- 02.3. Configure your new module 정보입력
ex>
Application/Library name: Commons
Module name: commons
Package name: me.blog.korn123.commons
Minimum SDK: API 19: Android 4.4 (KitKat)
03. lib module(commons-lang3 wrapper util 만들기)
- 03.1. build.gradle(Module: commons)에 dependeny 추가
1 2 3 4 5 6 7 8 9 | dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.1' testCompile 'junit:junit:4.12' compile 'org.apache.commons:commons-lang3:3.0' } | cs |
- 03.2. StringUtils Wrapper Class 생성
1 2 3 4 5 6 7 8 9 10 11 12 | package me.blog.korn123.commons.utils; /** * Created by hanjoong on 2017-03-09. */ public class StringUtils { public static String upperCase(String str) { return org.apache.commons.lang3.StringUtils.upperCase(str); } } | cs |
04. application에서 wrapping된 lib 사용하기
- 04.1. build.gradle(Module: app)에 dependeny 추가
1 2 3 4 5 6 7 8 9 10 | dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.1' testCompile 'junit:junit:4.12' compile project(':commons') } | cs |
- 04.2 . MainActivity wrapping된 class 호출
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package me.blog.korn123.recipe; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import me.blog.korn123.commons.utils.StringUtils; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView welcome = (TextView)findViewById(R.id.welcome); welcome.setText(StringUtils.upperCase("Hello android lib module !!!")); } } | cs |