Git Product home page Git Product logo

autoupdateapk-android's Introduction

AutoUpdateApk-Android

Android library for updating your project by downloading the latest apk file of your program from the server

Install

Gradle

Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Add the dependency (CHECK REPO TAG AND REPLACE "Tag" WITH CURRENT REPO LATEST RELEASE TAG)

	dependencies {
	        implementation 'com.github.2sweetheart2:AutoUpdateApk-Android:Tag'
	}

Maven

Add the JitPack repository to your build file

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add the dependency (CHECK REPO TAG AND REPLACE "Tag" WITH CURRENT REPO LATEST RELEASE TAG)

	<dependency>
	    <groupId>com.github.2sweetheart2</groupId>
	    <artifactId>AutoUpdateApk-Android</artifactId>
	    <version>Tag</version>
	</dependency>

Usage

Permissions

add this in your AndroidManifest file:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
      ...
      android:requestLegacyExternalStorage="true"
    </application>

Code

first, you must to check user permissions:

private val REQUEST_EXTERNAL_PERMISSION_CODE = 666
    private var PERMISSIONS_EXTERNAL_STORAGE =
        arrayOf(READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE)

    private fun checkPermission(permissions: Array<String>) {
        if (ContextCompat.checkSelfPermission(
                this@AutoUpdateExample,
                permissions[0]
            ) == PackageManager.PERMISSION_DENIED
            || ContextCompat.checkSelfPermission(
                this@AutoUpdateExample,
                permissions[1]
            ) == PackageManager.PERMISSION_DENIED
        ) {

            ActivityCompat.requestPermissions(
                this@AutoUpdateExample,
                PERMISSIONS_EXTERNAL_STORAGE,
                REQUEST_EXTERNAL_PERMISSION_CODE
            )
        } else {
            Toast.makeText(this@AutoUpdateExample, "Permission already granted", Toast.LENGTH_SHORT).show()
            runUpdater()
        }
    }

override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_EXTERNAL_PERMISSION_CODE && grantResults[0]==PackageManager.PERMISSION_GRANTED) {
            runUpdater()
        }
    }

runUpdater():

    private fun runUpdater(){
        val autoUpdate = AutoUpdate("url", BuildConfig.VERSION_CODE, null, null)
        autoUpdate.getUpdate(supportFragmentManager,false)
    }

for run check function and runUpdater(), call checkPermission(PERMISSIONS_EXTERNAL_STORAGE)

Modify

you can replace old dialog. For do this, you need put your dialog layout id in constructor ``` AutoUpdate("url", BuildConfig.VERSION_CODE, YOUR DIALOG'S LAYOUT ID, "response") ``

** BE CAREFUL **. Your custom dialog must include this:

  1. TextView (title of update) with id - update_update_title
  2. TextView (version_str) with id - update_update_version
  3. LinearLayout (layout with all change log texts) with id - update_update_change_log_list
  4. Button (button update) with id - update_update_update_btn
  5. Button (button skip) with id - update_update_skip_btn

Explanations

the first parameter takes the URL of your server, which should return such a json-string

{
      "version_str":"NEW VERSION, DOWNLOAD NOW", # - this text will be displayed under the title of the dialog
      "version":YOUR APP WERSION (1,2,19 .etc), # - this need for check build version client and apk on server, check versionCode in Gradle
      "title":"New Update", # - title of update
      "change_log":[{"title":"Title 1","text":"title 1 text"},{"title":"Title 2","text":"title 2 text"}], # - update log. The new object in the array will be shown as a new block
      "update_link":"url to new apk" 
}

last parameter takes json key, if your server send json like this:

{"response":
  {"version_str":"123",...},
...
}

using such an example of a server response, you need to specify such parameters in the constructor AutoUpdate("url", BuildConfig.VERSION_CODE, null, "response")

if your server response only

{"version_str":"123"...}

AutoUpdate("url", BuildConfig.VERSION_CODE, null, null)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.