1

In my Qt Android (5.6+, latest SDK and NDK 10d) app I need to request permissions at runtime because of targetApi 26. From Google docs I've found out that I have to do something like this:

import android.Manifest.permission;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;

public class  MyActivity extends QtActivity implements ActivityCompat.OnRequestPermissionsResultCallback
{
  private static final String[] LOCATION_PERMS = {
      permission.ACCESS_FINE_LOCATION
  };
  private static final int LOCATION_REQUEST = 1337;

    private boolean hasPermission(String perm)
  {
    return (PackageManager.PERMISSION_GRANTED == checkSelfPermission(perm));
  }

  public boolean canAccessLocation()
  {
    return hasPermission(permission.ACCESS_FINE_LOCATION);
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
  {
    //Figure out what was given to us
    switch(requestCode)
    {
      case LOCATION_REQUEST:
        if (canAccessLocation())
          Log.i(TAG, "Access to location is granted");
        else {
          Log.e(TAG, "Access to location is denied");
          finish();
        }
        break;
    }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      //Ensure we can run the app
      if (!canAccessLocation())
        requestPermissions( LOCATION_PERMS, LOCATION_REQUEST );
    }
  }
}

I've also installed Android Support Repository: enter image description here

And this is my build.gradle:

buildscript {
    repositories {
        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

apply plugin: 'com.android.application'

dependencies {
    compile "com.android.support:support-v4:27.0.2"
    compile "com.android.support:support-v13:27.0.2"
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

But I get error: package android.support.v4.app does not exist. I also tried to find android-support-v4.jar on my workstation but with no success. Adding

compile 'com.android.support:support-v4:+'

from here doesn't make any difference.

One important thing: my project is built from command line with help of CMake, I can't use any IDE like Android Studio or Eclipse. What am I missing?

Thanks!

UPDATE: Full error list: enter image description here

Max Pashkov
  • 404
  • 1
  • 3
  • 12

2 Answers2

1

I don't know if you still have this problem, but in case anyone wants to know...

I changed the compile sdk version to: 27: Android 8.1 (Oreo) was 26: Android 8.0 (Oreo) and in build.grade

dependencies {
    implementation 'com.android.support:appcompat-v7:27.0.0'
    implementation 'com.android.support:design:27.0.0'
    implementation 'com.android.support:support-v4:27.1.1'
}

Apparently the 27.1.1 in android.support:support did the trick (27.0.0 didn't work). I hope this helps (:

0

Effectively, by applying dependencies from Android Studio, an updated build.gradle works with thee settings:

apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'

}

I also guess you need to import Activity:

import android.app.Activity;
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47