Android SDK集成

197 阅读1分钟

环境

  • Android Studio Chipmunk | 2021.2.1
  • Gradle Version 6.7.1
  • Android Gradle Plugin Version 4.2.2

允许HTTP请求

  • Manifest中Application中添加该属性即可
android:usesCleartextTraffic="true"

Android11文件存储权限开关打开

android:requestLegacyExternalStorage="true"
  • 上述打开,发现在Android11依然会被忽略掉
  • targetSDK 26~28即可避免以上问题
android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.androidhidecodesdkdemo"
        minSdk 21
        targetSdk 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

AndroidX支持

android.useAndroidX=true
android.enableJetifier=true

aar包 jar包加入之后需要添加以下代码到app的build.gradle的dependencies中

    implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])

添加权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.any" />

存储权限、HTTP权限

<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:roundIcon="@mipmap/ic_launcher_round"
    android:requestLegacyExternalStorage="true"     
    android:supportsRtl="true"
    android:theme="@style/Theme.AndroidHideCodeSDKDemo"
    android:usesCleartextTraffic="true"
    tools:targetApi="s">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Gradle7.0以下三方库依赖配置

  • 在项目级别的build.gradle文件中配置
ext {
    compileSdkVersion = 30
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.5.0'

    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        //友盟
        maven { url 'https://repo1.maven.org/maven2/' }
        // 设置仓库
        maven { url "https://chaquo.com/maven" }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

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

三方库依赖

  • 在APP下的build.gradle中配置
dependencies {

    implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation project(path: ':hide_code')
    implementation project(path: ':opencv')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // 必要的三方库
    implementation "androidx.camera:camera-core:1.1.0-beta02"
    implementation "androidx.camera:camera-camera2:1.1.0-beta02"
    implementation "androidx.camera:camera-lifecycle:1.1.0-beta02"
    implementation "androidx.camera:camera-view:1.1.0-beta02"
    implementation 'com.orhanobut:logger:2.2.0'
    implementation 'com.google.zxing:core:3.3.3'
    implementation 'com.squareup.okio:okio:2.2.2'
    implementation "com.squareup.okhttp3:okhttp:3.14.9"
    implementation "com.squareup.okhttp3:logging-interceptor:3.14.9"
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "com.squareup.retrofit2:adapter-rxjava3:2.9.0"
    implementation "io.reactivex.rxjava3:rxjava:3.0.11"
    implementation "io.reactivex.rxjava3:rxandroid:3.0.0"
    implementation "com.uber.autodispose2:autodispose-androidx-lifecycle:2.0.0"
}

三方库源、Kotlin支持

ext {
    compileSdkVersion = 30
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.5.0'

    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        //友盟
        maven { url 'https://repo1.maven.org/maven2/' }
        // 设置仓库
        maven { url "https://chaquo.com/maven" }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

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

最后添加aar包或者moudle文件