Let Android Library Documentation

repository·master·Indexed 19 days ago

https://github.com/canelmas/let

An annotation-based library for Android that uses Aspect-Oriented Programming (AOP) to simplify the runtime permission model. It reduces boilerplate by using the @AskPermission annotation to automatically request permissions before executing methods, utilizing Let.handle() for result processing and the RuntimePermissionListener interface for callbacks.

Tokens
1.3K
Snippets
5
Records
5
Agent score
18%

What's inside Let

  1. Install Let via Gradle

    master

    To use Let in your Android project, add the Let plugin to your buildscript dependencies and apply the plugin in your app-level build file.

    For Java projects: Use version 0.1.11.

    For Kotlin projects: Use version 1.0.0-beta1.

    // Java project setup
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.canelmas.let:let-plugin:0.1.11'
        }
    }
    
    apply plugin: 'com.android.application'
    apply plugin: 'let'
    
    repositories {
        jcenter()
    }
    // Kotlin project setup
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.canelmas.let:let-plugin:1.0.0-beta1'
        }
    }
    
    apply plugin: 'com.android.application'
    apply plugin: 'let'
    
    repositories {
        jcenter()
    }
  2. Handle permission results with Let.handle()

    master

    To allow Let to process the results of a permission request, you must override onRequestPermissionsResult in the Activity or Fragment where your @AskPermission annotated methods are located and delegate the call to Let.handle().

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Let.handle(this, requestCode, permissions, grantResults);
    }
  3. Request permissions using @AskPermission

    master

    Annotate methods that require runtime permissions with @AskPermission. Let will check if the permissions are already granted. If they are, the method executes immediately. If not, Let pauses execution, requests the permissions, and only resumes the method if the user grants them.

    @AskPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    private void getUserLocationAndDoSomething() {
        // This code only runs if permission is granted
        ...
    }
    
    @AskPermission({
        Manifest.permission.READ_CONTACTS,
        Manifest.permission.CALL_PHONE,
        Manifest.permission.CAMERA
    })
    private void skipTutorial() {
        // This code only runs if all listed permissions are granted
        startActivity(new Intent(this, HomeActivity.class));
    }
  4. Implement RuntimePermissionListener for callbacks

    master

    To receive notifications about permission rationales or denials, your Activity or Fragment must implement the RuntimePermissionListener interface. This allows you to handle custom UI logic, such as showing a dialog for rationales or directing users to settings if they selected 'Never Ask Again'.

    public class SampleActivity extends AppCompatActivity implements RuntimePermissionListener {
        
        @Override
        public void onShowPermissionRationale(List<String> permissions, final RuntimePermissionRequest request) {
            /**
            * Show rationale in a dialog, then retry:
            * request.retry()
            */
        }
      
        @Override
        public void onPermissionDenied(List<DeniedPermission> deniedPermissionList) {
            /**
            * Handle denied permissions (e.g., update UI or prompt to go to settings)
            */
        }
    }
  5. Configure Proguard for Let

    master

    Add the following rules to your Proguard configuration to prevent the obfuscation of Let's classes, listeners, and annotated methods.

    -keep class com.canelmas.let.** { *; }
    -keepnames class * implements com.canelmas.let.RuntimePermissionListener
    
    -keepclassmembers class * implements com.canelmas.let.RuntimePermissionListener {
        public void onRequestPermissionsResult(***);
    }
    
    -keepclasseswithmembernames class * {
        @com.canelmas.let.* <methods>;
    }