일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- MVVM pattern
- prisma
- 안드로이드 mvp
- 자바
- Android
- 우분투 파이썬
- 유니티
- java
- Apollo Server
- PYTHON
- 자바기초
- 안드로이드 디자인패턴
- 파이썬 크롤링
- 안드로이드
- Data structure
- ubuntu python
- Kotlin
- 안드로이드 테스트
- LinkedList
- flutter
- dagger-hilt
- Design Pattern
- 웹크롤링
- unit test
- Nexus GraphQL
- Apollo GraphQL
- mvvm
- graphQL
- Android test
- Dependency Injection
- Today
- Total
Hun's Blog
안드로이드 라이브러리 만들기 본문
https://developer.android.com/studio/projects/android-library?hl=ko
Android 라이브러리 만들기 | Android 개발자 | Android Developers
Android 라이브러리를 생성하는 방법을 알아보세요.
developer.android.com
MAC OS
Android studio 4.2.1
java
Openjdk 1.8
Android Library
- Android 라이브러리는 구조적으로 안드로이드 앱 모듈과 동일
- 소스 코드, 리소스 파일, 매니페스트를 비롯하여 앱을 빌드하는 데 필요한 모든 항목이 포함될 수 있음
- APK로 컴파일되는 대신 Android 앱 모듈의 종속 항목으로 사용할 수 있는 AAR로 컴파일
AAR ?
해당 파일에는 안드로이드 리소스 및 매니페스트 파일이 포함될 수 있음
자바 클래스 및 메서드 외에 레이아웃 및 드로어블과 같은 공유 리소스를 번들로 구성할 수 있음
또한 앱 모듈의 C/C++ 코드에서 사용할 라이브러리를 포함할 수 있음
라이브러리의 유용성
- 액티비티, 서비스 등 일부 구성요소를 동일하게 사용하는 여러 앱을 빌드하는 경우
- 여러 APK 변형에 포함되는 앱을 빌드하며 두 버전에서 모두 동일한 핵심 구성요소가 필요한 경우
아래 2가지 항목을 바탕으로 간단한 샘플을 만들어보고 라이브러리 모듈생성의 아주아주 기초적인 흐름을 정리해보자.

1. 라이브러리 모듈 생성

- New project -> No Activity 프로젝트 생성(empty activity도 상관없음)

- File -> New -> New Module -> Android Library -> Finish

- plugins에서 id 'com.android.application' -> id 'com.android.library' 변경
- defaultConfig에서 applicationId "com.example.libsample" 삭제

- Sync Project with Gradle Files 를 클릭 or Sync Now
* toast 함수 추가
samplelib/src/main/java/com.example.samplelib/Test.java
package com.example.samplelib;
import android.content.Context;
import android.widget.Toast;
public class Test {
public void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
}
* aar 빌드

방법 A
Project에서 mylibrary선택 -> build -> Make Module 'LibSample.samplelib'
방법 B
Terminal -> ./gradlew assembleDebug
* aar 경로
samplelib/build/output/aar/samplelib-debug.aar
2. AAR 또는 JAR을 종속 항목으로 추가
- empty activity 프로젝트 생성 후 TestApplication/app/libs 경로에 위에서 생성한 aar추가
- File > Project Structure > Dependencies로 이동
- Declared Dependencies 탭 -> Jar/AAR Dependency 선택
- libs/samplelib-debug.aar 입력후 ok
- File > Sync Project with Gradle Files 실행
- 추가된 라이브러리 확인

*메인 액티비티에서 라이브러리 함수 호출
package com.example.testapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.samplelib.Test;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test test = new Test();
test.showToast(this, "library_sample");
}
}
* 앱실행 혹은 Terminal -> ./gradlew installDebug

* 설치된 앱 실행 확인

'Android' 카테고리의 다른 글
개인 프로젝트(Bookmark-kotlin)를 통해 알아보는 ViewModel의 생성 (0) | 2020.12.04 |
---|---|
개인 프로젝트(Bookmark-kotlin)를 통해 알아보는 MVC-MVP-MVVM (0) | 2020.12.03 |
Start an activity using an animation (0) | 2020.11.22 |
Android Dagger-Hilt 적용기 (6) - ViewModel + Repository(Sandwich) (7) | 2020.11.20 |
[번역]Coroutines on Android (part I): Getting the background (0) | 2020.11.11 |