본문 바로가기
개발/ETC

[ETC] JitPack으로 라이브러리 모듈 배포하기

by JhDroid 2021. 3. 2.
728x90

JitPack

  • 자신이 만든 라이브러리를 배포하는 방법들 중 JitPack을 통해 라이브러리 모듈을 배포하는 방법을 알아보겠습니다.
  • JitPack을 사용하면 크게 설정할 것 없이 Github에 있는 자신의 프로젝트를 배포할 수 있습니다.

 

프로젝트에 JitPack 설정

  • 시작 전 프로젝트 구조는 다음과 같습니다.
    • Github 프로젝트는 미리 생성했습니다.
    • 꼭 다음과 같은 구조일 필요는 없습니다.(라이브러리 모듈만 존재해도 가능)

  • app : 테스트를 위한 앱 모듈 (배포하지 않음)
  • exlibrary : 테스트를 위한 라이브러리 모듈 (배포 대상)
    • Example 클래스 코드
class Example {
    fun getString(): String = "Jhdroid"
    fun getBlogUrl(): String = "https://jhdroid.tistory.com/"
}

 

1. maven-gradle-plugin 추가

  • Project Gradle(최상위 그래들)에 maven-gradle-plugin를 추가해줍니다.
buildscript {
    ...
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // 추가
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

 

2. com.github.dcendents.android-maven 추가

  • Module Gradle(앱 그래들) plugins에 다음과 같이 추가해줍니다.
  • 여기서 추가할 때는 application 모듈이 아니라 library 모듈에만 설정하면 됩니다.
    • 버전에 따라 추가 방법이 다르니 프로젝트에 어떤 방식으로 설정되어있는지 확인 후 추가해주세요.
plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'com.github.dcendents.android-maven' // 추가
}

// 또는

apply plugin: 'com.github.dcendents.android-maven'

 

3. group 설정

  • 자신의 Github 계정 주소를 Project Gradle에 group에 추가합니다.
    • 계정 주소는 com.gihub.<username> 입니다
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ...
    dependencies {
        ...
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    }
}

allprojects {
    repositories {
        ...
    }

    // 추가
    group = 'com.github.JhDroid'
}
  • 여기까지 완료되었다면 해당 프로젝트를 자신의 Github 저장소에 Push 해줍니다.

 

Github에서 릴리즈 태그 생성 후 JitPack에 배포

  • 자신의 Github 프로젝트로 이동하면 메인화면 오른쪽 메뉴에 `Releases`메뉴를 확인합니다.
    • 좌측 이미지는 이미 Releases를 생성했다면 볼 수 있는 화면이고 오른쪽은 생성한 적이 없을 때 볼 수 있는 화면입니다.

 

1. Release 생성

  • 오른쪽 메뉴에서 Releases를 선택해 Releases 화면으로 이동합니다.

1-1. Release를 생성한 적이 있다면

  • Release를 생성한적이 있다면 다음과 같은 화면이 나오고 여기서 새로운 버전의 Release를 추가하기 위해 `Draft a new release`를 선택합니다.

 

1-2. Release를 생성한적 없다면

  • Release를 생성한적 없다면 다음과 같은 화면이 나오고 여기서 새로운 Release를 생성하기 위해 `Create a new release`를 선택합니다.

 

  • Release 생성화면은 동일하며 다음과 같습니다.

  • 여기서 새롭게 생성할 Release의 버전을 Tag Version에 추가합니다.
    • 저는 이미 1.0.0을 생성했기 때문에 이번에는 1.0.1로 추가하겠습니다.
    • Tag Version을 제외한 나머지는 추가하지 않아도 됩니다.(선택)

Release가 생성이 완료된 후 화면

 

2. JitPack에 배포

  • 여기까지 완료했다면 JitPack에 배포하는 과정은 간단합니다.
  • JitPack 홈페이지에 접속하면 다음과 같은 화면을 볼 수 있습니다.
    • JitPack 홈페이지 
    • 여기서 `Git repo url` 부분에 자신의 Github 저장소 주소를 추가하고 Look up을 선택합니다.

  • Github 저장소 주소를 추가하고 Look up을 누르면 프로젝트의 상태와 자신이 추가한 Release 들을 확인할 수 있습니다.
    • Look up을 누르면 가장 상위에 있는 Release 버전만 배포 시도를 하고 이전 버전은 하지 않습니다.

가장 최신버전인 1.0.1 버전만 배포 시도를 한다.

  • Get it 버튼이 활성화되어있다면 이를 선택하면 다음과 같은 가이드를 볼 수 있습니다.
    • Log의 문서 이미지의 버튼을 누르면 빌드 관련 로그를 확인할 수 있습니다.

활성화된 Get it 버튼을 선택
Get it버튼을 누르면 변경되는 가이드

 

배포된 라이브러리 모듈 사용하기

  • 이제 JitPack에 나온 가이드대로 배포한 라이브러리 모듈을 사용해보겠습니다.
    • 위 프로젝트에서 Application Module(:app)에 라이브러리 모듈을 추가해서 테스트하는 과정입니다.

1.  JitPack maven url 추가

  • Project Gradle에 다음과 같이 jitpack maven url을 추가합니다.

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

 

2. 라이브러리 의존성 추가

  • Application Module Gradle에 다음과 같이 라이브러리 모듈의 의존성을 추가합니다.
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    // 추가
    implementation 'com.github.JhDroid:android-jitpack-example:1.0.1'
}

 

3. 확인

package com.jhdroid.jitpack.example

...
import com.jhdroid.jitpack.exlibrary.Example

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val example = Example()
        Log.d("jhdroid", "example library get string : ${example.getString()}")
        Log.d("jhdroid", "example library get blog url : ${example.getBlogUrl()}")
    }
}

로그 출력 결과

  • 정상적으로 라이브러리 모듈에 있는 클래스와 함수를 불러올 수 있는 것을 확인할 수 있습니다.

 

 

JhDroid/android-jitpack-example

Contribute to JhDroid/android-jitpack-example development by creating an account on GitHub.

github.com

 

* 글에 틀린 부분이 있으면 댓글 부탁드립니다 :D

 

728x90