androidkotlinandroid-permissionsandroid-11

Permissions on AAR Android 11 adding external library


I am developing an application that makes use of a third party SDK, this SDK comes in an example project. I only copied the contents of the libs folder to a new project, Add the library in the build.gradle and it loads without problems, since I can see it in code and use its classes without any exceptions, the problem is that it gives me a permissions error in the Log.

I/DeviceAPI: UHF device = C72_6765

I/DeviceAPI: UHF_Init----------->DEVICE_C72_6765

D/DeviceAPI: [mt_gpio_ioctl] platform=6765, gpio=set 165 0111000100, gpiolen=18

E/DeviceAPI: [mt_gpio_ioctl] open error: [13]: Permission denied

D/DeviceAPI: [mt_gpio_ioctl] platform=6765, gpio=set 21 0111000100, gpiolen=17

E/DeviceAPI: [mt_gpio_ioctl] open error: [13]: Permission denied W/_solutions.rfid: type=1400 audit(0.0:1956): avc: denied { read write } for name="mt_gpio" dev="sysfs" ino=35345 scontext=u:r:untrusted_app:s0:c172,c256,c512,c768 tcontext=u:object_r:mt_gpio:s0 tclass=file permissive=0 app=com.computx_solutions.rfid

I/DeviceAPI: UHF_Init----------->DevIsOpen = 1

I/DeviceAPI: UHF_OpenAndConnect: uart = /dev/ttyS1

D/DeviceAPI_SerialPort: [SerialPort_Open] [13]: Permission denied

All Permission Denied errors do not appear in the sample application.

Working App build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}
android {
    compileSdkVersion 31
//    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.android.uhftest"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        //testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                abiFilters "armeabi"//abi体系结构下的so库
            }
        }
        vectorDrawables {
            useSupportLibrary true
        }
    }

    signingConfigs {
        debug {
            File strFile = new File("/chainway.keystore")
            storeFile file(strFile)
            storePassword "123456"
            keyPassword "123456"
            keyAlias "chainway"
        }
        release {
            File strFile = new File("/chainway.keystore")
            storeFile file(strFile)
            storePassword "123456"
            keyPassword "123456"
            keyAlias "chainway"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation files('libs/DeviceAPI_ver20220323_release.aar')


    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

}

Working manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.uhf"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:screenOrientation="portrait"
        android:theme="@style/CustomTheme">
        <activity
            android:name=".MainActivity"
            android:label="Main Acitivity"
            android:theme="@style/CustomTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.UHFMainActivity"
            android:label="@string/app_name" />
    </application>

</manifest>

NOT Working build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.computx_solutions.rfid"
        minSdk 27
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    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 = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"


    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation files('libs/DeviceAPI_ver20220323_release.aar')
}

NOT working manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.computx_solutions.rfid">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.RFIDInventory"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.RFIDInventory">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I also tried to put the same sdk in both projects and it didn't work. Is there any other file that involves permissions or configurations of the external libraries that you are not considering?

It is worth mentioning that this error only occurs with android 11 since previously I was using a device with android 8 and it did not give me permission errors, I also tried to grant full write and read permissions and it did not work, besides that I have access to the code that works and nowhere does it use the permissions request and even in android it shows that it does not have any special permissions.


Solution

  • I got it working adding the signing configs. Sometimes it still doesn't work and it stops at init() err UHFOpenAndConnect result:-1. I don't know what they did in the new APIs.
    I copied the files chainway.keystore and in build.gradle I added:

    signingConfigs {
        debug {
            File strFile = new File("/chainway.keystore")
            storeFile file(strFile)
            storePassword "123456"
            keyPassword "123456"
            keyAlias "chainway"
        }
        release {
            File strFile = new File("/chainway.keystore")
            storeFile file(strFile)
            storePassword "123456"
            keyPassword "123456"
            keyAlias "chainway"
        }
    }
    
    buildTypes {
        release {
            minifyEnabled false
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }