Categories
discuss

An exception occurred applying plugin request [id: ‘com.android.application’] while integration Jetpack Compose

I am trying to implement JetPack Compose using Canary 8 but getting this error every time I try to sync gradle

An exception occurred applying plugin request [id: ‘com.android.application’]

My appbuild.gradle file is below

    plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '11'
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.4.30'
    }
}
dependencies {
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-alpha06'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

My build.gradle file is below

buildscript {
    ext {
        compose_version = '1.0.0-beta01'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha08'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and this is my gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionUrl=https://services.gradle.org/distributions/gradle-6.8.2-all.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

Answer

Android Gradle Plugin 7 requires Java 11.

Go to File > Project Structure and change JDK location to Embedded JDK.

Categories
discuss

Two dimensional array passed to new Map fails in typescript

Can’t pass two dimensional array to new Map in Typescript:

 const myArray: string[][] = [['DE', 'Germany'], ['AU', 'Austria']];
 const myMap = new Map(myArray);

Fails to me with TS2769: No overload matches this call. Am I doing something wrong here?

TS Version 3.9.9

Answer

If you dig through the log, you’ll see an error message of

Target requires 2 element(s) but source may have fewer.

The problem is that ['DE', 'Germany'] gets automatically type-widened to string[] – an array of strings, which may have two elements, or one, or zero – but the Map constructor requires such entry arrays to have at least 2 elements. The type information that the number of items in the array is 2 gets lost.

I’d put the array declaration on the same line as the new Map:

const myMap = new Map([['DE', 'Germany'], ['AU', 'Austria']]);

Another option is

const arr: [string, string][] = [['DE', 'Germany'], ['AU', 'Austria']];

to show that the array actually does have 2 items in it.

Also note that you can’t have a variable name start with a number – start it with something else, probably a letter.

Categories
discuss

How to correctly re-compile an apk file?

i was trying to inspect an .apk file for a project, so i decided to decompile it using APKtool using:

apktool d name.apk

I find what i wanted, changed it (i modified just a bunch of .json file and some .png, all in the /assets folder), but after re-building it with

apktool b name -o new.apk

I had some problems in installing the app on my device. Any suggestions?

It could be the signature of the .apk, according to other similar questions. But none of those explain the method to sign the .apk file in a correct way.

Answer

The following is for your reference


Compile, decompile and sign APK using apktool utility.

  1. Download latest apktool version.

  2. Download the batch file and aapt.exe.

  3. Create a folder anywhere in the PC and put all the apktool.jar, aapt.exe and the batch script in that folder.

  4. Open command prompt.

  5. Navigate to the folder where you placed apktool.jar, batch script and the aapt.exe.

  6. Now, you need to install the file using the ” IF ” command.

  7. Type the following command.

    apktool if name-of-the-app.apk

  8. For decompiling use the command “d”. The “d” stands for decompile.

    apktool d name-of-the-app.apk

  9. After the app is correctly decompiled, a new folder will be created in the same folder where you placed your app. This contains all the xml’s and smali files which can be edited for different mode’s.

  10. To recompile the app use the following command ” B “. The “b” simply means recompile.

    apktool b name-of-the-app-folder

  11. The final modded app will be in the “dist” folder located inside the original app folder created by apktool.

Signing the apk

  1. open a new command prompt and change into the sign-apk directory using cmd

  2. move the modified-unsigned apk into this folder

  3. then type the following command –

    java -jar signapk.jar certificate.pem key.pk8 path-of-the-folder-contaning-the-apk.apk path-of-the-new-signed-apk.apk

  4. Once compiled, the signed apk will be found in the same folder.

Categories
discuss

Cannot destructure property ‘data’ of ‘(intermediate value)’ as it is undefined

This error is in the console and it prevents the app from working, I cant find the bug at all if anyone could help? Its a MERN application

The code in question

export const getPosts = () => async (dispatch) => {
    try {
      const { data } = await api.fetchPosts();
  
      dispatch({ type: 'FETCH_ALL', payload: data });
    } catch (error) {
      console.log(error.message);
    }
  };

VSC is telling me await doesn’t effect this kind of expression, which it should as fetchPosts is a request? The code for this is below

export const fetchPosts = () => {
    axios.get(url)
}

Answer

The problem is that, although axios.get may return a promise, the fetchPosts function you’ve wrapped it in doesn’t return the promise that axios.get returns:

const fetchPosts = () => {
    axios.get(url);
};

const myFetch = fetchPosts();

console.log(myFetch); // will log `undefined`

If you rewrite fetchPosts as so:

export const fetchPosts = () => axios.get(url);

…with the implicit return from your arrow function, I think it should work. Alternatively, you could just explicitly return the result of axios.get:

const fetchPosts = () => {
    return axios.get(url);
};

…but your linter may complain about that.

Categories
discuss

Android 11 background location permission “Allow only while using the app” does not grant permission

As the title says, I’m currently in the process of updating my app to SDK 30 and having trouble with the ACCESS_BACKGROUND_LOCATION permission. Upon requesting the permission, the user is taken to the app location permission settings and is given 4 options:

  • Allow all the time
  • Allow only while using the app
  • Ask every time
  • Deny

All choices except “Allow all the time” do not grant the permission and return a -1 in the grantResults IntArray inside the onRequestPermissionsResult() callback function. Why? This is incredibly confusing. The naming would lead one to believe the permission would be granted and background permission requests only be made when the app is in focus. Is it possible to remove the “Allow only while using the app” option from the choices? How can I fix this?? I only see these choices confusing the user since the permission won’t be granted unless they choose “Allow all the time”.

Answer

Background location permission does not work like other permissions. It’s a request to elevate location permission from foreground-only to foreground & background.

User has to consciously select “Allow all the time” in order to do that and grant background location permission. Otherwise that permission is considered denied.

You cannot even request background location unless foreground location is already granted – when system permission activity shows up it should already have option 2 or 3 selected.

See https://developer.android.com/training/location/permissions#request-background-location

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..