smartcar / android-sdk

Smartcar Android Auth SDK
https://smartcar.github.io/android-sdk
MIT License
19 stars 2 forks source link

smart car android -SDK In android forcefully close my application while verifying vehicles in browser. #27

Closed sahilXperge closed 2 years ago

sahilXperge commented 2 years ago

I implemented smart car sdk in android flutter. When I call native method it work perfectly till select vehicle and enter email. But, when I click on submit in browser it will close all running applications in background.

Flutter doctor

apple@Sahil-MacBook-Air climatekarmafe % flutter doctor -v
[✓] Flutter (Channel stable, 3.3.1, on macOS 12.0.1 21A559 darwin-x64, locale en-GB)
    • Flutter version 3.3.1 on channel stable at /Users/apple/Downloads/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4f9d92fbbd (5 days ago), 2022-09-06 17:54:53 -0700
    • Engine revision 3efdf03e73
    • Dart version 2.18.0
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/apple/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that
        responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for
      instructions.

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google
    Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] Connected device (2 available)
    • CPH2015 (mobile) • Z9PJAMUKHI4LO7UC • android-arm64 • Android 9 (API 28)
    • macOS (desktop)  • macos            • darwin-x64    • macOS 12.0.1 21A559 darwin-x64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.

build.gradle(app)

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 33
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.climatekarma.climate_karma"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
        minSdkVersion 28
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    compileOnly("io.flutter:flutter_embedding_debug:1.0.0-0fdb562ac8068ce3dda6b69aca3f355f4d1d2718")
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.smartcar.sdk:smartcar-auth:3.0.0'
    implementation 'androidx.browser:browser:1.0.0'
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxxxxxxxxxxxxx">
    <uses-permission android:name="android.permission.INTERNET"/>
   <application
        android:label="xxxxxxxxxxxxxxx"
        android:name="${applicationName}"
       android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

       <activity
           android:exported="true"
           android:name="com.smartcar.sdk.SmartcarCodeReceiver">
           <intent-filter>
               <action android:name="android.intent.action.VIEW" />
               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />
               <data
                   android:host="@string/smartcar_auth_host"
                   android:scheme="@string/smartcar_auth_scheme" />
           </intent-filter>
       </activity>

        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

MainActivity.kt

package com.climatekarma.climate_karma

import android.content.Context
import android.view.View
import com.smartcar.sdk.SmartcarAuth
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
    private var CLIENT_ID: String? = null
    private var REDIRECT_URI: String? = null
    private var SCOPE: Array<String>? = null
    private var smartcarAuth: SmartcarAuth? = null

    private var appContext: Context? = null
    lateinit var smartCarChannel:MethodChannel
    private val SMART_CAR_CHANNEL_NAME = "xxxxxxxxxxxxxxxxxx/smartcar"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        appContext = applicationContext
        CLIENT_ID = Constants.SMART_CAR_CLIENT_ID
       REDIRECT_URI = Constants.SMART_CAR_AUTH_SCHEME + "://" + Constants.SMART_CAR_AUTH_HOST.trim()
       SCOPE = arrayOf("required:read_vehicle_info")
        smartCarChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger,SMART_CAR_CHANNEL_NAME)

        //Recive Data from flutter
        smartCarChannel.setMethodCallHandler { call, result ->
            if(call.method == "connectSmartCar"){
              //  val arguments = call.arguments as Map<String,String>

                smartcarAuth = SmartcarAuth(
                    CLIENT_ID,
                    REDIRECT_URI,
                    SCOPE,
                    true
                ) { smartcarResponse ->
                    result.success("${smartcarResponse.code}")
                }
                smartcarAuth?.launchAuthFlow(getApplicationContext());
                val connectButton = View(appContext)
                smartcarAuth?.addClickHandler(appContext, connectButton)
                connectButton.performClick()
                connectButton.isPressed = true

            }
        }

    }

}

Logs

Click to see logs

sahilXperge commented 2 years ago

There is no sdk issue it work perfectly after adding def appcompat_version = "1.5.1" implementation "androidx.appcompat:appcompat:$appcompat_version"

yisusparkr commented 1 year ago

There is no sdk issue it work perfectly after adding def appcompat_version = "1.5.1" implementation "androidx.appcompat:appcompat:$appcompat_version"

I'm also integrating the SDK to a Flutter project and was facing the same issue, cannot believe adding the appcompat implementation would fix it. Thanks @sahilXperge!